Reputation: 1010
I have webix datatable having 100 rows. Now, assume that when I apply a filter on a particular column, the datatable size reduces and renders 10 matched rows.
I am using serialize() to take the whole table data to a JSON object like below:
var data = {};
var $$mytab = $$('myDataTable');
data = $$mytab.serialize();
Now, the problem is, when the entire table is filtered and showing only 10 rows out of total 100 rows, then the serialize() giving me only those 10 rows into the data object instead of 100.
Is there a way, I can always serialize the entire datatable (i.e 100 rows in this case) irrespective of it is showing filtered data or the entire table ?
After implementing your solution ...
It is giving me the same array in both case when I print them in the browser console with a slight difference in the format.
With your solution, it is giving below output on browser console :
(2) [1497258960089, 1497258960162]
and with mine , as below with values in detail, I can view them if I expand (>) the output :
(2) > [Object, Object]
Earlier when I was using serialize, then the entire structure (with their values) were getting saved into the array. So that later on I can iterate a loop on it and match using 'in' operator to delete a value.
Please consider following code:
mydata = {};
mydata.employee = $$mytab.serialize();
console.log(JSON.stringify(mydata.employee);
for (var i = 0; i < mydata["employee"].length; i++) {
if("salary" in mydata["employee"][i]) {
delete mydata["employee"][i]["salary"];
}
}
In the above code I get following output when I do console.log on mydata.employee :
[{"name":"mark", "company":"abcd", "salary":"100"},{"name":"smith", "company":"pqrs", "salary":"200"}]
However if I use below code as you suggested:
mydata = {};
mydata.employee = [];
$$mytab.eachRow(function(item){mydata.employee.push(item);}, true);
console.log(JSON.stringify(mydata.employee);
/* Consider the same forloop in this case as above */
This one is just giving me : [1497258960089,1497258960162]
and throws an error as :
Uncaught TypeError: Cannot use 'in' operator to search for 'salary' in 1497258960089
How can I achieve the same thing in your case as well?
Upvotes: 1
Views: 770
Reputation: 2192
For datatable you can use eachRow
method with true
as second argument.
To have objects array :
var data = [];
var datatable = $$('my_table')
datatable.eachRow(function(item_id) {data.push(datatable.getItem(item_id));}, true);
For treetable, solution we use is to iterate over data using this low level method :
var treetable = $$('my_table')
treetable.data.each(callback, treetable, true);
To have objects array :
var data = [];
var treetable = $$('my_table')
treetable.data.each(function(item) {data.push(item);}, treetable, true);
Upvotes: 1