Author Topic: Uncaught TypeError in cKeyNav.bodyKeyDown typing in empty grid with groupModel  (Read 602 times)

jplevene

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 210
    • View Profile
Uncaught TypeError in cKeyNav.bodyKeyDown when typing in an empty grid with groupModel enabled (v11.1.0)

Description:
When groupModel is active but the grid currently contains no data (empty dataModel.data), clicking on the grid body to give it focus and typing any character on the keyboard causes a fatal Javascript exception.

Steps to Reproduce:
  • Initialize a pqGrid (v11.1.0) with groupModel enabled.
    Ensure the grid has no rows (dataModel.data: []).
    Click inside the empty grid body so it gains focus (the .pq-focus-mgr element).
    Type any alphanumeric key.

Expected Behavior:
The grid should quietly ignore the keystroke since there are no rows to navigate or jump to via Keyboard Navigation.

Actual Behavior:
The internal keyboard navigation ($.paramquery.cKeyNav.bodyKeyDown) intercepts the keystroke and attempts to jump to a row starting with the typed character. It triggers a selection event but fails to verify if the row actually exists before trying to read the pq_gtitle property of an undefined object, resulting in a crash.

Error Trace:
Uncaught TypeError: Cannot read properties of undefined (reading 'pq_gtitle')
    at $.<computed>.<computed>.<anonymous> (pqgrid.min.js?v=11.1.0:752:459)
    at handleListeners (pqgrid.min.js?v=11.1.0:39:329)
    at _pq_.trigger (pqgrid.min.js?v=11.1.0:41:19)
    at $.paramquery.cKeyNav.bodyKeyDown (pqgrid.min.js?v=11.1.0:335:462)
    at fn.onKeyDown (pqgrid.min.js?v=11.1.0:195:239)
    at HTMLDivElement.<anonymous> (pqgrid.min.js?v=11.1.0:195:403)
    at HTMLDivElement.dispatch (jquery-4.0.0.min.js:2:37917)
    at v.handle (jquery-4.0.0.min.js:2:35897)

Proposed Internal Fix (for pqGrid core):
In cKeyNav.bodyKeyDown or the internal row iteration logic for grouping, ensure rowData is defined before evaluating rowData.pq_gtitle or rowData.pq_gsummary.

Temporary Workaround / App-Level Fix:
For anyone else experiencing this in the meantime, you can bypass the crash by attaching a capture-phase keydown listener to the grid container to kill the event before cKeyNav can intercept it:

Code: [Select]
// Bind this immediately after initializing the grid
gridContainer[0].addEventListener("keydown", function(e) {
    var gridData = $(this).pqGrid("option", "dataModel.data");
   
    // Is the grid totally empty?
    if (!gridData || gridData.length === 0) {
        // Allow typing in external inputs/filters, but block empty grid body key navigation
        if (!$(e.target).is("input:not(:checkbox), textarea, select") || $(e.target).hasClass("pq-focus-mgr") || $(e.target).parent().hasClass("pq-focus-mgr")) {
            e.stopImmediatePropagation();
            e.stopPropagation();
        }
    }
}, true); // useCapture = true

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6552
    • View Profile
Thank you for reporting the issue.