After datatables().clear cannot add new row?
After datatables().clear cannot add new row?
hello all, I'm making a website that displays the data dynamically based on parameters without having to change their web pages, the obstacles that I experienced was after the command DataTable (). clear; I can not add a new line, although with the same data as beginning,
here my code:
$r = '[{"Id":"1","OrderNum":"1","Caption":"Profiles","Hint":"not yet","Action":"(none)","Parameter":""}]';
dbTable.clear().draw();
dbTable.row.add(r).draw();
on first load it's work and data is loaded, i don't what when wrong,
Regard's, Anton
This question has an accepted answers - jump to answer
Answers
You're specifying the row with a
$
, as$r
, but calling justr
indbTable.row.add(r).draw()
sorry typo, the right code is
var r = '[{"Id":"1","OrderNum":"1","Caption":"Profiles","Hint":"not yet","Action":"(none)","Parameter":""}]';
it's always return with message
DataTables warning: table id=dataTables - Requested unknown parameter 'Id' for row 0. For more information about this error, please see http://datatables.net/tn/4
What make me confuse is everything is doing fine, until table clear() and load new data
Regard's, Anton
Can you show all of the DT related code?
Just making sure, you have the columns named
Id
,OrderNum
,Caption
, etc, inside thecolumns
config right?Also, just noticed, the
r
variable you have, the JSON shouldnt be in quotes.. meaning this will do:dbTable = $('#dataTables').DataTable({
responsive: true,
order: [[ 1, "asc" ]],
sAjaxSource: "menus-data.php?id=0",
sAjaxDataProp: "",
aoColumns: [
{ "mData": "Id" },
{ "mData": "OrderNum" },
{ "mData": "Caption" },
{ "mData": "Hint" },
{ "mData": "Action" },
{ "mData": "Parameter" }],
columnDefs: [
{ "visible": false, "targets": [0] },
{ "type": "num", "targets": [0] }
]
});
output from php like this:
[
{
"Id":"1",
"OrderNum":"1",
"Caption":"Profiles",
"Hint":"not yet",
"Action":"(none)",
"Parameter":""}
]
Regard's, Anton
Can you use proper formatting? Makes it easier to read..
Also, I created this quick demo for ya: http://live.datatables.net/juhacoze/1/edit?js,output
If you're using
row.add()
, then you dont need to specify an array with a single object asvar r
, rather just a single object.. if you're usingrows.add()
, then you would need to specify an array with one or more objects.So if you can change the JSON thats returned by the PHP to output just:
Then it should work, if not, change the
row.add()
torows.add()
Also.. just an FYI, you can chain the methods, so this will work as well:
after i call this code, error showed..
Edit: I changed it a little, you dont have to specify the
columns.name
, justcolumns.data
If the literal output from PHP is an array of a single object.. then use
rows.add()
, instead ofrow
..http://live.datatables.net/siloxave/1/edit?js,output
i understand, but sometimes it's result more than 1 row depands parameter that i send
what is wrong with my code, it's using same php file from ajax and result same format
lol.. Ive said it 2 or 3 times....
One more time.
use
rows.add()
instead ofrow.add()
.. then you can pass an array of any number of objectsAlso, instead of
You just need one
draw()
.. So:The less draws, the better
Also, one more thing.. it might be a good idea to either specify the
dataType
asjson
in your jQuery request, or if you cant do that, then you can use parseJSON, but I recommend the formeroh, i dont see that, thank you jLinux it's work
Ok, cool, glad I could help