1
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.

Sameet
  • 2,191
  • 7
  • 28
  • 55
  • Do you use `multiselect: true` option or you want just highlight (with some color, background color or by adding special CSS class) the rows in the grid? – Oleg Feb 26 '13 at 17:39
  • @Oleg Is there a way to get the current rowId from the rowattr method? – Mark Feb 26 '13 at 18:10
  • @Oleg - using multiselect: true... – Sameet Feb 26 '13 at 18:20
  • 2
    I think that [the answer](http://stackoverflow.com/a/8172723/315935) could be interesting for you. – Oleg Feb 26 '13 at 19:29
  • @Mark: `rowattr` have no comfortable way to access to `id`. One have to get it from the second parameter (or from the first one in some cases). One can modify [the line](https://github.com/tonytomov/jqGrid/blob/v4.4.4/js/grid.base.js#L1109) of code to add `id` as the third parameter of `rowattr`. – Oleg Feb 26 '13 at 19:35

2 Answers2

9

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.

Mark
  • 3,123
  • 4
  • 20
  • 31
  • One can't use current implementation of jqGrid to set selection inside of `rowattr` or by usage of `cellattr` etc. See [the thread](http://www.trirand.com/blog/?page_id=393/help/seleting-the-checkbox-based-on-xml-value/) about the subject. Mark, you can improve the code from your answer if you would use `$(this).jqGrid("getCol", "Id", true)`. – Oleg Feb 26 '13 at 19:43
  • Interesting... I always learn from your answers! I'll have to look to use that I need to iterate over grid rows! – Mark Feb 26 '13 at 22:37
  • @Mark - So iterate over each row and select the ones that match the criteria...thanks for the direction – Sameet Feb 27 '13 at 05:51
  • 2
    Just to spare a lot of frustration among people who copy and paste code, I think you should locally declare/scope the variables in this snippet. `var i;` `var rowData;`, etc – IcedDante Jun 12 '15 at 21:41
0

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

H4dr1en
  • 277
  • 2
  • 11