1
0
mirror of https://github.com/DataTables/DataTables.git synced 2025-02-18 16:54:14 +01:00

New: column() API methods for working with a single column

- To round off the API, this commit adds a column() method, for working
  with a single column, as with row/rows, cell/cells and table/tables.

- New methods:
  - column() - Column selector
  - column().data() - Get column data
  - column().header() - Get the column header cell
  - column().visible( set ) - Get / set the column visiblity
  - column().order( dir ) - Order the table by this column

- Note that I haven't implemented column().search() yet as I'm sure
  there must be a better way of dealing with the singular functions...
  Need to look at the four groups a bit more before this (and then the
  API :-) ) is finalised
This commit is contained in:
Allan Jardine 2013-05-19 15:53:54 +01:00
parent 1b4688cdf3
commit 103e997db8
2 changed files with 108 additions and 54 deletions

View File

@ -4,6 +4,60 @@
var _api = DataTable.Api;
var _setColumnVis = function ( settings, column, vis ) {
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 );
_fnCallbackFire( settings, null, 'column-visibility', [settings, column, vis] );
_fnSaveState( settings );
};
/**
*
@ -56,61 +110,9 @@ _api.register( 'columns().data()', function () {
} );
_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 );
_fnCallbackFire( settings, null, 'column-visibility', [settings, column, vis] );
_fnSaveState( settings );
return _setColumnVis( settings, column, vis );
} );
} );
@ -151,5 +153,47 @@ _api.register( 'column.index()', function ( type, idx ) {
_api.register( 'column()', function ( selector, opts ) {
return _selector_first( this.columns( 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;
} );
}());

View File

@ -77,7 +77,7 @@ _Api.register( 'order.listener()', function ( node, column, callback ) {
} );
// Order by the selected column(s)
// Order by the selected columns
_Api.register( 'columns().order()', function ( dir ) {
var that = this;
@ -92,6 +92,16 @@ _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;
} );
}());