Select a row in jqGrid based on cell value

13,792

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.

Share:
13,792
Sameet
Author by

Sameet

Here to learn

Updated on June 24, 2022

Comments

  • Sameet
    Sameet almost 2 years
    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.

    • Oleg
      Oleg about 11 years
      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?
    • Mark
      Mark about 11 years
      @Oleg Is there a way to get the current rowId from the rowattr method?
    • Sameet
      Sameet about 11 years
      @Oleg - using multiselect: true...
    • Oleg
      Oleg about 11 years
      I think that the answer could be interesting for you.
    • Oleg
      Oleg about 11 years
      @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 of code to add id as the third parameter of rowattr.
  • Oleg
    Oleg about 11 years
    One can't use current implementation of jqGrid to set selection inside of rowattr or by usage of cellattr etc. See the thread about the subject. Mark, you can improve the code from your answer if you would use $(this).jqGrid("getCol", "Id", true).
  • Mark
    Mark about 11 years
    Interesting... I always learn from your answers! I'll have to look to use that I need to iterate over grid rows!
  • Sameet
    Sameet about 11 years
    @Mark - So iterate over each row and select the ones that match the criteria...thanks for the direction
  • IcedDante
    IcedDante almost 9 years
    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