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

New: Automatic detection of HTML5 data-* attributes for orthogonal data

- Building on the new ability of DataTables to work with element
  attributes as data sources for sorting and filtering, this commit adds
  auto-detection of the attributes 'data-sort' and 'data-filter' in DOM
  sourced tables.

- The first row in the table is used for this auto-detection and the
  built mData object assigned only when it is found to be the default
  for the column (i.e. the column index). So any custom value is
  retained and thus if you want to use a custom value you need to build
  the attribute querying object manually (I think the majority of uses
  will not need this!).
This commit is contained in:
Allan Jardine 2013-06-17 11:35:06 +01:00
parent 0f2cc15217
commit 5cecbb5528
2 changed files with 35 additions and 6 deletions

View File

@ -247,8 +247,9 @@ if ( oInit.asStripeClasses === null )
/* Remove row stripe classes if they are already on the table row */
var stripeClasses = oSettings.asStripeClasses;
var rowOne = $('tbody tr:eq(0)', this);
if ( $.inArray( true, $.map( stripeClasses, function(el, i) {
return $('tbody tr:eq(0)', this).hasClass(el);
return rowOne.hasClass(el);
} ) ) !== -1 ) {
$('tbody tr', this).removeClass( stripeClasses.join(' ') );
oSettings.asDestroyStripes = stripeClasses.slice();
@ -292,6 +293,31 @@ _fnApplyColumnDefs( oSettings, oInit.aoColumnDefs, aoColumnsInit, function (iCol
_fnColumnOptions( oSettings, iCol, oDef );
} );
/* HTML5 attribute detection - build an mData object automatically if the
* attributes are found
*/
if ( rowOne.length ) {
$.each( _fnGetRowElements( oSettings, rowOne[0] ).cells, function (i, cell) {
var col = oSettings.aoColumns[i];
if ( col.mData === i ) {
var sort = cell.getAttribute('data-sort');
var filter = cell.getAttribute('data-filter');
if ( sort !== null || filter !== null ) {
col.mData = {
_: i+'.display',
sort: sort !== null ? i+'.@data-sort' : undefined,
type: sort !== null ? i+'.@data-sort' : undefined,
filter: filter !== null ? i+'.@data-filter' : undefined
};
_fnColumnOptions( oSettings, i );
}
}
} );
}
/* Must be done after everything which can be overridden by the state saving! */
if ( oInit.bStateSave )

View File

@ -558,10 +558,13 @@ function _fnGetRowElements( settings, row )
columns = settings.aoColumns;
var attr = function ( str, data, td ) {
var idx = str.indexOf('@');
if ( typeof str === 'string' && idx !== -1 ) {
var src = str.substring( idx+1 );
o[ '@'+src ] = td.getAttribute( src );
if ( typeof str === 'string' ) {
var idx = str.indexOf('@');
if ( idx !== -1 ) {
var src = str.substring( idx+1 );
o[ '@'+src ] = td.getAttribute( src );
}
}
};
@ -572,7 +575,7 @@ function _fnGetRowElements( settings, row )
col = columns[i];
contents = $.trim(td.innerHTML);
if ( col._bAttrSrc ) {
if ( col && col._bAttrSrc ) {
o = {
display: contents
};