2 * Change the order of the table
3 * @param {object} oSettings dataTables settings object
4 * @param {bool} bApplyClasses optional - should we apply classes or not
5 * @memberof DataTable#oApi
7 function _fnSort ( oSettings, bApplyClasses )
10 i, iLen, j, jLen, k, kLen,
14 oSort = DataTable.ext.oSort,
15 aoData = oSettings.aoData,
16 aoColumns = oSettings.aoColumns,
17 oAria = oSettings.oLanguage.oAria;
19 /* No sorting required if server-side or no sorting array */
20 if ( !oSettings.oFeatures.bServerSide &&
21 (oSettings.aaSorting.length !== 0 || oSettings.aaSortingFixed !== null) )
23 aaSort = ( oSettings.aaSortingFixed !== null ) ?
24 oSettings.aaSortingFixed.concat( oSettings.aaSorting ) :
25 oSettings.aaSorting.slice();
27 /* If there is a sorting data type, and a function belonging to it, then we need to
28 * get the data from the developer's function and apply it for this column
30 for ( i=0 ; i<aaSort.length ; i++ )
32 var iColumn = aaSort[i][0];
33 var iVisColumn = _fnColumnIndexToVisible( oSettings, iColumn );
34 sDataType = oSettings.aoColumns[ iColumn ].sSortDataType;
35 if ( DataTable.ext.afnSortData[sDataType] )
37 var aData = DataTable.ext.afnSortData[sDataType].call(
38 oSettings.oInstance, oSettings, iColumn, iVisColumn
40 if ( aData.length === aoData.length )
42 for ( j=0, jLen=aoData.length ; j<jLen ; j++ )
44 _fnSetCellData( oSettings, j, iColumn, aData[j] );
49 _fnLog( oSettings, 0, "Returned data sort array (col "+iColumn+") is the wrong length" );
54 /* Create a value - key array of the current row positions such that we can use their
55 * current position during the sort, if values match, in order to perform stable sorting
57 for ( i=0, iLen=oSettings.aiDisplayMaster.length ; i<iLen ; i++ )
59 aiOrig[ oSettings.aiDisplayMaster[i] ] = i;
62 /* Build an internal data array which is specific to the sort, so we can get and prep
63 * the data to be sorted only once, rather than needing to do it every time the sorting
64 * function runs. This make the sorting function a very simple comparison
66 var iSortLen = aaSort.length;
67 var fnSortFormat, aDataSort;
68 for ( i=0, iLen=aoData.length ; i<iLen ; i++ )
70 for ( j=0 ; j<iSortLen ; j++ )
72 aDataSort = aoColumns[ aaSort[j][0] ].aDataSort;
74 for ( k=0, kLen=aDataSort.length ; k<kLen ; k++ )
76 sDataType = aoColumns[ aDataSort[k] ].sType;
77 fnSortFormat = oSort[ (sDataType ? sDataType : 'string')+"-pre" ];
79 aoData[i]._aSortData[ aDataSort[k] ] = fnSortFormat ?
80 fnSortFormat( _fnGetCellData( oSettings, i, aDataSort[k], 'sort' ) ) :
81 _fnGetCellData( oSettings, i, aDataSort[k], 'sort' );
86 /* Do the sort - here we want multi-column sorting based on a given data source (column)
87 * and sorting function (from oSort) in a certain direction. It's reasonably complex to
88 * follow on it's own, but this is what we want (example two column sorting):
89 * fnLocalSorting = function(a,b){
91 * iTest = oSort['string-asc']('data11', 'data12');
94 * iTest = oSort['numeric-desc']('data21', 'data22');
97 * return oSort['numeric-asc']( aiOrig[a], aiOrig[b] );
99 * Basically we have a test for each sorting column, if the data in that column is equal,
100 * test the next column. If all columns match, then we use a numeric sort on the row
101 * positions in the original data array to provide a stable sort.
103 oSettings.aiDisplayMaster.sort( function ( a, b ) {
104 var k, l, lLen, iTest, aDataSort, sDataType;
105 for ( k=0 ; k<iSortLen ; k++ )
107 aDataSort = aoColumns[ aaSort[k][0] ].aDataSort;
109 for ( l=0, lLen=aDataSort.length ; l<lLen ; l++ )
111 sDataType = aoColumns[ aDataSort[l] ].sType;
113 iTest = oSort[ (sDataType ? sDataType : 'string')+"-"+aaSort[k][1] ](
114 aoData[a]._aSortData[ aDataSort[l] ],
115 aoData[b]._aSortData[ aDataSort[l] ]
125 return oSort['numeric-asc']( aiOrig[a], aiOrig[b] );
129 /* Alter the sorting classes to take account of the changes */
130 if ( (bApplyClasses === undefined || bApplyClasses) && !oSettings.oFeatures.bDeferRender )
132 _fnSortingClasses( oSettings );
135 for ( i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
137 var sTitle = aoColumns[i].sTitle.replace( /<.*?>/g, "" );
138 nTh = aoColumns[i].nTh;
139 nTh.removeAttribute('aria-sort');
140 nTh.removeAttribute('aria-label');
142 /* In ARIA only the first sorting column can be marked as sorting - no multi-sort option */
143 if ( aoColumns[i].bSortable )
145 if ( aaSort.length > 0 && aaSort[0][0] == i )
147 nTh.setAttribute('aria-sort', aaSort[0][1]=="asc" ? "ascending" : "descending" );
149 var nextSort = (aoColumns[i].asSorting[ aaSort[0][2]+1 ]) ?
150 aoColumns[i].asSorting[ aaSort[0][2]+1 ] : aoColumns[i].asSorting[0];
151 nTh.setAttribute('aria-label', sTitle+
152 (nextSort=="asc" ? oAria.sSortAscending : oAria.sSortDescending) );
156 nTh.setAttribute('aria-label', sTitle+
157 (aoColumns[i].asSorting[0]=="asc" ? oAria.sSortAscending : oAria.sSortDescending) );
162 nTh.setAttribute('aria-label', sTitle);
166 /* Tell the draw function that we have sorted the data */
167 oSettings.bSorted = true;
168 $(oSettings.oInstance).trigger('sort', oSettings);
170 /* Copy the master data into the draw array and re-draw */
171 if ( oSettings.oFeatures.bFilter )
173 /* _fnFilter() will redraw the table for us */
174 _fnFilterComplete( oSettings, oSettings.oPreviousSearch, 1 );
178 oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
179 oSettings._iDisplayStart = 0; /* reset display back to page 0 */
180 _fnCalculateEnd( oSettings );
181 _fnDraw( oSettings );
187 * Attach a sort handler (click) to a node
188 * @param {object} oSettings dataTables settings object
189 * @param {node} nNode node to attach the handler to
190 * @param {int} iDataIndex column sorting index
191 * @param {function} [fnCallback] callback function
192 * @memberof DataTable#oApi
194 function _fnSortAttachListener ( oSettings, nNode, iDataIndex, fnCallback )
196 _fnBindAction( nNode, {}, function (e) {
197 /* If the column is not sortable - don't to anything */
198 if ( oSettings.aoColumns[iDataIndex].bSortable === false )
204 * This is a little bit odd I admit... I declare a temporary function inside the scope of
205 * _fnBuildHead and the click handler in order that the code presented here can be used
206 * twice - once for when bProcessing is enabled, and another time for when it is
207 * disabled, as we need to perform slightly different actions.
208 * Basically the issue here is that the Javascript engine in modern browsers don't
209 * appear to allow the rendering engine to update the display while it is still executing
210 * it's thread (well - it does but only after long intervals). This means that the
211 * 'processing' display doesn't appear for a table sort. To break the js thread up a bit
212 * I force an execution break by using setTimeout - but this breaks the expected
213 * thread continuation for the end-developer's point of view (their code would execute
214 * too early), so we only do it when we absolutely have to.
216 var fnInnerSorting = function () {
217 var iColumn, iNextSort;
219 /* If the shift key is pressed then we are multiple column sorting */
222 /* Are we already doing some kind of sort on this column? */
224 for ( var i=0 ; i<oSettings.aaSorting.length ; i++ )
226 if ( oSettings.aaSorting[i][0] == iDataIndex )
229 iColumn = oSettings.aaSorting[i][0];
230 iNextSort = oSettings.aaSorting[i][2]+1;
232 if ( !oSettings.aoColumns[iColumn].asSorting[iNextSort] )
234 /* Reached the end of the sorting options, remove from multi-col sort */
235 oSettings.aaSorting.splice( i, 1 );
239 /* Move onto next sorting direction */
240 oSettings.aaSorting[i][1] = oSettings.aoColumns[iColumn].asSorting[iNextSort];
241 oSettings.aaSorting[i][2] = iNextSort;
247 /* No sort yet - add it in */
248 if ( bFound === false )
250 oSettings.aaSorting.push( [ iDataIndex,
251 oSettings.aoColumns[iDataIndex].asSorting[0], 0 ] );
256 /* If no shift key then single column sort */
257 if ( oSettings.aaSorting.length == 1 && oSettings.aaSorting[0][0] == iDataIndex )
259 iColumn = oSettings.aaSorting[0][0];
260 iNextSort = oSettings.aaSorting[0][2]+1;
261 if ( !oSettings.aoColumns[iColumn].asSorting[iNextSort] )
265 oSettings.aaSorting[0][1] = oSettings.aoColumns[iColumn].asSorting[iNextSort];
266 oSettings.aaSorting[0][2] = iNextSort;
270 oSettings.aaSorting.splice( 0, oSettings.aaSorting.length );
271 oSettings.aaSorting.push( [ iDataIndex,
272 oSettings.aoColumns[iDataIndex].asSorting[0], 0 ] );
277 _fnSort( oSettings );
278 }; /* /fnInnerSorting */
280 if ( !oSettings.oFeatures.bProcessing )
286 _fnProcessingDisplay( oSettings, true );
287 setTimeout( function() {
289 if ( !oSettings.oFeatures.bServerSide )
291 _fnProcessingDisplay( oSettings, false );
296 /* Call the user specified callback function - used for async user interaction */
297 if ( typeof fnCallback == 'function' )
299 fnCallback( oSettings );
306 * Set the sorting classes on the header, Note: it is safe to call this function
307 * when bSort and bSortClasses are false
308 * @param {object} oSettings dataTables settings object
309 * @memberof DataTable#oApi
311 function _fnSortingClasses( oSettings )
313 var i, iLen, j, jLen, iFound;
315 var iColumns = oSettings.aoColumns.length;
316 var oClasses = oSettings.oClasses;
318 for ( i=0 ; i<iColumns ; i++ )
320 if ( oSettings.aoColumns[i].bSortable )
322 $(oSettings.aoColumns[i].nTh).removeClass( oClasses.sSortAsc +" "+ oClasses.sSortDesc +
323 " "+ oSettings.aoColumns[i].sSortingClass );
327 if ( oSettings.aaSortingFixed !== null )
329 aaSort = oSettings.aaSortingFixed.concat( oSettings.aaSorting );
333 aaSort = oSettings.aaSorting.slice();
336 /* Apply the required classes to the header */
337 for ( i=0 ; i<oSettings.aoColumns.length ; i++ )
339 if ( oSettings.aoColumns[i].bSortable )
341 sClass = oSettings.aoColumns[i].sSortingClass;
343 for ( j=0 ; j<aaSort.length ; j++ )
345 if ( aaSort[j][0] == i )
347 sClass = ( aaSort[j][1] == "asc" ) ?
348 oClasses.sSortAsc : oClasses.sSortDesc;
353 $(oSettings.aoColumns[i].nTh).addClass( sClass );
355 if ( oSettings.bJUI )
357 /* jQuery UI uses extra markup */
358 var jqSpan = $("span."+oClasses.sSortIcon, oSettings.aoColumns[i].nTh);
359 jqSpan.removeClass(oClasses.sSortJUIAsc +" "+ oClasses.sSortJUIDesc +" "+
360 oClasses.sSortJUI +" "+ oClasses.sSortJUIAscAllowed +" "+ oClasses.sSortJUIDescAllowed );
365 sSpanClass = oSettings.aoColumns[i].sSortingClassJUI;
367 else if ( aaSort[iFound][1] == "asc" )
369 sSpanClass = oClasses.sSortJUIAsc;
373 sSpanClass = oClasses.sSortJUIDesc;
376 jqSpan.addClass( sSpanClass );
381 /* No sorting on this column, so add the base class. This will have been assigned by
384 $(oSettings.aoColumns[i].nTh).addClass( oSettings.aoColumns[i].sSortingClass );
389 * Apply the required classes to the table body
390 * Note that this is given as a feature switch since it can significantly slow down a sort
391 * on large data sets (adding and removing of classes is always slow at the best of times..)
392 * Further to this, note that this code is admittedly fairly ugly. It could be made a lot
393 * simpler using jQuery selectors and add/removeClass, but that is significantly slower
394 * (on the order of 5 times slower) - hence the direct DOM manipulation here.
395 * Note that for deferred drawing we do use jQuery - the reason being that taking the first
396 * row found to see if the whole column needs processed can miss classes since the first
397 * column might be new.
399 sClass = oClasses.sSortColumn;
401 if ( oSettings.oFeatures.bSort && oSettings.oFeatures.bSortClasses )
403 var nTds = _fnGetTdNodes( oSettings );
405 /* Determine what the sorting class for each column should be */
406 var iClass, iTargetCol;
408 for (i = 0; i < iColumns; i++)
412 for (i = 0, iClass = 1; i < aaSort.length; i++)
414 iTargetCol = parseInt( aaSort[i][0], 10 );
415 asClasses[iTargetCol] = sClass + iClass;
423 /* Make changes to the classes for each cell as needed */
424 var reClass = new RegExp(sClass + "[123]");
425 var sTmpClass, sCurrentClass, sNewClass;
426 for ( i=0, iLen=nTds.length; i<iLen; i++ )
428 /* Determine which column we're looking at */
429 iTargetCol = i % iColumns;
431 /* What is the full list of classes now */
432 sCurrentClass = nTds[i].className;
433 /* What sorting class should be applied? */
434 sNewClass = asClasses[iTargetCol];
435 /* What would the new full list be if we did a replacement? */
436 sTmpClass = sCurrentClass.replace(reClass, sNewClass);
438 if ( sTmpClass != sCurrentClass )
440 /* We changed something */
441 nTds[i].className = $.trim( sTmpClass );
443 else if ( sNewClass.length > 0 && sCurrentClass.indexOf(sNewClass) == -1 )
445 /* We need to add a class */
446 nTds[i].className = sCurrentClass + " " + sNewClass;