Using columns().data() api with ajax sourced JSON data in jquery datatables
Using columns().data() api with ajax sourced JSON data in jquery datatables

I want to get unique values from a column of datatables using columns().data() api and store it in an array.
Datatables code:
table = $('#example').DataTable({
processing: true,
ajax: {
dataSrc: "",
url: "api.php?type=1&dmid="+dmid+"&mindate="+min_date+"&maxdate="+max_date,
type: "GET"
},
columns: [
{ data: "id" },
{ data: "receipt_num" },
.
.
.
{ data: "amount" }
],
columnDefs: [
{ "visible": false, "targets": 2 }
],
dom: 'Bflrtip',
scrollX: true,
colReorder: true
});
example:
table.columns(2).data().unique().sort().each(function(value, index) {
console.log(value, index);
});
The above example works when i dont use ajax sourced data but when i use ajax there is no output in console.
As the column i want values from is hidden i am unable to use js/jquery to get column data that is why i was trying to use columns() api.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Hi @kunalpatel00750 ,
It might be because the data is still loading - the Ajax data is loaded asynchronously. Try adding those lines inside
initComplete
, so it will be called once the data is fully loaded and the table initialised.Cheers,
Colin
@colin Damn thanks man it worked