Error in Refreshing Datatable Using AJAX in Codeigniter
Error in Refreshing Datatable Using AJAX in Codeigniter
First sorry for my bad english.
I am trying to create button in my datatable. The button for delete data in that row. Delete function works. The problem is after the delete, my Datatable did not refresh the content (Only when I refresh the page, the content refreshed).
I already try some suggestion in other's stackoverflow.
My datatble id = "data'
var data as global variable.
This is my JavaScript Code (inside document.ready) :
var temp = $('#data').DataTable
(
"columns":
[
null, null, null, null,
null, null, null,
"width": "17%"
]
);
table = JSON.parse('"' + temp + '"');
This is my code for button function
function delete(kode)
var x = confirm("Do you want to delete this data?");
if (x == true)
jQuery.ajax(
type : "POST",
url : "***"
dataType : 'json',
success : function()
table.ajax.reload();
alert("delete success");
,
error : function()
table.ajax.reload();
alert("delete fail");
);
Error in console :
Uncaught Type-error: Cannot read property 'ajax' of undefined
at Object.error (hotel:191)
at u (VM226 jquery.js:2)
at Object.fireWith [as rejectWith] (VM226 jquery.js:2)
at k (VM226 jquery.js:2)
at XMLHttpRequest. (VM226 jquery.js:2)
table
temp
error
Table is global variable. my bad, in my post, it should not data = JSON.parse('"' + temp + '"'); but it should be Table = JSON.parse('"' + temp + '"');
– Devin
Aug 25 at 8:37
when i remared the JSON.parse line. It gave me an alert "Invalid JSON response. For more information about this error, please see datatables.net/tn/1"
– Devin
Aug 25 at 8:40
when i remark the JSON.parse line. It gave me an alert "Invalid JSON response. For more information about this error, please see datatables.net/tn/1"; Of course I changed the variable temp to data when I tried it.
– Devin
Aug 25 at 8:49
@David if I may know, why you said the delete opeartion failed? When I checked in my table in database (MySQL) , that row (data) deleted.
– Devin
Aug 25 at 8:53
1 Answer
1
You need to write below in ajax success. It will retrieve or refresh all the data of it.
var $lmTable = $("#data").dataTable(bRetrieve: true);
$lmTable.fnDraw();
TypeError: table.fnDraw is not a function at Object.error (hotel:243) at u (VM140 jquery.js:2) at Object.fireWith [as rejectWith] (VM140 jquery.js:2) at k (VM140 jquery.js:2) at XMLHttpRequest.<anonymous> (VM140 jquery.js:2)
– Devin
Aug 25 at 8:42
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
"Cannot read property 'ajax' of undefined" - Where do you define a variable called
table
? Your DataTables instance is calledtemp
. You also may not need to reload the whole set of data, just delete that one record from the data. And you probably don't need to reload or modify the table at all in theerror
callback, since the delete operation failed.– David
Aug 24 at 9:48