Jqgrid sort column
Asked Answered
O

3

6

Is it possible to dynamically sort one column of a jqGrid when clicking a button instead of clicking in the column name?

Oversee answered 1/3, 2012 at 12:44 Comment(1)
why not ?? there must be a tweak for thatWife
S
9

In the button click event set the sort column in the grids postdata and then call a reload on the grid

$('#mybutton').click(function() {
    $('#yourgrid').jqGrid('setGridParam', {sortname: 'yourColumn', sortorder: 'asc'}).trigger('reloadGrid', [{page: 1}]);
});
Steep answered 1/3, 2012 at 19:59 Comment(1)
The value of sidx parameter will be constructed from sortname option of jqGrid and the value of sord from sortorder. So you should do the following: $('#yourgrid').jqGrid('setGridParam', {sortname: 'yourColumn', sortorder: 'asc'}).trigger('reloadGrid', [{page: 1}]);. Moreover you should close the quote in $('#mybutton).Androsterone
R
8

The fourth time even luckier!. Using true in the third parameter will reload the grid for sure.

$('#grid').jqGrid('sortGrid', 'id', true, 'asc');

If you don't use true in the third parameter, in the first execution, the order ('asc' or 'desc') is not updated correctly.

Rife answered 23/8, 2012 at 5:16 Comment(0)
M
3

A possible solution - but not pretty:

$('#grid').jqGrid('setGridParam', {sortname: 'id', sortorder: 'asc'}).trigger('reloadGrid', [{page: 1}]);
$('#gbox_grid .s-ico').css('display','none');
$('#gbox_grid #jqgh_grid_id .s-ico').css('display','');
$('#gbox_grid #jqgh_grid_id .s-ico .ui-icon-triangle-1-s').removeClass('ui-state-disabled');

as shown here

http://jsfiddle.net/qhYLT/


Another way to sort by a column programatically - specifying the order:

$('#grid').jqGrid('setGridParam', {sortorder: 'desc'});
$('#grid').jqGrid('sortGrid', 'id');

The sortGrid fires off a reload for you. It wouldn't be complete without a demo : http://jsfiddle.net/uTqD5/


Third time lucky! An undocumented feature:

$('#grid').jqGrid('sortGrid', 'id', '', 'asc');
Maggio answered 29/6, 2012 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.