Ali
Ali

Reputation: 151

Jqgrid treegrid scroll to a row by row ID and expand the node if collapsed

Scenario is a Jqgrid treegrid with all nodes loaded, some of them collapsed some of them expanded as a result of user interaction. Now there is a need to scroll to a specific row based on the row ID, and if the row is inside a collapsed node, then expand the node till row is visible to the user. Any hints?

Upvotes: 1

Views: 1767

Answers (1)

Oleg
Oleg

Reputation: 221997

To expand the nodes of the TreeGrid you can use expandRow. One should additionally make a loop and expand all parents of the row. One can use getNodeParent to get direct parent. Additionally one should use scrollrows: true option to scroll the grid to selected row.

The resulting demo allows to choose the row by rowid which need be selected. Clicking on "Select row by id" button do what you need:

enter image description here

Click event handle which I used in the demo you will see below

$("#selectId").button().click(function () {
    var idToSelect = $("#selectedId").val(), // id of the row which need be selected
        localRowData = $grid.jqGrid("getLocalRow", idToSelect);

    while (localRowData.parent !== null && localRowData.parent.toUpperCase() !== "NULL") {
        localRowData = $grid.jqGrid("getNodeParent", localRowData);
        $grid.jqGrid("expandRow", localRowData);
    }

    // we use scrollrows: true option so the selection below
    // will scroll the grid to the selected row additionally
    $grid.jqGrid("setSelection", idToSelect);
});

Upvotes: 1

Related Questions