[Contribution] get columns types from HTML

[Contribution] get columns types from HTML

ValkyValky Posts: 5Questions: 0Answers: 0
edited April 2012 in DataTables 1.9
Because my tables are generated in PHP and each have their own columns and types, I was looking for a solution to avoid sType "boring" declaration in javascript.

I found a way to do it, by adding a "type" property in the of the table.
I share it, if it can help anyone.

HTML example
[code]



Ref.
Product name
Price
Date




225-ABCSun glasses$180,002012/01/10


312-ABCBlue Sun glasses$145,002012/01/20


150-ABCBlack Sun glasses$90,002011/12/01



[/code]

JAVASCRIPT
[code]
$(document).ready(function(){
$('#table_list').dataTable({"aoColumns": getColType()});
});
// Function to get the attribut "type" of the columns header
function getColType()
{
var type_list='[';
$('#table_list thead tr th').each(function(){
var type=$(this).attr('type');
if(type)
type_list+='{"sType": "'+type+'"},';
else
type_list+='{"sType": null},';});
return eval(type_list.slice(0, -1)+']');
}
// Add the "formatted-num" plugin and each you need
[/code]

It could be optimized, but it works like a charm.

Replies

  • allanallan Posts: 61,775Questions: 1Answers: 10,112 Site admin
    Very nice! Thanks for sharing this with us all - I'm sure it will prove to be very useful.

    Allan
This discussion has been closed.