Button to remove unique rows and display rows with duplicate column data
Button to remove unique rows and display rows with duplicate column data
2008jackson
Posts: 38Questions: 9Answers: 0
Greetings. I require to use a button to remove all unique rows returned through AJAX JSON and display rows with duplicate column data. With code below, im unable to pick the right rows using IndexOf.
Kindly assist to fix code below.
var table = $('#table_id').DataTable({
dom: 'PBfrtip',
"buttons": ['copy', 'excel', {
text: 'Show duplicates',
action: function(row, data, index) {
var allData = table.column(2).data().toArray();
if (allData.indexOf(data.Reg) == allData.lastIndexOf(data.Reg)) {
$(row).css('display', 'none');
}
}
}],
"aaData": data.d.results,
"aoColumns": [{
"mData": "Created",
"render": function(data, type, row) {
data = moment(data).format('DD-MM-YYYY HH:mm');
return data;
}
},
{
"mData": "Family"
},
{
"mData": "Reg"
},
{
"mData": "Date",
"render": function(data, type, row) {
data = moment(data).format('DD-MM-YYYY');
return data;
}
},
{
"mData": "Number"
}
]
});
This question has an accepted answers - jump to answer
Answers
The second table here removes duplicate rows - you can add that code into your button,
Colin
Thanks Colin for the good start! Code below works to pull duplicate entries. However, i require to pull the initial entry for matching Reg and duplicate entry instead of pulling the duplicate entry only.
Please advice.
I'm sorry, but I'm not clear what functionality you're after. Could you update my test case, please, and give an example of what you would expect to happen,
Colin
Hi Colin, sorry for not being clear. Test case at
http://live.datatables.net/nawudoba/1/edit
Code is working well to pull out the duplicates however, the first instance isnt available. I need to see 3 rows for 'John Smith' i.e. rows with same name column data > 1. Appreciate the support.
That would require a more complex loop. You can use
column().data()
along withtoArray()
to get an array of names, for example:Then you can use the
rows().every()
to loop through all the rows and see if the name is in the array more than once. This SO thread has some techniques for this. Choose the one that best fits your solution.Kevin
Hi Kevin, Appreciate the feedback. I've now been able to identify the unique column values to remove rows with var difference. Guess i need the row IDs for these values to remove the rows. Could you pls guide me to fetch the corresponding IDs to remove rows?
Sorry I meant something more like this:
http://live.datatables.net/nawudoba/4/edit
Kevin
Hi Kevin,
Your example works perfect for an array of arrays. However, im working with an array of objects. Refer example at http://live.datatables.net/nawudoba/5/edit
TypeError: Cannot read property 'name' of undefined.
Unable to pick the data field for name and filter values to remove uniques. Appreciate the support.
Can you update Kevin's test case with your data, so we can see the problem, please.
Colin
Hi Colin,
Updated case available at http://live.datatables.net/nawudoba/5/edit with array of objects.
The problem is we are removing the rows in the loop. Instead we need to keep track of the row indexes in the loop then use
rows().remove()
after the loop with the indexes.http://live.datatables.net/nawudoba/7/edit
Kevin
Thankyou Kevin! worked like a charm!