0

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);
        }

    }
     ...
  });
}
Community
  • 1
  • 1
Andrea
  • 1,057
  • 1
  • 20
  • 49

1 Answers1

2

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);
        }
    }
}
Andrea
  • 1,057
  • 1
  • 20
  • 49
  • 3
    The enumeration of all rows like you do (with respect of `getDataIDs`) will work very slow in case of many rows. Look at [the another](http://stackoverflow.com/questions/5664587/jqgrid-load-large-data-set-without-pagination/5690583#5690583). There show much more effective way of enumeration. You should just change the setting of `ui-state-disabled` class to call of `setSelection``which you need. – Oleg Aug 15 '11 at 23:40
  • Thanks for taking the time to catch a potential problem. Much appreciated! – Andrea Aug 16 '11 at 12:18
  • wait, now i have the problem that gridComplete never finishes, because _index is the object {undefined:99}, so I can never get rowData and start selecting things. could this be a local data problem? – Andrea Aug 16 '11 at 14:51
  • If you include the test data which you use and include more full JavaScript code I could try to help you. To be able to do this I should have enough data to reproduce your problem. – Oleg Aug 16 '11 at 14:59
  • are there any hosts for jqgrid so i can create a fiddle? – Andrea Aug 16 '11 at 15:16