mirror of
https://github.com/DataTables/DataTables.git
synced 2025-01-30 23:52:11 +01:00
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.
This commit is contained in:
parent
103e997db8
commit
f5286dc130
@ -110,7 +110,7 @@ var _selector_first = function ( inst )
|
||||
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[0] = inst[i];
|
||||
inst.length = 1;
|
||||
inst.context = [ inst.context[i] ];
|
||||
|
||||
|
@ -618,5 +618,23 @@ _Api.register = function ( name, val )
|
||||
};
|
||||
|
||||
|
||||
_Api.registerPlural = function ( pluralName, singularName, val ) {
|
||||
_Api.register( pluralName, val );
|
||||
|
||||
_Api.register( singularName, function () {
|
||||
var ret = val.apply( this, arguments );
|
||||
|
||||
if ( ret instanceof _Api ) {
|
||||
return ret.length ?
|
||||
$.isArray( ret[0] ) ?
|
||||
new _Api( ret.context, ret[0] ) : // Array results are 'enhanced'
|
||||
ret[0] :
|
||||
undefined;
|
||||
}
|
||||
return ret;
|
||||
} );
|
||||
};
|
||||
|
||||
|
||||
}());
|
||||
|
||||
|
@ -35,7 +35,7 @@ _api.register( 'cells()', function ( rowSelector, columnSelector, opts ) {
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'cells().nodes()', function () {
|
||||
_api.registerPlural( 'cells().nodes()', 'cell().nodes()', function () {
|
||||
return this.iterator( 'cell', function ( settings, row, column ) {
|
||||
return settings.aoData[ row ].anCells[ column ];
|
||||
} );
|
||||
@ -49,7 +49,10 @@ _api.register( 'cells().data()', function () {
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'cells().invalidate()', function ( src ) {
|
||||
_api.register( [
|
||||
'cells().invalidate()',
|
||||
'cell().invalidate()'
|
||||
], function ( src ) {
|
||||
var selector = this.selector;
|
||||
|
||||
// Use the rows method of the instance to perform the invalidation, rather
|
||||
@ -68,15 +71,6 @@ _api.register( 'cell()', function ( rowSelector, columnSelector, opts ) {
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'cell().node()', function () {
|
||||
var ctx = this.context;
|
||||
|
||||
if ( ctx.length && this.length ) {
|
||||
return ctx[0].aoData[ this[0].row ].anCells[ this[0].column ];
|
||||
}
|
||||
// undefined
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'cell().data()', function ( data ) {
|
||||
var ctx = this.context;
|
||||
@ -96,17 +90,6 @@ _api.register( 'cell().data()', function ( data ) {
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'cell().invalidate()', function ( src ) {
|
||||
var ctx = this.context;
|
||||
|
||||
if ( ctx.length && this.length ) {
|
||||
_fnInvalidateRow( ctx[0], this[0].row, src );
|
||||
}
|
||||
|
||||
return this;
|
||||
} );
|
||||
|
||||
|
||||
|
||||
}());
|
||||
|
||||
|
@ -89,7 +89,7 @@ _api.register( 'columns()', function ( selector, opts ) {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
_api.register( 'columns().header()', function ( selector, opts ) {
|
||||
_api.registerPlural( 'columns().header()', 'column().header()', function ( selector, opts ) {
|
||||
return this.iterator( 'column', function ( settings, column ) {
|
||||
return settings.aoColumns[column].nTh;
|
||||
} );
|
||||
@ -99,7 +99,7 @@ _api.register( 'columns().header()', function ( selector, opts ) {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
_api.register( 'columns().data()', function () {
|
||||
_api.registerPlural( 'columns().data()', 'column().data()', function () {
|
||||
return this.iterator( 'column-rows', function ( settings, column, i, j, rows ) {
|
||||
var a = [];
|
||||
for ( var row=0, ien=rows.length ; row<ien ; row++ ) {
|
||||
@ -110,7 +110,7 @@ _api.register( 'columns().data()', function () {
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'columns().visible()', function ( vis ) {
|
||||
_api.registerPlural( 'columns().visible()', 'column().visible()', function ( vis ) {
|
||||
return this.iterator( 'column', function ( settings, column ) {
|
||||
return _setColumnVis( settings, column, vis );
|
||||
} );
|
||||
@ -158,42 +158,5 @@ _api.register( 'column()', function ( selector, opts ) {
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'column().data()', function () {
|
||||
var ctx = this.context;
|
||||
|
||||
if ( ctx.length && this.length ) {
|
||||
return this.columns( this[0] ).data().flatten();
|
||||
}
|
||||
// return undefined
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'column().header()', function () {
|
||||
var ctx = this.context;
|
||||
|
||||
if ( ctx.length && this.length ) {
|
||||
return this.columns( this[0] ).header().flatten();
|
||||
}
|
||||
// return undefined
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'column().visible()', function ( vis ) {
|
||||
var ctx = this.context;
|
||||
|
||||
if ( vis === undefined ) {
|
||||
// Get
|
||||
return ctx.length && this.length ?
|
||||
ctx[0].aoColumns[ this[0] ].bVisible :
|
||||
undefined;
|
||||
}
|
||||
|
||||
// Set
|
||||
_setColumnVis( ctx[0], this[0], vis );
|
||||
|
||||
return this;
|
||||
} );
|
||||
|
||||
|
||||
}());
|
||||
|
||||
|
@ -77,8 +77,11 @@ _Api.register( 'order.listener()', function ( node, column, callback ) {
|
||||
} );
|
||||
|
||||
|
||||
// Order by the selected columns
|
||||
_Api.register( 'columns().order()', function ( dir ) {
|
||||
// Order by the selected column(s)
|
||||
_Api.register( [
|
||||
'columns().order()',
|
||||
'column().order()'
|
||||
], function ( dir ) {
|
||||
var that = this;
|
||||
|
||||
return this.iterator( 'table', function ( settings, i ) {
|
||||
@ -92,17 +95,6 @@ _Api.register( 'columns().order()', function ( dir ) {
|
||||
} );
|
||||
} );
|
||||
|
||||
// Order by the selected columns
|
||||
_Api.register( 'column().order()', function ( dir ) {
|
||||
var ctx = this.context;
|
||||
|
||||
if ( ctx.length && this.length ) {
|
||||
ctx[0].aaSorting = [ [ this[0], dir ] ];
|
||||
}
|
||||
|
||||
return this;
|
||||
} );
|
||||
|
||||
|
||||
}());
|
||||
|
||||
|
@ -16,16 +16,6 @@ _api.register( 'row()', function ( 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().data()', function ( data ) {
|
||||
var ctx = this.context;
|
||||
|
||||
@ -46,33 +36,11 @@ _api.register( 'row().data()', function ( data ) {
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'row().invalidate()', function ( src ) {
|
||||
var ctx = this.context;
|
||||
|
||||
if ( ctx.length && this.length ) {
|
||||
_fnInvalidateRow( ctx[0], this[0], src );
|
||||
}
|
||||
|
||||
return this;
|
||||
} );
|
||||
|
||||
|
||||
_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;
|
||||
} );
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -35,7 +35,7 @@ _api.register( 'rows()', function ( selector, opts ) {
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'rows().nodes()', function () {
|
||||
_api.registerPlural( 'rows().nodes()', 'row().nodes()' , function () {
|
||||
return this.iterator( 'row', function ( settings, row ) {
|
||||
// use pluck order on an array rather - rows gives an array, row gives it individually
|
||||
return settings.aoData[ row ].nTr || undefined;
|
||||
@ -50,14 +50,14 @@ _api.register( 'rows().data()', function ( data ) {
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'rows().invalidate()', function ( src ) {
|
||||
_api.registerPlural( 'rows().invalidate()', 'row().invalidate()', function ( src ) {
|
||||
return this.iterator( 'row', function ( settings, row ) {
|
||||
_fnInvalidateRow( settings, row, src );
|
||||
} );
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'rows().remove()', function () {
|
||||
_api.registerPlural( 'rows().remove()', 'row().remove()', function () {
|
||||
var that = this;
|
||||
|
||||
return this.iterator( 'row', function ( settings, row, thatIdx ) {
|
||||
|
@ -21,7 +21,10 @@ _api.register( 'search()', function ( input, caseInsen, regex, smart ) {
|
||||
} );
|
||||
|
||||
|
||||
_api.register( 'columns().search()', function ( input, caseInsen, regex, smart ) {
|
||||
_api.register( [
|
||||
'columns().search()',
|
||||
'column().search()'
|
||||
], function ( input, caseInsen, regex, smart ) {
|
||||
return this.iterator( 'column', function ( settings, column ) {
|
||||
if ( ! settings.oFeatures.bFilter ) {
|
||||
return;
|
||||
|
Loading…
x
Reference in New Issue
Block a user