Created
January 5, 2019 12:02
-
-
Save gurkanakdeniz/b3108c944a0d41fbc44f1f20ccf30398 to your computer and use it in GitHub Desktop.
datatables use with jquery ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function exampleUseDatatables(tableId) { | |
newTable(tableId); | |
var data = []; //.. example get data with ajax | |
// ajax | |
// ... | |
//success : function(result) { | |
// reloadTableData(result, tableId); // result add table | |
//}, | |
//error: function(jqXHR, textStatus, errorThrown) { | |
// reloadTableData([], tableId); // empty data add table | |
//} | |
} | |
function renderData(data) { | |
// example data can be append | |
return '<button class="btn btn-success" type="button" name="button">D</button>' | |
+ '<button class="btn btn-dark" type="button" name="button">S</button>'; | |
} | |
function newTable(tableId){ | |
$('#' + tableId).DataTable({ | |
"data" : [], | |
"columns" : [ { | |
"data" : "name" | |
}, { | |
"data" : "date" | |
}, { | |
"data" : "size" | |
}, { | |
"data" : "process", | |
"render" : function(data, type, row) { | |
return renderData(data); | |
} | |
} ], | |
"columnDefs" : [ { | |
className : "example-css-class", | |
targets : "_all" | |
} //"targets": [ 0,1,2,3 ] } | |
], | |
retrieve : true | |
// tablo bir kere olusturulduktan sonra tekrar olusturmak icin | |
}); | |
} | |
function reloadTableData(data, tableId){ | |
var datatable = $('#' + tableId).DataTable(); | |
datatable.clear(); | |
datatable.rows.add(data); | |
datatable.draw(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment