From 623b24329ac77d6ace980e9a35e7e1dc86d64b06 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Sat, 18 May 2013 18:56:04 +0100 Subject: [PATCH] New: New cell based API methods - New API methods: - cells( rowSelector, columnSelector, opts ) - Cell selector - cells( ... ).nodes() - Get the nodes for the selected cells - cells( ... ).data() - Get the data for the selected cells - cell( rowSelector, columnSelector, opts ) - Single cell selector - cell( ... ).node() - Get the cell node - cell( ... ).data() - Get the cell data - There was a bug in `_selector_first` whereby it wasn't correctly reducing the set - was gatting away with it before, because it was reducing to a single element array, which has a toString() method which allowed it to appear to work for integers. --- media/src/DataTables.js | 1 + media/src/api/api._selectors.js | 19 ++++---- media/src/api/api.base.js | 13 ++++-- media/src/api/api.cells.js | 78 +++++++++++++++++++++++++++++++++ media/src/api/api.row.js | 6 ++- 5 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 media/src/api/api.cells.js diff --git a/media/src/DataTables.js b/media/src/DataTables.js index de4bda79..329c8a41 100644 --- a/media/src/DataTables.js +++ b/media/src/DataTables.js @@ -117,6 +117,7 @@ require('api.rows.js'); require('api.row.js'); require('api.columns.js'); + require('api.cells.js'); require('api.order.js'); require('api.search.js'); require('api.static.js'); diff --git a/media/src/api/api._selectors.js b/media/src/api/api._selectors.js index 4c43205c..2d31d720 100644 --- a/media/src/api/api._selectors.js +++ b/media/src/api/api._selectors.js @@ -106,21 +106,20 @@ var _range = function ( len ) var _selector_first = function ( inst ) { // Reduce the API instance to the first item found - var found = false; for ( var i=0, ien=inst.length ; i 0 ) { - inst[i].splice( 1, inst[i].length ); - inst.context = [ inst.context[i] ]; + if ( inst[i].length > 0 ) { + // Assign the first element to the first item in the instance + // and truncate the instance and context + inst[0] = inst[i][0]; + inst.length = 1; + inst.context = [ inst.context[i] ]; - found = true; - } - } - else { - inst[i].splice( 0, inst[i].length ); + return inst; } } + // Not found - return an empty instance + inst.length = 0; return inst; }; diff --git a/media/src/api/api.base.js b/media/src/api/api.base.js index fed37c37..82c2294e 100644 --- a/media/src/api/api.base.js +++ b/media/src/api/api.base.js @@ -324,7 +324,7 @@ _Api.prototype = /** @lends DataTables.Api */{ a = [], ret, i, ien, j, jen, context = this.context, - rows, items, + rows, items, item, selector = this.selector; // Argument shifting @@ -350,7 +350,7 @@ _Api.prototype = /** @lends DataTables.Api */{ a.push( ret ); } } - else if ( type === 'column' || type === 'column-rows' || type === 'row' ) { + else if ( type === 'column' || type === 'column-rows' || type === 'row' || type === 'cell' ) { // columns and rows share the same structure. // 'this' is an array of column indexes for each context items = this[i]; @@ -360,7 +360,14 @@ _Api.prototype = /** @lends DataTables.Api */{ } for ( j=0, jen=items.length ; j */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; + + return this.iterator( 'table', function ( settings, idx ) { + a = []; + + for ( i=0, ien=rows[idx].length ; i