Sameet
Sameet

Reputation: 2211

Select a row in jqGrid based on cell value

colModel: [
            { name: 'Id', index: 'Id', hidden: true, search: false },
            { name: 'Name', index: 'Name', hidden: true, search: false },
          ]

Just as the setSelection method allows selection of a row in jqGrid based on the row number, is it possible to similarly select a row based on one of the cell values.

For e.g. in the colModel above, is it possible to select a row having a certain 'Id' or 'Name' value...assuming that these values are unique for each row.

Upvotes: 1

Views: 15918

Answers (3)

SerF 78
SerF 78

Reputation: 1

I found bug in Mark's answer =)

It works only if rowIds = [0,1,2,...]

My version is

loadComplete: function () {
   let rowIds = $(this).jqGrid('getDataIDs')
     i = 0,
     rd = [];

   for (i; i <= rowIds.length; i++) {
     rd = $(this).jqGrid('getRowData', rowIds[i]);

     if (rowData['Id'] === idSearchValue ) {
       $(this).jqGrid('setSelection',rowIds[i]); 
     } //if
   } //for
   ...
}

Upvotes: 0

H4dr1en
H4dr1en

Reputation: 287

If you have an array of objects containing cells values, you will need another approach.

Eg. with your colModel, you would like to retrieve rows with these values:

let toSearch = [ 
        { Name: 'Arthur', Id: 150},
        { Name: 'Julien', Id: 90},            
]

Then what you can do is to retrieve the whole data from the jqGrid table and seek after values dynamically:

let data = $grid.jqGrid('getGridParam', 'data');
let toSelect = data.filter((row,i) => {
    return toSearch.some(toSelectRow => {
        for(let prop in prevRow) {
            if(prevRow[prop] != row[prop])
                return false;
        }
        return true;
    })
});

toSelect.forEach((row) => $grid.jqGrid('setSelection',row.id, false)); // Prevent firing onSelectRow event

Here we pass false to prevent from firing onSelectRow event, if you need it just remove false

Upvotes: 0

Mark
Mark

Reputation: 3123

In the loadComplete: portion of your jqGrid you could iterate over each row and test for the value you are looking for. If the value is found, select the row.

Ex

loadComplete: function () {
    var rowIds = $(this).jqGrid('getDataIDs');

    for (i = 1; i <= rowIds.length; i++) {
        rowData = $(this).jqGrid('getRowData', i);

        if (rowData['Id'] == idSearchValue ) {
           $(this).jqGrid('setSelection',i); 
        } //if

    } //for
...

There would also be the rowattr: but I can't seem to find where you can get the rowID of the current row. Oleg might see this and respond as well as it was his addition to jqGrid but I didn't have any luck with my testing or read though of where you would get the current rowId to pass to the setSelection method.

Upvotes: 9

Related Questions