Ajax source and custom attributes
Ajax source and custom attributes
JoeDavola
Posts: 3Questions: 0Answers: 0
Let's say I have this table:
[code]
A
B
C
X1
Y1
Z1
X2
Y2
Z2
[/code]
Is it possible to create the same table using ajax? Can you pass the "id" and "title" attributes for and ?
[code]
A
B
C
X1
Y1
Z1
X2
Y2
Z2
[/code]
Is it possible to create the same table using ajax? Can you pass the "id" and "title" attributes for and ?
This discussion has been closed.
Replies
Regards,
Allan
Thank you for your help.
Is there a way to process data before it's added to the row? I'd like to be able to pass an object instead of a string and then use that object to create custom attributes. For example:
[code]
$(document).ready(function() {
$('#example').dataTable( {
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
/* imagine aData[0] is an object, not a string {text: 'X1', title: 'Title X1'} */
$('td:eq(0)', nRow).html(aData[0].text);
$('td:eq(0)', nRow).attr('title', aData[0].title);
return nRow;
}
} );
} );
[/code]
[code]
$(document).ready(function() {
$('#example').dataTable( {
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
var oCellData = JSON.parse(aData[0]);
$('td:eq(0)', nRow).html(oCellData.text);
$('td:eq(0)', nRow).attr('title', oCellData.title);
return nRow;
}
} );
} );
[/code]
Not sure if that's the best solution, but it works :)
Allan
Allan
There's an easy way to do it. In PHP send an array instead text for cell value:
[code]
$row[] = array('data'=>$aRow[$aColumns[$i]],'style'=>'width: 50px', 'cssclass'=>'fixedwidth');
[/code]
Then in your js do the following:
[code]
$('#example').dataTable({
...
"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
$('td',nRow).each(function(i,v){
if (typeof aData[i]=='object'){
if (typeof aData[i].style!='undefined'){
$(v).attr('style',aData[i].style);
}
if (typeof aData[i].cssclass!='undefined'){
$(v).removeClass(aData[i].cssclass);
$(v).addClass(aData[i].cssclass);
}
$(v).html('');
if (typeof aData[i].data!='undefined'){
$(v).html(aData[i].data);
}
}
});
}
}
...
}
);
[/code]
PHP arrays come as objects after json_encode.
That's all to allow sending objects and different properties in them. :)