reading properties from table

reading properties from table

petterpetter Posts: 8Questions: 0Answers: 0
edited March 2009 in Plug-ins
Extension for datatables to read intit properties from the table..
Currently in Alpha version.
More properties to be addeded and we must implement looping over the TH tags of the table to read sorting/type info..

jQuery.fn.dataTableExt.fnGetHtmlOptions = function(t, defaults) {
if (t.tagName != "TABLE") return;

var TableNames = ["bPaginate", "iDisplayLength", "bSortable", "bFilter", "bLengthChange","bAutoWidth"];
var ThNames = ["bVisible", "bSearchable", "bSortable", "sType"];

var name;
var colInfo = new Array;
var bFound = false;
var ret = {};

for (var pos in TableNames) {
name = TableNames[pos];
if (t.getAttribute(name)) {
ret[name] = t.getAttribute(name);
}
}


$("th", t).each(function(i) {
var thProps = {};
for (var pos in ThNames) {
name = ThNames[pos];
if (this.getAttribute(name)) {
thProps[name] = this.getAttribute(name);
bFound = true;
}
}
if (thProps) {
colInfo[i] = jQuery.fn.dataTableExt._fnTransformValues(thProps);
} else {
colInfo[i] = null;
}
});

if (bFound) {
if (!defaults.aoColumns) defaults.aoColumns = colInfo;
else { /*loop and replace...*/ }
}

ret = jQuery.fn.dataTableExt._fnTransformValues(ret);

for (var n in ret) {
if (typeof defaults[n] != "array")
defaults[n] = ret[n];
}
return defaults;
}

//Transforming values according to the prefix. Defaulting to string.... 'Not to good solution' but it works....
jQuery.fn.dataTableExt._fnTransformValues = function(o) {
var val;
for (var n in o) {
val = o[n];
switch (n.substring(0,1))
{
case "b": //Boolean
if (val.toUpperCase() == "FALSE") val = false;
else val = true;
break;
case "i": //Integer
val = parseInt(val);
break;
}
o[n] = val;
}
return o;
}

//Init like this...
var defaultSettings = {
sDom: "<'Data't>",
sPaginationType: "full_numbers",
bAutoWidth: false,
iDisplayLength: 5,
bLengthChange: false,
oLanguage: { sUrl: '/javascripts/Table_nb_NO.js'}
};

$(document).ready(function() {

$(".dataTable").each(
function(i) {
$(this).dataTable($.fn.dataTableExt.fnGetHtmlOptions(this,defaultSettings));
$(this).show();
});
});
This discussion has been closed.