Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  45] [ 2]  / answers: 1 / hits: 29773  / 13 Years ago, wed, june 1, 2011, 12:00:00

I'm using DataTables (datatables.net) to display data from an Ajax source and having trouble customizing it. One thing I would like to do is add a column so I can have for example an 'edit' button for each row.



The closest thing to that in the examples is here but I can't get that to work with an ajax source.



Currently, I'm using the following code to display my table:



fnServerObjectToArray = function ( aElements ){
return function ( sSource, aoData, fnCallback ) {
$.ajax( {
dataType: 'json',
type: POST,
url: sSource,
data: aoData,
success: function (json) {
var a = [];
for ( var i=0, iLen=json.aaData.length ; i<iLen ; i++ ) {
var inner = [];
for ( var j=0, jLen=aElements.length ; j<jLen ; j++ ) {

inner.push( json.aaData[i][aElements[j]] );
}
a.push( inner );
}
json.aaData = a;
fnCallback(json);
}
} );
}
}

$(document).ready(function() {
$('#example').dataTable( {
bProcessing: true,
sAjaxSource: 'get_data.php',
fnServerData: fnServerObjectToArray( [ 'username', 'email' ] )
} );
});

More From » jquery

 Answers
6

Why don't you use fnRenderFunction in the aoColumns? As an example:



aoColumns: [ { bVisible: false} , null, null, null, null,
{ sName: ID,
bSearchable: false,
bSortable: false,
fnRender: function (oObj) {
return <a href='EditData.php?id= + oObj.aData[0] + '>Edit</a>;
}
}
]


You can use it to format the value from the server side.



See similar example on the http://jquery-datatables-editable.googlecode.com/svn/trunk/ajax-inlinebuttons.html (ignore specific settings for the editable plugin)


[#91937] Tuesday, May 31, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jimmiejudahm

Total Points: 319
Total Questions: 98
Total Answers: 117

Location: Venezuela
Member since Sat, Apr 24, 2021
3 Years ago
;