Andrea
Andrea

Reputation: 1057

jqgrid select rows by default based on column value

As an extension to my previous question, I would like to automatically select rows by default. In this response, they use loadComplete to select rows after server request. However, I request from the server once and use local data from then on. I need instead to reselect rows every time the columns are organized, the grid is searched...basically every time the view of the data changes.

I select rows based on a column (book_id) rather than an explicit rowid, so would the answer here be appropriate? Or does jqGrid have an explicit method (onUpdateGrid, e.g.) to help achieve this goal? It looks for now that I would just have to replicate code under both onPaging and onSortCol.

The dataInit method for the fav_books column:

initBookEdit: function(elem){
  //populate reference table
  populateBookRefs($(elem).val());

  //display dialog which contains reference table
  //pressing OK button on dialog saves all id's as a
  //comma delimited list in the main table
  $('#bookRefPopup').dialog({
     buttons: {
    "OK": function(){
        var selectedRows = bookRefTable.jqGrid('getGridParam', 'selarrrow');
        var selectedIds = new Array();
        for(var i=0; i<selectedRows.length; i++){
            var changedRow = bookRefTable.getRowData(selectedRows[i]);
            var book_id = changedRow['book_id'];
            selectedIds.push(book_id);
        }
        var editedRow = $('#mainTable').jqGrid('getGridParam', 'selrow');
        $('#mainTable').jqGrid('setCell',editedRow, 'docs_ref', selectedIds, null, null, true);
        $('#mainTable').trigger('reloadGrid');
        $(this).dialog( "close" );
    },
    Cancel: function() {
        $( this ).dialog( "close" );
    }
}//close buttons
  });//close dialog
}

And the initialization of the reference table:

function populateBookRefs(ids){
  values = ids.split(',');
  grid.jqGrid({
     ...
     loadComplete:  function(){ //event executed after server request
        for(var i=0; i<values.length; i++){
          grid.jqGrid('setSelection',values[i],true);
        }

    }
     ...
  });
}

Upvotes: 0

Views: 6679

Answers (1)

Andrea
Andrea

Reputation: 1057

it looks like gridComplete will do what i want:

gridComplete: function(){   
    var grid_ids=grid.jqGrid('getDataIDs');
    for(var i=0; i<grid_ids.length; i++){
        var rowid = grid_ids[i];
        var aRow = grid.jqGrid('getRowData',rowid);
        var book_id = aRow['book_id'];
        if($.inArray(book_id,values)!=-1){
            grid.jqGrid('setSelection',rowid,true);
        }
    }
}

Upvotes: 2

Related Questions