Search code examples
javascriptjqueryjqgrid

How to return jqgrid data of selected rows


In JQGrid

 var gridData=$("#SearchResults").jqGrid('getRowData')

The above line gives you the grid data of all the rows, is there a way where I can get the grid data of only the selected rows.

selRowId = myGrid.jqGrid ('getGridParam', 'selrow'),

The above gives the selected row IDs but I want the data as well of all the selected rows as it returns with gridData but I need only of those of selected one


Solution

  • It's very simple. The second optional option parameter of getRowData method is rowid of the row which data is requested (see the documentation). So you can use

    var selRowId = myGrid.jqGrid("getGridParam", "selrow");
    

    to get last selected rowid first and then get the data of the row by

    var rowData = myGrid.jqGrid("getRowData", selRowId);
    

    If you use datatype: "local" or some remote datatype ("xml" or "json"), but with loadonce: true then jqGrid hold the data internally in data array. In the case the usage of getLocalRow method is more effective as the usage of getRowData:

    var rowData = myGrid.jqGrid("getLocalRow", selRowId);
    

    If you use multiselect: true option then jqGrid supports selarrrow array of selected rowids and you can get all required data in the loop:

    var i, selRowIds = myGrid.jqGrid("getGridParam", "selarrrow"), n, rowData;
    for (i = 0, n = selRowIds.length; i < n; i++) {
        rowData = myGrid.jqGrid("getLocalRow", selRowIds[i]);
        // one can uses the data here
    }