From ed34f7972d991eab8a7f8e2121a551566caea918 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Mon, 6 May 2013 07:14:43 +0100 Subject: [PATCH] New: `row()` methods - operate on a single row. - The rows() method allows operations on multiple rows with a single call, this new singualr option, `row()` performs operations on a single row, allowing fine grain control and easy access to certain aspects. For example. `rows().data()` will return an array of data objects, even if there is only one row as a result of the selector, while `row().data()` will return the data object directly. - Impletemented thus far: - row() - row().node() - row().cells() - row().data() - get only as of yet - row().index() - row().remove() --- media/src/DataTables.js | 1 + media/src/api/api._selectors.js | 23 +++++++++++ media/src/api/api.row.js | 67 +++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 media/src/api/api.row.js diff --git a/media/src/DataTables.js b/media/src/DataTables.js index 5897f0a4..74c050e1 100644 --- a/media/src/DataTables.js +++ b/media/src/DataTables.js @@ -116,6 +116,7 @@ require('api.order.js'); require('api._selectors.js'); require('api.rows.js'); + require('api.row.js'); require('api.columns.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 b5265428..4c43205c 100644 --- a/media/src/api/api._selectors.js +++ b/media/src/api/api._selectors.js @@ -102,6 +102,29 @@ 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] ]; + + found = true; + } + } + else { + inst[i].splice( 0, inst[i].length ); + } + } + + return inst; +}; + + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Tables */ diff --git a/media/src/api/api.row.js b/media/src/api/api.row.js new file mode 100644 index 00000000..6c7e6bf2 --- /dev/null +++ b/media/src/api/api.row.js @@ -0,0 +1,67 @@ + + +(/** @lends */function() { + +var _api = DataTable.Api; + + + + + +/** + * + */ +_api.register( 'row()', function ( selector, opts ) { + return _selector_first( this.rows( selector, opts ) ); +} ); + + +_api.register( 'row().node()', function () { + var ctx = this.context; + + if ( ctx.length && this.length ) { + return ctx[0].aoData[ this[0] ].nTr || undefined; + } + // return undefined; +} ); + + +_api.register( 'row().cells()', function () { + var ctx = this.context; + + if ( ctx.length && this.length ) { + return ctx[0].aoData[ this[0] ].anCells || undefined; + } + // return undefined; +} ); + + +_api.register( 'row().data()', function ( data ) { + var ctx = this.context; + + if ( ctx.length && this.length ) { + return ctx[0].aoData[ this[0] ]._aData; + } + // return undefined; + + // @todo - Set operator +} ); + + +_api.register( 'row().index()', function () { + return this.length ? this[0] : undefined; +} ); + + +_api.register( 'row().remove()', function () { + if ( this.length ) { + // Hand off to the rows function + this.rows( this[0] ).remove(); + } + return this; +} ); + + + +}()); +