1
0
mirror of https://github.com/DataTables/DataTables.git synced 2025-04-09 01:53:56 +02:00
DataTables/media/src/api/api.cells.js
Allan Jardine f5286dc130 Internal: Create registerPlural method to provide an easy way of working
with plurality

- There are four groupings of plural / singular methods in the new API:
  table(s), row(s), column(s) and cell(s). We need to provide similar
  methods for each, often leading to code duplication. To help reduce
  this code duplication I've created a new `registerPlural` method which
  allows both the singular and plural to be derived from a single
  function automatically. The plural form is given and wrapped up by a
  container function which extracts a singular return.

- Note that not all singular methods can use this approach, for example
  row().data() provides extra functionality over rows().data() in that
  it can be used as a setter as well as getter.
2013-05-20 08:37:19 +01:00

96 lines
1.9 KiB
JavaScript

(/** @lends <global> */function() {
var _api = DataTable.Api;
_api.register( 'cells()', function ( rowSelector, columnSelector, opts ) {
var columns = this.columns( columnSelector, opts );
var rows = this.rows( rowSelector, opts );
var a, i, ien, j, jen;
var cells = this.iterator( 'table', function ( settings, idx ) {
a = [];
for ( i=0, ien=rows[idx].length ; i<ien ; i++ ) {
for ( j=0, jen=columns[idx].length ; j<jen ; j++ ) {
a.push( {
row: rows[idx][i],
column: columns[idx][j]
} );
}
}
return a;
} );
$.extend( cells.selector, {
cols: columnSelector,
rows: rowSelector,
opts: opts
} );
return cells;
} );
_api.registerPlural( 'cells().nodes()', 'cell().nodes()', function () {
return this.iterator( 'cell', function ( settings, row, column ) {
return settings.aoData[ row ].anCells[ column ];
} );
} );
_api.register( 'cells().data()', function () {
return this.iterator( 'cell', function ( settings, row, column ) {
return _fnGetCellData( settings, row, column );
} );
} );
_api.register( [
'cells().invalidate()',
'cell().invalidate()'
], function ( src ) {
var selector = this.selector;
// Use the rows method of the instance to perform the invalidation, rather
// than doing it here. This avoids needing to handle duplicate rows from
// the cells.
this.rows( selector.rows, selector.opts ).invalidate( src );
return this;
} );
_api.register( 'cell()', function ( rowSelector, columnSelector, opts ) {
return _selector_first( this.cells( rowSelector, columnSelector, opts ) );
} );
_api.register( 'cell().data()', function ( data ) {
var ctx = this.context;
if ( data === undefined ) {
// Get
return ctx.length && this.length ?
_fnGetCellData( ctx[0], this[0].row, this[0].column ) :
undefined;
}
// Set
_fnSetCellData( ctx[0], this[0].row, this[0].column, data );
_fnInvalidateRow( ctx[0], this[0].row, 'data' );
return this;
} );
}());