Andrus
Andrus

Reputation: 27909

How user can to toggle row numbers in jqgrid

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 ?

Upvotes: 0

Views: 3069

Answers (1)

Oleg
Oleg

Reputation: 221997

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.

Upvotes: 1

Related Questions