Available only in PRO Edition
Since 5.1
The widget is available in the Webix Pro edition.
Webix GridLayout widget makes it easier for you to build a graceful and balanced page structure. It gives you a ready scheme for arranging elements, thus saving your time in creating a nicely organized web application.
GridLayout is based on Webix Layout and possesses the idea of arranging content from DataTable. It divides a page into rows and columns and uses grid cells for aligning views in the layout. Besides, it provides the possibility to save and restore the state of elements on a page.
To initialize GridLayout on a page, make use of the code below:
webix.ui({
view:"gridlayout",
gridColumns:4,
gridRows:3,
cells:[
{ template:"Single", x:0, y:0, dx:1, dy:1 },
{ template:"Wide 1", x:1, y:0, dx:2, dy:1 },
{ template:"Wide 2", x:0, y:1, dx:2, dy:1 },
{ template:"Square", x:2, y:1, dx:2, dy:2 }
]
});
GridLayout contains a set of cells, each of which presents an object with the properties that define the content, location and size of a cell. Each cell can contain a template or a view inside. The list of available cell properties is given below:
GridLayout provides a range of configuration options that will help you to achieve the desired look and feel.
By default, the grid of GridLayout includes 2 grid rows and 2 grid columns. You can change these grid settings via the gridRows and gridColumns options:
webix.ui({
view:"gridlayout",
gridColumns:4,
gridRows:3,
cells:[
{ template:"Single", x:0, y:0, dx:1, dy:1 },
{ template:"Wide 1", x:1, y:0, dx:2, dy:1 }
// more cells
]
});
You can specify the necessary width and height of grid cells in pixels via the cellHeight and cellWidth options:
webix.ui:({
view:"gridlayout",
id:"grid",
gridColumns:4, gridRows:3,
cellHeight: 150, cellWidth: 200,
cells:[
{ id:"a", template:"Single", x:0, y:0, dx:1, dy:1 },
{ id:"b", template:"Wide 1", x:1, y:0, dx:2, dy:1 }
// more cells
]
});
You can define proper margin and padding values to get the necessary alignment of cells in the grid. By default, the value of both margin and padding is 10.
It is also possible to change the horizontal and vertical padding separately with the help of the paddingX and paddingY properties.
webix.ui({
view:"gridlayout",
gridColumns:4, gridRows:3,
margin:20,
// paddingY:20
padding 20,
cells:[
{ template:"Single", x:0, y:0, dx:1, dy:1 },
{ template:"Wide 1", x:1, y:0, dx:2, dy:1 },
// more cells
]
});
GridLayout provides a set of API methods to operate the inner views. You can add/remove views and move them to new positions.
You can add a new view into a gridlayout via the addView method. It may take two parameters:
$$("grid").addView({ template:"Column", x:1, y:1, dx:1, dy:1 });
Related sample: Grid Layout API
To remove an unnecessary view, apply the removeView method. It takes either a child view object or its id as an argument:
webix.ui({
view:"gridlayout",
id:"grid",
cells:[
{ id:"a", template:"Single", x:0, y:0, dx:1, dy:1 },
{ id:"b", template:"Wide 1", x:1, y:0, dx:2, dy:1 }
// more cells
]
});
$$("grid").removeView("a");
Related sample: Grid Layout API
You can also remove all views from a gridlayout with the help of the clearAll method:
$$("grid").clearAll();
Related sample: Grid Dashboard - Saving State
To change the location and sizes of a view, make use of the moveView method. You need to pass two parameters to it:
$$("grid").moveView("d", { x:0, y:0, dx:2, dy:2 });
Related sample: Grid Layout API
Webix GridLayout enables you to store/restore the state of a layout to a cookie, a local or session storage.
For these needs you should store the configurations of all widgets within gridlayout cells to recreate them multiple times.
var widgets = {
view:"list", id:"list",
width:200,
drag:true,
template:"#value# - (#dx#x#dy#)",
data:[
{ id:"a", value:"Widget A", dx:1, dy:1 },
{ id:"b", value:"Widget B", dx:1, dy:2 },
{ id:"c", value:"Widget C", dx:2, dy:1 },
// other cells
]
};
webix.ui({
type:"space", cols:[
{ view:"scrollview", body:grid },
widgets
]
});
Then you can initialize gridlayout using these configuration objects with the help of the function defined by the factory configuration option. It takes one parameter:
webix.ui({
view:"gridlayout",
id:"grid",
gridColumns:4, gridRows:4,
factory:function(obj){
// your logic here
}
});
After that you are ready to work with the gridlayout state.
To save the current state of a gridlayout to the local storage, you should call the serialize method as in:
var state = $$("grid").serialize(serializer);
webix.storage.local.put("grid-dashboard-state", state);
As a parameter the serialize() method may take the serializer function that contains the serialization logic of each particular component within a gridlayout body.
To recreate the cells content, you need to get the state object by its name from the storage and call the restore method of GridLayout. It takes two parameters:
var state = webix.storage.local.get("grid-dashboard-state");
$$("grid").restore(state,factory);
The factory function declared above implements the logic of creating cells. It will return a view from the existing configuration (here configurations are stored in the widgets object) to which it will point by the id.
Related sample: Grid Dashboard - Saving State
You can catch changes made in a gridlayout, such as adding, removing or moving of views and handle them with the help of the onChange event handler.
$$("grid").attachEvent("onChange",function(){
// webix.message("A view has been moved")
});
Back to top