Events that occur on the page with a Webix-based application fall into three groups:
The main part of the article below concerns inner events of Webix components, while the details of the other two groups can be checked by a link.
Common inner events for all ui components are mouse events (onItemClick, onBeforeContextMenu, onMouseMoving, onMouseOut, etc) since all the components can be clicked (and right-clicked) and respond to a mouse pointer.
Other inner events depend on the component functionality and are provided in API reference for each component.
A handler can be attached to a components in different ways:
1 . The most common way is to use any event of the component (check its API ref) within:
2 . Depending on the component nature, you can attach a handler to some specific events defined by component properties:
Now let's study each possibility in detail.
An attachEvent() method takes the name of the component's inner event and the function it fires (handler) as parameters. The syntax is:
$$("element_id").attachEvent("onSomeEvent", some_function(){...});
Event names are case-insensitive.
A button to collapse the all tree nodes
var myEvent = $$("button_id").attachEvent("onItemClick", function(){
$$("dtree").closeAll();
});
Later on, you can easily detach this event from the component with the help of the opposite method:
$$("button_id").detachEvent(my Event);
You can also define event handlers right in the component constructor within either of the following attributes:
webix.ui({
view:"list",
$init: function(){...},
ready:function(){
this.select(this.getFirstId()); //selects the first item
this.load("data/books2.xml","xml"); //loads data from XML dataset
},
on:{
'onItemClick': function(){
alert("you have clicked an item");
},
'onAfterLoad':function(){ ... }
}
});
When attaching an event you can specify only the name of a function to attach and declare it outside the webix.ui() constructor:
function select_first(){
$$("datalist").select($$("datalist").getFirstId();)
};
webix.ui({
view:"button",
on:{
'onItemClick': select_first
}
});
Related sample: Creating a custom table
For buttons you can make it more compact by using click attribute:
function close_tree(){...};
{ view:"button", id:"sample_button", value:"Close", width:100, click:"close_tree"}
Data scheme allows changing the default pattern of data handling within the component.
For instance, the function specified in its $init key will run each time when data is loaded/reloaded to the component and when the add method is called.
webix.ui({
view:"list",
scheme:{
$init:function(obj){...},
$update:function(obj){...}
}
});
Related sample: Horizontal List
More possibilities of the data scheme are described separately).
Such properties come in pairs and include:
For instance, you need an event that fires each time you click an item in the list and performs an action that depends on the data derived from this item.
$$("mylist").attachEvent("onAfterSelect",function(id){
alert(this.getItem(id).title);
})
Both on_click and onClick can be modified if needed.
1 . With on_click redefining can be done only after the component initialization.
The on_click
grid = new webix.ui({
view:"datatable",
columns:[
{ id:"", template:"<input type='button' class='delbtn' value='Delete'>"},
{id:"title", ...}
],
on:{"onItemClick", function(){...} //default click behavior
},
select:true
});
grid.on_click.delbtn=function(e, id, trg){
webix.message("Delete row: "+id);
return false;//blocks default onclick event
};
Related sample: Datatable: Custom Handler
One of the columns features an HTML template with a button styled with 'delbtn' CSS class. All cells including the one with this button will react on default click behavior while when you click on the button itself rather than on its cell area, the default behaviour will be overridden with the function attached to the 'delbtn' CSS class.
2 . The onClick allows redefining current click behavior right in the component constructor.
webix.ui({
view:"list",
template:"#votes# <span class='info'>Details</span>",
select:"multiselect",
onClick:{
info:function(e, id){
webix.message(this.item(id).title);
return false; /blocks default onclick event
}
},
});
When you click list item styled with info CSS class, the item won't be selected and custom message will appear.
Related sample: List: Event Handlers
The same pattern is true for other pairs of properties.
If you have already attached an event to the component and described the function that this event triggers, you can attach this event to another component in your app.
For these needs, use the mapEvent method and pass a map into it, where the map is an event-object correspondence:
button.mapEvent({
onItemClick:list // where list - some other component
});
As a result, when the button is clicked, the function that was initially attached only to it, will be executed for list as well.
Event Mapping helps to get rid of repetitions in code when one and the same event should be attached to different components.
Inner events make it possible to work with component items provided that the ID of an item you click, select, etc. is passed to the attached function.
For instance, you need an event that fires each time you click an item in the list and performs an action that manipulates its data.
$$("mylist").attachEvent("onAfterSelect",function(id){
alert(this.item(id).title);
});
Related sample: Sizing and events
Events can be divided into two groups:
The function returns false, selection is blocked
$$("my_list").attachEvent("onBeforeSelect", function(){ return false; })
You can also temporarily block all events assosiated with the component by using the blockEvent() method. To re-enable event triggering use the unblockEvent() method.
$$("component_id").blockEvent();
$$("component_id").unblockEvent();
The default value for response to events is 500ms. With such events as onMouseMove and onMouseOut, you can delay the server response for as much time (in milliseconds) as you need:
webix.ui({
view:"menu",
mouseEventDelay:100,
...
});
Components that are in focus at the moment automatically receive the ability to respond to keyboard actions and can take keyboard events, namely
Look how the onTimedKeyPress event works with a text field to filter data in view list:
$$("list_input").attachEvent("onTimedKeyPress",function(){
var value = this.getValue().toLowerCase();
$$("list").filter(function(obj){
return obj.title.toLowerCase().indexOf(value)==0;
});
});
//"list_input" id the ID of the dedicated "text" input control
Related sample: List: Filtering
More information about key codes and hot keys is to be found in the UI Manager article.
Webix global events are not connected with any specific component and can be used to control general application issues (clicks, touch movements, serverside requests, etc). Some of them repeat native DOM events.
The details on Touch Events are given in the related part of API reference
All such events are attached to webix object:
webix.attachEvent("onRotate", function(mode){
..// logic
});
Native DOM events can be handled with the help of Webix event(); and eventRemove() functions that are called from webix object:
var eventId = webix.event("divId", "click", function(e){
do_something();
});
webix.eventRemove(eventId);
Comments: