0

Using answer from Set rownumbers to false dynamically in jqgrid I created row numbers toggle button. Initially row numbers are not shown. Button click is ignored, row numbers column is not added. How to force button to add row number column ? Or is it possible to add option to column chooser to toggle row number columns ? This would be better, not additional button is required.

 var rownumbers=  isColState ? myColumnsState.rownumbers : false;
 $("#grid_toppager_left table.navtable tbody tr").append(
        '<td class="ui-pg-button ui-corner-all">' +
            '<div class="ui-pg-div my-nav-checkbox">' +
            '<input tabindex="-1" type="checkbox" id="RowNumbers" ' + (rownumbers ? 'checked ' : '')+'/>' +
            '<label title="Toggle row numbers"' +
            ' for="RowNumbers">Toggle row numbers</label></div></td>'
    );
 $("#RowNumbers").button({
        text: false,
        icons: {primary: "ui-icon-grip-dotted-vertical"}
    }).click(function () {
        rownumbers = !rownumbers;
        if (rownumbers ) {
          $grid.jqGrid('showCol', 'rn'); 
         } else { 
          $grid.jqGrid('hideCol', 'rn'); 
          }
       saveWindowState();
    });

Update

jqgrid contains also multiselect and _actions columns. in loadcomplete new row is added to end of grid using

var newRowData = { Dokumnr: 123,
    Reanr: $grid[0].rows.length + 1
},
newRowId = '_empty' + $.jgrid.randId();
$grid.jqGrid('addRowData', newRowId, newRowData);

If row numbers are turned on in first time after load, multiselect column checkbox in added row appears in rown numbers column:

checkbox in row number column

How to fix this ?

Community
  • 1
  • 1
Andrus
  • 26,339
  • 60
  • 204
  • 378

1 Answers1

1

You have to include rownumbers: true in any way to create the row numbers. If you want that row numbers are initially not shown you should call $grid.jqGrid('hideCol', 'rn'); after the grid is created. You can additionally set rownumbers to false with respect of $grid.jqGrid('setGridParam', {rownumbers: false});, but I don't think that it's really required.

After that you can use the button $("#RowNumbers") like you as wanted initially. Probably you can consider to set rownumbers option to true or false together with showCol and hideCol and use the rownumbers option instead of your rownumbers variable.

UPDATED: The current code of addRowData just test whether rownumbers option of jqGrid is true:

var ni = t.p.rownumbers===true ? 1 :0;
    gi = t.p.multiselect ===true ? 1 :0;
    si = t.p.subGrid===true ? 1 :0;

Depend on the value it calculate the position of the data:

prp = t.formatCol(ni,1,'', null, rowid, true);

So I should correct my answer. You should not change the value of rownumbers option. It should stay always true even if the corresponding column will be hidden.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you. Excellent suggestions. I implemented them but multiselect checkbox in added row appears in row numbers column if row numbers are toggled on at firts time. How to fix this ? I updated question – Andrus Mar 18 '12 at 09:22
  • @Andrus: It seems that to fix the problem you should just not change the `rownumbers` option of jqGrid. It should stay always `true`. – Oleg Mar 18 '12 at 09:51
  • thank you. How to find current state of row numbers for toggle and grid state save ? Should I check is rownumbers column in hidden state ? If yes, how to check this ? – Andrus Mar 18 '12 at 13:28
  • 1
    @Andrus: I find that checking of `hidden` property of 'rn' column in `colModel` the most direct and clear way. You can either use `getColumnIndexByName` to get the column index or get just the first column of `colModel`. – Oleg Mar 18 '12 at 13:42
  • Thank you. I used ` if ($grid.jqGrid('getGridParam', 'rownumbers') && $grid[0].p.colModel[0].hidden ) { $grid.jqGrid('showCol', 'rn'); } else { $grid.jqGrid('hideCol', 'rn'); } ` Is this best solution? – Andrus Mar 18 '12 at 17:00
  • @Andrus: It's difficult to answer on the question "Is this best...?" because one need to prove this. Moreover it's difficult to comment any code without the context where it is used. In any way I see that you correctly understand what I mean. You can use `var firstCol = $grid[0].p.colModel[0]; if (firstCol.name === 'rn' && firstCol.hidden)` to verify whether you have hidden rownumber column and you can show it with `$grid.jqGrid('showCol', 'rn');`. `if (firstCol.name === 'rn' && !firstCol.hidden)` then you have visible rownumber column and you can hide it with `$grid.jqGrid('hideCol', 'rn');` – Oleg Mar 18 '12 at 17:11