1
0
mirror of https://github.com/DataTables/DataTables.git synced 2025-03-21 13:29:04 +01:00

New: Ordering functions for the new API:

- order() and order.listener() added (to replace fnSort and
  fnSortListener) from the old API.

- Note that the name `order` is selected to not conflict with the `sort`
  method of the API object, which can be used to order the sort data
  held in the collection.

- The `sort()` method is expanded over the abilities of fnSort to allow
  multiple different forms of input (column + direction, 1D array, list
  of 1D arrays or a 2D array).
This commit is contained in:
Allan Jardine 2013-04-20 12:00:16 +01:00
parent aa76a6baf5
commit 2e278b0285
3 changed files with 171 additions and 2 deletions

View File

@ -1604,9 +1604,7 @@
settings.aiDisplay = settings.aiDisplayMaster.slice();
}
console.log( '_fnReDraw', holdPosition );
if ( holdPosition !== true ) {
console.log( 'resetting paging' );
settings._iDisplayStart = 0;
}
@ -7594,6 +7592,91 @@
} );
}());
(/** @lends <global> */function() {
var _Api = DataTable.Api;
/**
* Get current ordering (sorting) that has been applied to the table.
*
* @returns {array} 2D array containing the sorting information for the first
* table in the current context. Each element in the parent array represents
* a column being sorted upon (i.e. multi-sorting with two columns would have
* 2 inner arrays). The inner arrays may have 2 or 3 elements. The first is
* the column index that the sorting condition applies to, the second is the
* direction of the sort (`desc` or `asc`) and, optionally, the third is the
* index of the sorting order from the `column.sorting` initialisation array.
*//**
* Set the ordering for the table.
*
* @param {integer} order Column index to sort upon.
* @param {string} direction Direction of the sort to be applied (`asc` or `desc`)
* @returns {DataTables.Api} this
*//**
* Set the ordering for the table.
*
* @param {array} order 1D array of sorting information to be applied.
* @param {array} [...] Optional additional sorting conditions
* @returns {DataTables.Api} this
*//**
* Set the ordering for the table.
*
* @param {array} order 2D array of sorting information to be applied.
* @returns {DataTables.Api} this
*/
_Api.register( 'order()', function ( order, dir ) {
var ctx = this.context;
if ( order === undefined ) {
// get
return ctx.length !== 0 ?
ctx[0].aaSorting :
undefined;
}
// set
if ( typeof order === 'number' ) {
// Simple column / direction passed in
order = [ [ order, dir ] ];
}
else if ( arguments.length > 1 ) {
// Arguments passed in (list of 1D arrays)
order = Array.prototype.slice.call( arguments );
}
else if ( ! $.isArray( order[0] ) ) {
// Just a 1D array passed in, convert to 2D to use for sorting
order = [ order ];
}
// otherwise a 2D array was passed in
return this.tables( function ( settings ) {
settings.aaSorting = order;
} );
} );
/**
* Attach a sort listener to an element for a given column
*
* @param {node|jQuery|string} node Identifier for the element(s) to attach the
* listener to. This can take the form of a single DOM node, a jQuery
* collection of nodes or a jQuery selector which will identify the node(s).
* @param {integer} column the column that a click on this node will sort on
* @param {function} [callback] callback function when sort is run
* @returns {DataTables.Api} this
*/
_Api.register( 'order.listener()', function ( node, column, callback ) {
return this.tables( function ( settings ) {
_fnSortAttachListener( settings, node, column, callback );
} );
} );
}());
/**

View File

@ -113,6 +113,7 @@
require('api.draw.js');
require('api.page.js');
require('api.ajax.js');
require('api.order.js');
require('api.static.js');
/**

View File

@ -0,0 +1,85 @@
(/** @lends <global> */function() {
var _Api = DataTable.Api;
/**
* Get current ordering (sorting) that has been applied to the table.
*
* @returns {array} 2D array containing the sorting information for the first
* table in the current context. Each element in the parent array represents
* a column being sorted upon (i.e. multi-sorting with two columns would have
* 2 inner arrays). The inner arrays may have 2 or 3 elements. The first is
* the column index that the sorting condition applies to, the second is the
* direction of the sort (`desc` or `asc`) and, optionally, the third is the
* index of the sorting order from the `column.sorting` initialisation array.
*//**
* Set the ordering for the table.
*
* @param {integer} order Column index to sort upon.
* @param {string} direction Direction of the sort to be applied (`asc` or `desc`)
* @returns {DataTables.Api} this
*//**
* Set the ordering for the table.
*
* @param {array} order 1D array of sorting information to be applied.
* @param {array} [...] Optional additional sorting conditions
* @returns {DataTables.Api} this
*//**
* Set the ordering for the table.
*
* @param {array} order 2D array of sorting information to be applied.
* @returns {DataTables.Api} this
*/
_Api.register( 'order()', function ( order, dir ) {
var ctx = this.context;
if ( order === undefined ) {
// get
return ctx.length !== 0 ?
ctx[0].aaSorting :
undefined;
}
// set
if ( typeof order === 'number' ) {
// Simple column / direction passed in
order = [ [ order, dir ] ];
}
else if ( arguments.length > 1 ) {
// Arguments passed in (list of 1D arrays)
order = Array.prototype.slice.call( arguments );
}
else if ( ! $.isArray( order[0] ) ) {
// Just a 1D array passed in, convert to 2D to use for sorting
order = [ order ];
}
// otherwise a 2D array was passed in
return this.tables( function ( settings ) {
settings.aaSorting = order;
} );
} );
/**
* Attach a sort listener to an element for a given column
*
* @param {node|jQuery|string} node Identifier for the element(s) to attach the
* listener to. This can take the form of a single DOM node, a jQuery
* collection of nodes or a jQuery selector which will identify the node(s).
* @param {integer} column the column that a click on this node will sort on
* @param {function} [callback] callback function when sort is run
* @returns {DataTables.Api} this
*/
_Api.register( 'order.listener()', function ( node, column, callback ) {
return this.tables( function ( settings ) {
_fnSortAttachListener( settings, node, column, callback );
} );
} );
}());