Files
DataTables/media/src/api/api.columns.js
Allan Jardine d7058eb45d New: search() and columns().search() API methods
- Ability to filter the table through the new API.

- This effectively replaces the fnFilter method of the old API.

- Note that one capability which has been removed from the old API is the
  ability to not show the new filter on the filtering input elements. I
  can't see any use for that feature to be honest, so it has been
  dropped. The filtering inputs always show the global filter state now.

- Additionally, note that we've been forced to call this `search` here
  rather than `filter` since there is a `filter` method for the API
  collection instance. This might be revisted before release.
2013-04-29 07:51:41 +01:00

164 lines
3.4 KiB
JavaScript

(/** @lends <global> */function() {
var _api = DataTable.Api;
/**
*
*/
_api.register( 'columns()', function ( selector, opts ) {
// argument shifting
if ( selector === undefined ) {
selector = '';
}
else if ( $.isPlainObject( selector ) ) {
opts = selector;
selector = '';
}
opts = _selector_opts( opts );
var inst = this.iterator( 'table', function ( settings ) {
return _column_selector( settings, selector, opts );
} );
// Want argument shifting here and in _row_selector?
inst.selector.cols = selector;
inst.selector.opts = opts;
return inst;
} );
/**
*
*/
_api.register( 'columns().header()', function ( selector, opts ) {
return this.iterator( 'column', function ( settings, column ) {
return settings.aoColumns[column].nTh;
} );
} );
/**
*
*/
_api.register( 'columns().cells()', function () {
return this.iterator( true, 'column-rows', function ( settings, column, i, j, rows ) {
return _pluck_order( settings.aoData, rows, 'anCells', column ) ;
} );
} );
/**
*
*/
_api.register( 'columns().data()', function () {
return this.iterator( true, 'column-rows', function ( settings, column, i, j, rows ) {
var a = [];
for ( var row=0, ien=rows.length ; row<ien ; row++ ) {
a.push( _fnGetCellData( settings, rows[row], column, '' ) );
}
return a;
} );
} );
_api.register( 'columns().visible()', function ( vis ) {
return this.iterator( 'column', function ( settings, column ) {
var
cols = settings.aoColumns,
col = cols[ column ],
data = settings.aoData,
row, cells, i, ien, tr;
// Get
if ( vis === undefined ) {
return col.bVisible;
}
// Set
// No change
if ( col.bVisible === vis ) {
return;
}
if ( vis ) {
// Insert column
// Need to decide if we should use appendChild or insertBefore
var insertBefore = $.inArray( true, _pluck(cols, 'bVisible'), column+1 );
for ( i=0, ien=data.length ; i<ien ; i++ ) {
tr = data[i].nTr;
cells = data[i].anCells;
if ( tr ) {
// insertBefore can act like appendChild if 2nd arg is null
tr.insertBefore( cells[ column ], cells[ insertBefore ] || null );
}
}
}
else {
// Remove column
$( _pluck( settings.aoData, 'anCells', column ) ).remove();
col.bVisible = false;
_fnDrawHead( settings, settings.aoHeader );
_fnDrawHead( settings, settings.aoFooter );
_fnSaveState( settings );
}
// Common actions
col.bVisible = vis;
_fnDrawHead( settings, settings.aoHeader );
_fnDrawHead( settings, settings.aoFooter );
_fnSaveState( settings );
} );
} );
// _api.register( 'columns().show()', function () {
// var selector = this.selector;
// return this.columns( selector.cols, selector.opts ).visible( true );
// } );
// _api.register( 'columns().hide()', function () {
// var selector = this.selector;
// return this.columns( selector.cols, selector.opts ).visible( false );
// } );
_api.register( 'columns.adjust()', function () {
return this.iterator( 'table', function ( settings ) {
_fnAdjustColumnSizing( settings );
} );
} );
// Convert from one column index type, to another type
_api.register( 'column.index()', function ( type, idx ) {
if ( this.context.length !== 0 ) {
var ctx = this.context[0];
if ( type === 'fromVisible' || type === 'toIndex' ) {
return _fnColumnIndexToVisible( ctx, idx );
}
else if ( type === 'fromIndex' || type === 'toVisible' ) {
return _fnVisibleToColumnIndex( ctx, idx );
}
}
} );
}());