I have a jqGrid with multiselect.
I would like to be able to pre-check a certain number of rows if another column is true, for example.
What I’ve done so far is passing an element (column) from the server which contains a boolean.
I hide this column in the gridComplete event.
I can I select – and check a predefined row during the load of the grid?
UPDATE:
This is my code:
jQuery("#OrdersGrid").jqGrid({
url: $.SalesOrders.url.OrdersFetchUrl,
postData: { OrderStatus: orderStatus },
datatype: 'json',
mtype: 'POST',
colNames: ['N.Ordine', 'Cliente', 'Ragione Sociale', 'Stato', 'Fido', 'Data', ''],
colModel: [
{ name: 'Number', index: 'Number', editable: false, resizable: true, sortable: false, width: 76, align: 'left' },
{ name: 'CustomerCode', index: 'CustomerCode', editable: false, resizable: true, sortable: false, width: 50, align: 'left' },
{ name: 'CustomerName', index: 'CustomerName', editable: false, resizable: true, sortable: false, width: 410, align: 'left' },
{ name: 'Status', index: 'Status', editable: false, resizable: true, sortable: false, width: 40, align: 'center' },
{ name: 'LoCStatus', index: 'LoCStatus', editable: false, resizable: true, sortable: false, width: 40, align: 'center' },
{ name: 'Date', index: 'Date', editable: false, resizable: true, sortable: false, width: 70, align: 'right' },
{ name: 'Checked', index: 'Checked', editable: false, resizable: false, visible: false }
],
pager: $('#OrdersPager'),
rowNum: 30,
width: 794,
height: 220,
viewrecords: true,
shrinkToFit: false,
scroll: true,
rownumbers: true,
hidegrid: false,
multiselect: true,
emptyrecords: "Nessun record presente",
loadComplete: function (data) {
if (data.rows.length > 0) {
for (var i = 0; i < data.rows.length; i++) {
if (data.rows[i].cell[6] == 'true') {
jQuery("#OrdersGrid").jqGrid('setSelection', data.rows[i].id, true);
}
}
}
jQuery("#OrdersGrid").jqGrid('hideCol', 'Checked');
}
});
I’ve implemented Oleg’s solution and it works like a charm.
You don’t posted the definition of the grid which you use, so some questions for me stay opened. Nevertheless I hope that you will find the answer on your question here. In the demo included in the answer are used the parameter
loadonce:truewhich makes all more complex. The rows which will be selected are not on the first page of the loaded data. Moreover, in the demo there are no columns (even hidden) which can be used to determine which rows should be selected. Instead of that the information about the selection in the grid will be sent from the server as a part ofuserdatapart of the JSON response. This way is more general and I hope it will work also in your grid.UPDATED: I find your code good. I seen only small places for optimization:
editable: false,resizable: trueandalign: 'left'are default in thecolModel(see jqGrid documentation). So I suggest you to remove the values.visible: falsedon’t exist in thecolModel. You mean probablyhidden: true. After the usage of correct property the statementjQuery("#OrdersGrid").jqGrid('hideCol', 'Checked');will be not needed in theloadComplete.jQuery("#OrdersGrid")every time in the loop you can save the value in a variable:var mygrid = jQuery("#OrdersGrid");before jqGrid initialization and then your code will bemygrid.jqGrid({...,loadComplete: function (data) {...mygrid.jqGrid('setSelection', ...Some additional remarks. The statement
data.rows[i].cell[6] = 'true'shows that you serializeCheckedproperty withChecked.ToString(). You can useChecked?"1":"0"instead and reduce the size of the data transfered. I use many grids with theformatter:"checkbox"which interpret both “1” and “true” like checked. In your case you are absolutely free in the format of data, so you can use “1” and “0” or even “1” and “”.If you use Checked column only to transfer the information about row selection you can remove
Checkedcolumn from the grid at all. In thedataparameter ofloadCompleteyou will still see theCheckedproperty (thedata.rows[i].cell[6]), but you reduce the number of columns.