Forum
02:28
09/03/2012
I've implemented my own multi-column sorting and decided to share the code below… enjoy.
p.s. I've also provided my code for column header separator double-click width autosizing in a separate post here.
(In both instances the code works, but could definitely be cleaned up)
// Somewhat messy code to implement the Multi-Sort… I'm sure this could be done using some kind of index and not resorting to Regular Expressions to manipulate strings
function multiSort(index, iCol, sortorder)
{
if (jQuery('#multisortcheckbox').is(':checked'))
{
var Column = jQuery('#grid')[0].p.colModel;
jQuery("#grid").setGridParam({sortname: colModel[0]['index'], sortorder: 'asc'});
if (multiSort.sortName == '')
{
multiSort.sortName = Column[iCol].index;
multiSort.sortOrder = sortorder;
}
else
{
var RegEx = new RegExp('s*' + Column[iCol].index + 's*', 'i');
if (RegEx.test(multiSort.sortName)) // Test if column already selected in Multi-Sort collection
{
RegEx = new RegExp('s*' + Column[iCol].index + ' ([ade]+sc)s*', 'i');
if (RegEx.test(multiSort.sortName)) // Test if NOT last field in sort list
{
var colSortOrder = RegEx.exec(multiSort.sortName);
colSortOrder = ((colSortOrder && colSortOrder[1] == 'asc') ? 'desc' : 'asc'); // Toggle Column Sort Order since this is not retained when moving from one column to the next in Multi-Sort mode
RegEx = new RegExp('(s*' + Column[iCol].index + ') [ade]+scs*', 'i');
multiSort.sortName = multiSort.sortName.replace(RegEx, '$1 ' + colSortOrder);
}
else multiSort.sortOrder = ((multiSort.sortOrder == 'asc') ? 'desc' : 'asc'); // If last field in sort list then change sort order (Toggle Column Sort Order since this is not retained when moving from one column to the next in Multi-Sort mode)
}
else
{
multiSort.sortName = multiSort.sortName + ' ' + multiSort.sortOrder + ', ' + Column[iCol].index;
multiSort.sortOrder = sortorder;
}
}
jQuery("#grid").setGridParam({sortname: multiSort.sortName, sortorder: multiSort.sortOrder});
}
}
multiSort.sortName = '';
multiSort.sortOrder = '';
// Toggle multi-sort mode
jQuery("#multisortcheckbox").change(function()
{
var Column = jQuery('#grid')[0].p.colModel;
if (!jQuery('#multisortcheckbox').is(':checked'))
{
jQuery("#grid").setGridParam({sortname: colModel[0]['index'], sortorder: 'asc'});
multiSort.sortName = '';
multiSort.sortOrder = '';
}
});
// Add a Multi-Sort checkbox after the Grid Refresh button in the Pager bar
jQuery('td#refresh_grid').after('<td id="multisortcell"><span id="multisort" title="Toggle Column Multi-Sorting"><label for="multisortcheckbox" style="padding: 0 3px 0 10px; cursor: pointer;">Multi-Sort</label><input id="multisortcheckbox" type="checkbox" class="ui-pg-input" style="vertical-align: middle; cursor: pointer;"/></span></td>'); // Add the "Multi-Sort" checkbox to the footer bar
// Construct the jqGrid component
jQuery("#grid").jqGrid
({
…
onSortCol: function(index, iCol, sortorder){multiSort(index, iCol, sortorder);}, // jqGrid onSortCol event handler
…
});
// Create the SQL sort statement in the PHP web service
$SortIndex = isset($_GET['sidx'])?$_GET['sidx']:(isset($_POST['sidx'])?$_POST['sidx']:'');
$SortOrder = isset($_GET['sord'])?$_GET['sord']:(isset($_POST['sord'])?$_POST['sord']:1);
$Order = '';
$Comma = '';
$SortIndices = explode(',', $SortIndex);
foreach ($SortIndices as $SortValue)
{
$SortValues = explode(' ', trim($SortValue));
foreach ($SortValues as $SortField)
{
if ($SortField == 'desc' || $SortField == 'asc') $Order .= ' ' . $SortField;
else $Order .= $Comma . '[' . $SortField . ']';
}
$Comma = ', ';
}
$Order .= ' ' . $SortOrder; // $Order contains the full SQL sort string
13:39
Moderators
30/10/2007
Hello,
Thanks a lot for the contribution.
This week I will look into the code and if it is OK and work fine I will include it into the source.
Best Regards
Tony
For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.
13:51
09/03/2012
Tony,
My approach was to toggle multi-sorting mode off and on via a check box in the pager bar... this implementation may not suit everybody.
Also there was no attempt to include a visual indicator in the column headers, (perhaps a sort index number or color-coded sort glyph etc).
Cheers,
Keith.
(A.K.A Rat)
16:09
Moderators
30/10/2007
Hello,
If everthing is ok this will be a grid option.
Regsrds
For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.
02:23
18/07/2013
It was finally released in jqgrid 4.5.0.
There is a demo together with code here - http://goo.gl/IMRyV
Most Users Ever Online: 715
Currently Online:
43 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
OlegK: 1255
markw65: 179
kobruleht: 144
phicarre: 132
YamilBracho: 124
Renso: 118
Member Stats:
Guest Posters: 447
Members: 11373
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 8
Topics: 10592
Posts: 31289
Newest Members:
, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66