columns().data() returns empty arrays
columns().data() returns empty arrays
rugggger
Posts: 23Questions: 6Answers: 1
I am trying to use table.columns().data() to get an array containing my table but I am getting empty arrays.
The following code
console.log('table ',t);
console.log('table.columns().data() ', t.columns().data());
console.log('table.column(0).data() ', t.column(0).data());
outputs the arrays to the console. They are all empty although the table is displayed on the page and contains several rows.
Link : http://app.meafood.org.il/%D7%9C%D7%A7%D7%95%D7%97%D7%95%D7%AA/
Thank you,
Yaron
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You are using Ajax - which means that the data is fetched asynchronously. When the
columns().data()
code runs, the data hasn't been loaded yet!Use
initComplete
to know when the data has been loaded.Allan
Oops, sorry for that.
Here I updated the code. When clicking on the 'ADD' button I want to get the sorted array of the 'ID' column. I get it but it is sorted as a string, although it is all integers, and I try to force it to 'type': num as well.
Clicking on ADD will print the data to the console
"1","10","11","12","13","14","15","16","17","18","19","2","20" and so on..
http://app.meafood.org.il/%D7%9C%D7%A7%D7%95%D7%97%D7%95%D7%AA/
Thanks,
Yaron
In the data source it is a string:
Which is why it is string sorting. The
type
has no effect on whatsort()
does - that is only used when the table itself does the sorting.Ideally you would change your data source to have integers.
Alternatively you can specify a sorting function for
sort()
just like you can withArray.prototype.sort()
.Allan
I don't understand why the data source is a string...
In the SQL table the column is defined as INT(10) Unsigned Auto Increment.
The field contains a number in all its rows.
What would make DataTables view it as a string?
Yaron
DataTables isn't making it a string - it is a string in the JSON data source that is being given to DataTables:
Note:
"id":"1"
.I don't know why the
clientsapi.php
script is giving it back as a string, but I'd suggest you need to cast the value in that script if you want it to be an integer.Allan
The clientsapi.php is as follows:
How can I set 'id' as a numeric field type ?
Is it done with getFormatter ? Couldn't find the info..
Thanks
Yaron
Yes, you could use:
Allan