Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
Multi-Column Sorting, (Ordering)
15/03/2012
02:28
Avatar
Rat
Member
Members
Forum Posts: 8
Member Since:
09/03/2012
sp_UserOfflineSmall Offline

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

19/03/2012
13:39
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

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.

19/03/2012
13:51
Avatar
Rat
Member
Members
Forum Posts: 8
Member Since:
09/03/2012
sp_UserOfflineSmall Offline

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)

19/03/2012
16:09
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

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.

08/06/2012
04:03
Avatar
chrismark
Member
Members
Forum Posts: 11
Member Since:
08/10/2011
sp_UserOfflineSmall Offline

is there a way to do this locally (datatype: 'local')?

09/04/2013
19:01
Avatar
ooo
Member
Members
Forum Posts: 5
Member Since:
13/02/2011
sp_UserOfflineSmall Offline

Tony,
Was this ever released into the regular product?  I don't see it anywhere.

18/07/2013
02:23
Avatar
jstagger
New Member
Members
Forum Posts: 1
Member Since:
18/07/2013
sp_UserOfflineSmall Offline

It was finally released in jqgrid 4.5.0.

There is a demo together with code here - http://goo.gl/IMRyV

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
22 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.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information