To export data from a data component into a CSV document, call the toCSV method. The method takes 2 parameters:
For example, this will export a datatable to a CSV document:
webix.ui({
view:"datatable",
id: "myDataTable",
// list configuration
...
});
webix.toCSV($$("myDataTable"));
By default: - The toCSV method returns all data specified in the dataset of a component or in the columns parameter of the DataTable view. - The data are exported into a CSV document named "Data".
You can change the default behavior and add some other settings.
You can
webix.toCSV($$("table"), {
filename: "table"
});
webix.toCSV($$("table"), {
filterHTML:true
});
You can provide an object with column ids as keys. Their values can be either boolean for default parameters (taken from component configuration) or object for custom parameters:
webix.toCSV($$("table"), {
columns:{
"rank":true,
"title":{ header:"Title", width:250}
}
});
With such a notation the order of the columns in the resulting file is defined automatically with a for.. in loop. Sometimes it may be unreliable, e.g.:
webix.toCSV($$("table"), {
columns:{
"rank":true,
"title":{ header:"Title", width:250},
"2016":{ header:"Votes in 2016"},
"2015":{ header:"Votes in 2015"}
}
});
The order in the export file will be: "2015", "2016", "rank", "title" since numbers are processed first and come in the ascending order.
To ensure the strict order of columns in the resulting file, you can specify the columns as a plain array:
webix.toCSV($$("table"), {
columns:[
{ id:"rank" },
{ id:"title", header:"Title", width:250},
{ id:"2016", header:"Votes in 2016"},
{ id:"2015", header:"Votes in 2015"}
]
});
Note that if you want to get the default column's parameters in this case, you should specify only the column's id (see the "rank" column).
webix.toCSV($$("table"), {
columns:{
"title":{header: "Title", template: webix.template("#id#.#name#")},
"year":{header: "Year"}
}
});
The column will be rendered with the stated additional properties, which may differ from the component's parameters.
webix.ui({
rows:[
{
view:"datatable",
data:grid_data,
columns:[
{id:"title", fillspace:true, template:"Film #title#"}
]
}
]
});
webix.toCSV($$("$datatable1"),{
columns:{
rawValues:false
}
});
webix.toCSV($$("table"), {
columns:{
Custom1:{
template:function(obj){
return "Year " + obj.year;
},
header:"Year"
},
//other columns
title:true
}
});
"Custom1" (any name can be used) receives data from the year field even if it is not seen in this component but is presented in its dataset. The field is rendered with template and header that will be the header of the corresponding column in the resulting table.
webix.toCSV($$("table"), {
hidden: true
});
The hidden columns/rows will be visible in the exported file.
webix.csv.delimiter.cols = ",";
webix.toCSV($$("table"), {
download:false
}).then(function(blob){
//process raw data
});
webix.toCSV($$("table"), {
ignore: { "votes":true, "rating":true }
});
webix.toCSV($$("table"), {
header:false,
footer:false
});
webix.toCSV($$("data"), {
filter:function(obj){
var found = sel.find(function(item){
return item.id == obj.id;
});
return found;
}
});
Back to top