Collections and Server Side
.. in Webix Apps
Invisible Master is Watching You
Abstract and invisible
'viewless'
Painted
in UI,
'view'
DataCollection
TreeCollection
DataTable
List, DataView
Chart
TreeTable, Tree
Invisible Master is Ruling You
var collection = new webix.DataCollection({
data:grid_data
});
webix.ui({
rows:[
{view:"list", id:"list"},
{view:"datatable", id:"table"}
]
});
$$("list").sync(collection);
$$("table").sync(collection);
Edit: master collection
Add: master collection
Remove: master collection
slave views
slave views
slave views
Why Collection?
$$("list").sync(collection);
$$("table").sync(collection, function(){
this.filter(function(obj){
return obj.rank%2;
});
});Collections
Same data, but different presentation
var users = webix.DataCollection({
url:"users.js"
});list.sync(users);chart.sync(users, function(){
this.group({
by:"year",
map:[
"age":[ "age", "count"]
]
})
});Using Collections
$$("table1").sync(categories);
//or
$$("table1").parse(categories);
//or even
{ view:"datatable", data:categories }{ view:"datatable", columns:[
{ id: "title" },
{ id: "categoryId", collection:categories }
]}var categories = new webix.DataCollection({
url:"categories.json" //[ { id:1, value:"" }, { id:2, value:"" } ]
});{ view:"combo", options:categories }webix.ui({
view:"datatable",
url:"server/getfilms",
save:"server/savefilms"
});Simple, yep?)
View
DataStore
DataProcessor
shows data,
lets users interact with it
stores data and its order,
provides CRUD and processing logic
listens to data changes and invokes server scripts
{view:"datatable", id:"table"}$$("table").data{ view:"datatable", save:"savefilms"}* There also exist more complex ways of DataProcessor initialization
Request
Response
Any parameters in URL or request body,
normally it's GET request
Try it (F12, Network tab): https://snippet.webix.com/c1o1ij2l
{ view:"list", url: "some_script" }
//or
list.load("some_script");{
data:[ .. ],
pos:0,
total_count:10000
}GET some_script
GET some_script?start=50&count = 50
GET some_script
GET some_script?parent=2
{ data:[ .. ], parent:2 }{ data:[
{ id:1, value:"Some" }
{ id:2, value:"Other", webix_kids:true }
]}list.clearAll();
//filter
list.load("some_script?filter[title]=a");
//sort
list.load("some_script?sort[title]=desc");Datatable
General
{ id:"title", { content:"serverFilter" }, sort:"server" },
{ id:"categoryId", { content:"serverSelectFilter" }, sort:"server"}Request
Response
Try it (F12, Network tab): https://snippet.webix.com/m1it5kfh
{ view:"list", save: "some_script" } { view:"list", save: "rest->some_script" } { view:"list", save: "some_script" }webix.ui({
view:"datatable",
on:{
"data->onStoreUpdated":function(id, item, operation){
//id: 1
//item: { id:1, title:"Some", year:1957 }
//operation: add, update, delete
}
},
save:"some_script"
});id:1,
title:"Some",
year:1957,
votes:1254,
rating:6,
webix_operation:"update" || "delete"id:4587121254545,
title:"Some",
year:1957,
votes:1254,
rating:6,
webix_operation:"insert"Request POST "some_script"
{ "id":"1", "status":"success" }{ "id":4587121254545,
"status":"success", "newid":99 }Response
webix.ui({
view:"datatable",
save:"rest->some_script"
});
id:1,
title:"Some",
year:1957,
votes:1254,
rating:6,
rank:1Add: POST "some_script"
Update: PUT "some_script/1"
Delete: DELETE "some_script/1"
...same onStoreUpdated event
{ view:"list", id:"mylist", save: {
url:"some_script",
trackMove:true
}}dp config and events
dp methods
dp init: save
var dp = new webix.DataProcessor({
url:"some_script",
trackMove:true,
master:$$("mylist")
});
{ view:"list", save: {
url:"some_script",
on:{
onAfterSync:handler,
onBeforeDataSend:handler
}
} var dp = webix.dp($$("mylist"));
//silent modifications
dp.ignore(function(){
$$("mylist").add({});
});dp init: object
Webix Topics:
Practice task: click here
Follow-up text: click here