Duplicate data in the data table
Duplicate data in the data table

I am adding data from one table to another table. I want to check if there is any in built function which checks for duplicates before adding to the data table. I do have a primary key.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
No, DataTables doesn't restrict what you add to the table. You could check yourself fairly easily with something like
table.column(pk_column).data().toArray().indexOf(pk) !== -1
,Colin
If this is how I am adding to the data to the table, how should I check if the existing table already has id or not?
The code that Colin provided should work. Did you try it?
Might look something like this:
Kevin
Yeah, I tried.. it did not work..
What is this pk_column?? Can you explain?
Which table column contains
"id": data.id,
? I made an assumption it is 0 (the first column). You may want to look thecolumn()
column-selector
docs to see how to select the appropriate column.When you say it doesn't work what happens? Do you get console errors? Does the row get added when the ID is already there?
Maybe you can post a link to your page are a test case so we can see exactly what you are doing. This will allow us to more easily help you debug your code.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
This is never adding the row because the value is always === -1
instead this is working in my case
The second one is not letting me add any duplicates to the table at all.