| 8 |
lars |
1 |
/**
|
|
|
2 |
* The DataTables core library provides the ability to set the ordering via the
|
|
|
3 |
* `dt-api column().order()` method, but there is no plural equivalent. While
|
|
|
4 |
* multi-column ordering can be set using `dt-api order()` that method requires
|
|
|
5 |
* that column indexes be used.
|
|
|
6 |
*
|
|
|
7 |
* This plug-in provides the plural `columns().order()` method so you can set
|
|
|
8 |
* multi-column ordering, while retaining the benefits of the `dt-api columns()`
|
|
|
9 |
* selector options.
|
|
|
10 |
*
|
|
|
11 |
* @name columns().order()
|
|
|
12 |
* @summary Apply multi-column ordering through the columns() API method.
|
|
|
13 |
* @author [Allan Jardine](http://sprymedia.co.uk)
|
|
|
14 |
* @requires DataTables 1.10+
|
|
|
15 |
* @param {string|array} dir The order to apply to the columns selected. This
|
|
|
16 |
* can be a string (`asc` or `desc`) which will be applied to all columns,
|
|
|
17 |
* or an array (again `asc` or `desc` as the elements in the array) which is
|
|
|
18 |
* the same length as the number of columns selected, and will be applied to
|
|
|
19 |
* the columns in sequence.
|
|
|
20 |
*
|
|
|
21 |
* @returns {DataTables.Api} DataTables API instance
|
|
|
22 |
*
|
|
|
23 |
* @example
|
|
|
24 |
* // Apply multi-column sorting with a common direction
|
|
|
25 |
* table.columns( [ 1, 2 ] ).order( 'desc' ).draw();
|
|
|
26 |
*
|
|
|
27 |
* @example
|
|
|
28 |
* // Multi-column sorting with individual direction for the columns
|
|
|
29 |
* table.columns( [ 1, 2 ] ).order( [ 'desc', 'asc' ] ).draw();
|
|
|
30 |
*
|
|
|
31 |
* @example
|
|
|
32 |
* // Multi-column sorting based on a name selector
|
|
|
33 |
* table.columns( [ 'sign_up_date:name', 'user_name:name' ] ).order( 'desc' ).draw();
|
|
|
34 |
*/
|
|
|
35 |
|
|
|
36 |
$.fn.dataTable.Api.register( 'columns().order()', function ( dir ) {
|
|
|
37 |
return this.iterator( 'columns', function ( settings, columns ) {
|
|
|
38 |
var a = [];
|
|
|
39 |
|
|
|
40 |
for ( var i=0, ien=columns.length ; i<ien ; i++ ) {
|
|
|
41 |
a.push( [ columns[i], $.isArray(dir) ? dir[i] : dir ] );
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
new $.fn.dataTable.Api( settings ).order( a );
|
|
|
45 |
} );
|
|
|
46 |
} );
|