jqgrid默认根据列值选择行

时间:2011-08-15 14:27:45

标签: jqgrid

作为previous question的扩展,我想默认自动选择行。在this response中,他们使用loadComplete在服务器请求后选择行。但是,我从服务器请求一次,然后使用本地数据。我需要在每次组织列时重新选择行,搜索网格...基本上每次数据视图都发生变化时。

我根据列(book_id)而不是显式rowid选择行,那么答案here是否合适?或者jqGrid有一个明确的方法(例如onUpdateGrid)来帮助实现这个目标吗?我现在需要在onPagingonSortCol下复制代码。

fav_books列的dataInit方法:

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
}

参考表的初始化:

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

    }
     ...
  });
}

1 个答案:

答案 0 :(得分:2)

看起来gridComplete会做我想做的事情:

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