Setting options in HTML attributes possible?
Setting options in HTML attributes possible?
Hi all,
I've searched for it, but have not been able to find anything yet. I was hoping to find a plugin that reads html attributes to set the options for a datatable.
I often have tables with a hidden first column for IDs (used by a javascript function to know the database record ID for the current row) and a visible but unsorted column at the end for a menu of user actions (like View Details, Edit, Delete) on the row data.
Instead of using the regular options for the dataTable() function, wouldn't the following be a lot easier (to read, to enter)? Also, when changing the columns in the table, I wouldn't need to change the options for the dataTable() function.
[code]
ID
First Name
Last Name
Country
Date of Birth
Actions
[/code]
Is there any plug-in that does this? Or is there a reason not to want this?
Thanks!
Jerry
I've searched for it, but have not been able to find anything yet. I was hoping to find a plugin that reads html attributes to set the options for a datatable.
I often have tables with a hidden first column for IDs (used by a javascript function to know the database record ID for the current row) and a visible but unsorted column at the end for a menu of user actions (like View Details, Edit, Delete) on the row data.
Instead of using the regular options for the dataTable() function, wouldn't the following be a lot easier (to read, to enter)? Also, when changing the columns in the table, I wouldn't need to change the options for the dataTable() function.
[code]
ID
First Name
Last Name
Country
Date of Birth
Actions
[/code]
Is there any plug-in that does this? Or is there a reason not to want this?
Thanks!
Jerry
This discussion has been closed.
Replies
I'm planning this as the first new plug-in once v1.10 is released. The attributes will be `data-sorting="..."` etc - i.e. conforming to HTML5 data attributes and camelCase rather than hungarian.
Allan
I am going to write one because I think it would save me time real quickly. I'll try and conform to HTML attribute naming and hope my name will not differ too much from the eventual official ones!
I am now working with data-paginate for the table tag and data-visible and data-sortable for the th tags.
Thanks!
Allan
As I am currently trying to make DataTables work for yootheme's uikit, I have removed a lot of CSS (because I want to use the uikit styles), and only a small set of the options are useful to us at the moment, but it's a start for someone who wants to add more options to be taken from HTML.
[code]
function HtmlAttributesToDatatableOptions( id ) {
// add a table setting that takes a single or multi array ( col-index, setting-value )
function AddTableSettingSingleMultiArray( setting, value, target ) {
if ( options.hasOwnProperty( setting ) ) {
// property exists: add to array
options[ setting ].push( [ target, value ] );
} else {
// setting does not yet exist: add as multi array, easier to append to
var o = [ [ target, value ] ];
options[ setting ] = o;
}
}
// add a column setting { setting : setting-value, aTarget : [ ... ] }
function AddColumnSetting( setting, value, target ) {
for ( ix in coldefs ) {
if ( coldefs[ ix ].hasOwnProperty( setting ) ) {
if ( coldefs[ ix ][ setting ] == value ) {
coldefs[ ix ].aTargets.push( target );
return;
}
}
}
var o = {};
o[ setting ] = value;
o.aTargets = [ target ];
coldefs.push( o );
}
var options = {};
var coldefs = [];
// read table attributes
if ( jQuery( id ).attr( 'data-paginate' ) ) {
options.bPaginate = ( jQuery( id ).attr( 'data-paginate' ) != "0" );
}
if ( jQuery( id ).attr( 'data-pagelength' ) ) {
options.iDisplayLength = parseInt( jQuery( id ).attr( 'data-pagelength' ) );
}
// read column attributes
jQuery( id + ' > thead > tr > th' ).each(
function( ix, elm ) {
var coldef = {};
// TODO:
// sType: string, numeric, date, html
// sort by ALT or TITLE tag!
// check sortable (0/1)
if ( jQuery( elm ).attr( 'data-sortable' ) ) {
AddColumnSetting( 'bSortable', ( jQuery( elm ).attr( 'data-sortable' ) != "0" ), ix );
}
// check initial sort order (asc/desc) (columna ttribute to table setting!)
if ( jQuery( elm ).attr( 'data-initsort' ) ) {
AddTableSettingSingleMultiArray( 'aaSorting', jQuery( elm ).attr( 'data-initsort' ), ix );
}
// check visible (0/1)
if ( jQuery( elm ).attr( 'data-visible' ) ) {
AddColumnSetting( 'bVisible', ( jQuery( elm ).attr( 'data-visible' ) != "0" ), ix );
}
// check sort column (integer)
if ( jQuery( elm ).attr( 'data-sortcolumn' ) ) {
AddColumnSetting( 'iDataSort', parseInt( jQuery( elm ).attr( 'data-sortcolumn' ) ), ix );
}
// check class (string)
if ( jQuery( elm ).attr( 'data-class' ) ) {
AddColumnSetting( 'sClass', jQuery( elm ).attr( 'data-class' ), ix );
}
}
);
// do not add an empty array
if ( coldefs.length > 0 ) {
options.aoColumnDefs = coldefs;
}
//console.log( options );
return options;
}
[/code]
To use this function, I have this:
[code]
var options = { [set your defaults here] };
// pre-init: read extra options from table attributes (extension for DataTable)
jQuery.extend( options, HtmlAttributesToDatatableOptions( id ) );
// regular way of initializing DataTables
var oTable = jQuery( id ).dataTable( options );
[/code]
In the HTML I have this:
[code]
id
sort-created
created
description
sort-playdate
actions
[/code]