1
0
mirror of https://github.com/DataTables/DataTables.git synced 2024-11-29 11:24:10 +01:00

New: cells() and cell() API methods now accept a cell selector and options

- For consistency and completeness, the cell() and cells() methods now
  accept their own cell selector option (rather than just rows and
  columns) which is a jQuery selector, or no selector at all for all
  cells. Additionally it allows an options object to be passed in, to
  allow configuraiton of the order of data etc.

- The cells() method now has these calling options:
  - cells() - all cells
  - cells( opts ) - option configuration
  - cells( selector ) - cell selector
  - cells( selector, opts ) - cell selector + options
  - cells( rowSelector, columnSelector ) - row + column selector
  - cells( rowSelector, columnSelector, opts ) - row + column selector +
    opttions

- The cell() method as the same signature.
This commit is contained in:
Allan Jardine 2013-05-23 19:07:30 +01:00
parent a30468a1f4
commit 8e583a51b3
2 changed files with 77 additions and 5 deletions

View File

@ -61,7 +61,7 @@ var _selector_run = function ( selector, select )
} }
for ( i=0, ien=selector.length ; i<ien ; i++ ) { for ( i=0, ien=selector.length ; i<ien ; i++ ) {
a = selector[i].split ? a = selector[i] && selector[i].split ?
selector[i].split(',') : selector[i].split(',') :
[ selector[i] ]; [ selector[i] ];
@ -337,3 +337,56 @@ var _column_selector = function ( settings, selector, opts )
} }
} ); } );
}; };
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Cells
*
* {node} - cell node
* "{string}" - jquery selector to run on the nodes
*
*/
var _cell_selector = function ( settings, selector, opts )
{
var data = settings.aoData;
var rows = _row_selector_indexes( settings, opts );
var cells = _pluck_order( data, rows, 'anCells' );
var allCells = $( [].concat.apply([], cells) );
var row;
var columns = settings.aoColumns.length;
var a, i, ien, j;
return _selector_run( selector, function ( s ) {
if ( ! s ) {
// All cells
a = [];
for ( i=0, ien=rows.length ; i<ien ; i++ ) {
row = rows[i];
for ( j=0 ; j<columns ; j++ ) {
a.push( {
row: row,
column: j
} );
}
}
return a;
}
// jQuery filtered cells
return allCells.filter( s ).map( function (i, el) {
row = el.parentNode._DT_RowIndex;
return {
row: row,
column: $.inArray( el, data[ row ].anCells )
};
} );
} );
};

View File

@ -6,6 +6,24 @@ var _api = DataTable.Api;
_api.register( 'cells()', function ( rowSelector, columnSelector, opts ) { _api.register( 'cells()', function ( rowSelector, columnSelector, opts ) {
// Argument shifting
if ( $.isPlainObject( rowSelector ) ) {
opts = rowSelector;
rowSelector = null;
}
if ( $.isPlainObject( columnSelector ) ) {
opts = columnSelector;
columnSelector = null;
}
// Cell selector
if ( columnSelector === null || columnSelector === undefined ) {
return this.iterator( 'table', function ( settings ) {
return _cell_selector( settings, rowSelector, _selector_opts( opts ) );
} );
}
// Row + column selector
var columns = this.columns( columnSelector, opts ); var columns = this.columns( columnSelector, opts );
var rows = this.rows( rowSelector, opts ); var rows = this.rows( rowSelector, opts );
var a, i, ien, j, jen; var a, i, ien, j, jen;
@ -74,17 +92,18 @@ _api.register( 'cell()', function ( rowSelector, columnSelector, opts ) {
_api.register( 'cell().data()', function ( data ) { _api.register( 'cell().data()', function ( data ) {
var ctx = this.context; var ctx = this.context;
var cell = this[0];
if ( data === undefined ) { if ( data === undefined ) {
// Get // Get
return ctx.length && this.length ? return ctx.length && cell.length ?
_fnGetCellData( ctx[0], this[0].row, this[0].column ) : _fnGetCellData( ctx[0], cell[0].row, cell[0].column ) :
undefined; undefined;
} }
// Set // Set
_fnSetCellData( ctx[0], this[0].row, this[0].column, data ); _fnSetCellData( ctx[0], cell[0].row, cell[0].column, data );
_fnInvalidateRow( ctx[0], this[0].row, 'data' ); _fnInvalidateRow( ctx[0], cell[0].row, 'data' );
return this; return this;
} ); } );