title | slug | tags | Publish | related |
---|---|---|---|---|
Using Kendo Widgets |
gs-using-kendo-widgets |
101, Getting Started, widgets |
true |
gs-downloading-kendo |
Since Kendo UI is built on top of jQuery, using the Kendo UI widgets is very intuitive. For example, to use Kendo AutoComplete, first you write some HTML:
<input id="myAutoComplete" />
Then, to initialize this HTML input and give it the Kendo AutoComplete functionality and styling, a single jQuery JavaScript line is required:
$("#myAutoComplete").kendoAutoComplete(["Apples", "Oranges", "Grapes"]);
To set additional configuration on widgets you can provide a JavaScript object to the widget constructor.
$("#customers").kendoGrid({
columns:[
{
field: "FirstName",
title: "First Name"
},
{
field: "LastName",
title: "Last Name"
}],
dataSource: {
data: [
{
FirstName: "Joe",
LastName: "Smith"
},
{
FirstName: "Jane",
LastName: "Smith"
}]
}
});
The specific property names and configuration options for each widget can be found in the API reference section of this site.
The widget client object is preserved in the jQuery data store for the corresponding element. The following code snippet shows how to get the client object of the grid widget.
var customers = $("#customers").data("kendoGrid");
The name of the data key is built by adding "kendo" prefix to the name of the type of widget being referenced
There are two ways to subscribe to Kendo UI widget events:
- Provide a handler in the configuration during widget initialization
- Using the bind method
$("#products").kendoAutoComplete({
change: onChange,
close: onClose,
open: onOpen
});
var autoComplete = $("#products");
autoComplete.bind("change", onChange);
The handler is a callback function, as shown below. Within the handler, the keyword this
refers to the Kendo widget to which the handler is subscribed.
<script>
function onChange(args) {
//"this" refers to the AutoComplete widget that triggers the event
console.log(this.value()); // outputs the value of the widget
}
</script>
In addition to the jQuery plugin initialization, each kendo widget can be initialized and configured by setting a role
data attribute
on the target element and calling kendo.init
on its element or any of its containers.
<div id="container">
<select data-role="dropdownlist"></select>
</div>
<script>
kendo.init($("#container"));
</script>
The role attribute value is the name of the widget in lower case.
Each widget configuration option can be specified via a data attribute on the respective element.
Camel-cased options are translated to dash separated attributes. For instance, dataTextField option of the autocomplete widget can be specified with data-text-field="foo"
attribute.
Options, which start with
data
do not require an additional "data" in the attribute name.
<select data-role="dropdownlist" data-delay="1000">
</select>
A widget event can be bound using data attributes as well. The value of the data attribute will be resolved to a function, available in the global scope.
<select data-role="dropdownlist" data-change="onChange">
</select>
<script>
function onChange(e) {
// ...
}
</script>
Event handlers also support member functions, using JavaScript syntax. An event handler can be specified using foo.bar
, resolving to the member method bar
of the object foo
.
<select data-role="dropdownlist" data-change="foo.bar">
</select>
<script>
var foo = {
bar: function(e) {
// ...
}
}
</script>
The declarative DataSource binding works in the same way, expecting a DataSource object or an array.
<select data-role="dropdownlist" data-source="source">
</select>
<script>
var source = ["foo", "bar", "baz"];
</script>
For widgets that have template configuration options, the data attribute value will be resolved to an id
of a script
element, with the contents of the template.
<select data-role="dropdownlist" data-template="foo"></select>
<script id="foo" type="script/x-kendo-template">
<span>#:data.Name</span>
</script>