0

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.

LeftyX
  • 35,328
  • 21
  • 132
  • 193

1 Answers1

2

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:true which 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 of userdata part 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:

  1. The properties editable: false, resizable: true and align: 'left' are default in the colModel (see jqGrid documentation). So I suggest you to remove the values.
  2. The property visible: false don't exist in the colModel. You mean probably hidden: true. After the usage of correct property the statement jQuery("#OrdersGrid").jqGrid('hideCol', 'Checked'); will be not needed in the loadComplete.
  3. Instead of the usage 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 be mygrid.jqGrid({...,loadComplete: function (data) {...mygrid.jqGrid('setSelection', ...

Some additional remarks. The statement data.rows[i].cell[6] = 'true' shows that you serialize Checked property with Checked.ToString(). You can use Checked?"1":"0" instead and reduce the size of the data transfered. I use many grids with the formatter:"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 Checked column from the grid at all. In the data parameter of loadComplete you will still see the Checked property (the data.rows[i].cell[6]), but you reduce the number of columns.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I've attached some code. As you can see my MVC controller streams a column Checked which is my discriminator.I've used the example in one of the links you've sent me and it works fine. What do you think about this solution? – LeftyX Mar 07 '11 at 17:06
  • @LeftyX: I updated my answer with some small suggestion to your code. – Oleg Mar 07 '11 at 17:45
  • very helpful.I tend not to optimize javascript but I know I should, as I do with the server side. – LeftyX Mar 08 '11 at 08:57