RailsCasts episode 340 on DataTables was interesting, but with the release of DataTables v1.10, lots have changed. Ajax is simpler but API's are easier to configure. Does anyone have examples of Rails code that creates Ajax data for DataTables? It'll be useful if it can be used for sorting, searching, in addition to pagination.
1 Answer
Yes, it is very useful for sorting ,searching and also pagination both for client side as well as serverside but not at the same time. It is a very good plugin(gem), I have used it. Example:-Ajax data for datatable
var table = $("#example_id").DataTable({
iDisplayLength: 100,
bInfo: false,
bSort: true,
sPaginationType: "full_numbers",
bStateSave: true,
bDestroy: true,
bProcessing: true,
bServerSide: true,
bFilter: false,
sAjaxSource: '/example/action_name',
fnServerParams: function (aoData) {
aoData.push(
{ "name": "example1", "value": $("#_example1").val() },
{ "name": "example2", "value": 5 },
{ "name": "example3", "value": "My Name" }
);
},
oLanguage:{
sZeroRecords: "No records found."},
"sDom": 'rtlfip'
});
Hope this will help you!!
4 Comments
netwire
Thanks, this is helpful. Do you also have server side (Rails) example?
Anuj Dubey
Server side (Rails) example? I didnt get you..for datatable?
netwire
Rails server side example to provide the JSON/ajax results needed that Datatable can consume.
Anuj Dubey
Yes, I can give example $.ajax({ url: '/example/search_result', method: 'post', dataType: 'JSON', async: false, data:data_params, success: function (data){ table.fnDraw(); }, failure: function (msg) { console.log("Error in sending data"); } });