XML Format
<book cover="javascript-the-good-parts.png">
<title>JavaScript: The Good Parts</title>
<author>Douglas Crockford</author>
<url>http://www.amazon.com/…</url>
</book>
Note: Security restrictions in Chrome prevent this example from working when the page is loaded from the file system (via file:// protocol).
- Description
- View Code
- Configuration (14)
- Methods (14)
- Events (2)
The DataSource component is an abstraction for using local (arrays of JavaScript objects) or remote (XML, JSON, JSONP) data. It fully supports CRUD (Create, Read, Update, Destroy) data operations and provides both local and server-side support for Sorting, Paging, Filtering, Grouping, and Aggregates.
It is a powerful piece of the Kendo UI Framework, dramatically simplifying data binding and data operations.
Getting Started
Creating a DataSource bound to local data
var movies = [ {
title: "Star Wars: A New Hope",
year: 1977
}, {
title: "Star Wars: The Empire Strikes Back",
year: 1980
}, {
title: "Star Wars: Return of the Jedi",
year: 1983
}
];
var localDataSource = new kendo.data.DataSource({data: movies});Creating a DataSource bound to a remote data service (Twitter)
var dataSource = new kendo.data.DataSource({
transport: {
read: {
// the remote service url
url: "http://search.twitter.com/search.json",
// JSONP is required for cross-domain AJAX
dataType: "jsonp",
// additional parameters sent to the remote service
data: {
q: "html5"
}
}
},
// describe the result format
schema: {
// the data which the data source will be bound to is in the "results" field
data: "results"
}
});Binding UI widgets to DataSource
Many Kendo UI widgets support data binding, and the Kendo UI DataSource is an ideal binding source for both local and remote data. A DataSource can be created in-line with other UI widget configuration settings, or a shared DataSource can be created to enable multiple UI widgets to bind to the same, observable data collection.
Creating a local DataSource in-line with UI widget configuration
$("#chart").kendoChart({
title: {
text: "Employee Sales"
},
dataSource: new kendo.data.DataSource({
data: [
{
employee: "Joe Smith",
sales: 2000
},
{
employee: "Jane Smith",
sales: 2250
},
{
employee: "Will Roberts",
sales: 1550
}]
}),
series: [{
type: "line",
field: "sales",
name: "Sales in Units"
}],
categoryAxis: {
field: "employee"
}
});Creating and binding to a sharable remote DataSource
var sharableDataSource = new kendo.data.DataSource({
transport: {
read: {
url: "data-service.json",
dataType: "json"
}
}
});
// Bind two UI widgets to same DataSource
$("#chart").kendoChart({
title: {
text: "Employee Sales"
},
dataSource: sharableDataSource,
series: [{
field: "sales",
name: "Sales in Units"
}],
categoryAxis: {
field: "employee"
}
});
$("#grid").kendoGrid({
dataSource: sharableDataSource,
columns: [
{
field: "employee",
title: "Employee"
},
{
field: "sales",
title: "Sales",
template: '<#= kendo.toString(sales, "N0") #>'
}]
});No code
-
aggregate(val) : ArrayGet current aggregate descriptors or applies aggregates to the data.Example
dataSource.aggregate({ field: "orderId", aggregate: "sum" });Parameters
-
val: Object|Array (optional) Aggregate(s) to be applied to the data.
- Returns
- Array Current aggregate descriptors
-
-
aggregates() : ArrayGet result of aggregates calculation- Returns
- Array Aggregates result
-
data(value) : ArrayGet data return from the transportParameters
-
value
- Returns
- Array Array of items
-
-
fetch()Fetches data using the current filter/sort/group/paging information. If data is not available or remote operations are enabled data is requested through the transport, otherwise operations are executed over the available data. -
filter(val) : ArrayGet current filters or filter the data.Supported filter operations/aliases are:
- Equal To: "eq", "==", "isequalto", "equals", "equalto", "equal"
- Not Equal To: "neq", "!=", "isnotequalto", "notequals", "notequalto", "notequal", "not", "ne"
- Less Then: "lt", "<", "islessthan", "lessthan", "less"
- Less Then or Equal To: "lte", "<=", "islessthanorequalto", "lessthanequal", "le"
- Greater Then: "gt", ">", "isgreaterthan", "greaterthan", "greater"
- Greater Then or Equal To: "gte", ">=", "isgreaterthanorequalto", "greaterthanequal", "ge"
- Starts With: "startswith"
- Ends With: "endswith"
- Contains: "contains", "substringof"
Example
dataSource.filter({ field: "orderId", operation: "eq", value: 10428 }); dataSource.filter([ { field: "orderId", operation: "neq", value: 42 }, { field: "unitPrice", operation: "ge", value: 3.14 } ]);Parameters
-
val: Object|Array (optional) Filter(s) to be applied to the data.
- Returns
- Array Current filter descriptors
-
group(val) : ArrayGet current group descriptors or group the data.Example
dataSource.group({ field: "orderId" });Parameters
-
val: Object|Array (optional) Group(s) to be applied to the data.
- Returns
- Array Current grouping descriptors
-
-
page(val) : NumberGet current page index or request a page with specified index.Example
dataSource.page(2);Parameters
-
val: Number (optional) The index of the page to be retrieved
- Returns
- Number Current page index
-
-
pageSize(val) : NumberGet current pageSize or request a page with specified number of records.Example
dataSource.pageSiza(25);Parameters
-
val: Number (optional) The of number of records to be retrieved.
- Returns
- Number Current page size
-
-
query(options)Executes a query over the data. Available operations are paging, sorting, filtering, grouping. If data is not available or remote operations are enabled data is requested through the transport, otherwise operations are executed over the available data.Example
// create a view containing at most 20 records, taken from the // 5th page and sorted ascending by orderId field. dataSource.query({ page: 5, pageSize: 20, sort: { field: "orderId", dir: "asc" } }); // moves the view to the first page returning at most 20 records // but without particular ordering. dataSource.query({ page: 1, pageSize: 20 });Parameters
-
options: Object (optional) - Contains the settings for the operations. Note: If setting for previous operation is omitted, this operation is not applied to the resulting view
-
-
read(additionalData)Read data from transportParameters
-
additionalData
-
-
sort(val) : ArrayGet current sort descriptors or sorts the data.Example
dataSource.sort({ field: "orderId", dir: "desc" }); dataSource.sort([ { field: "orderId", dir: "desc" }, { field: "unitPrice", dir: "asc" } ]);Parameters
-
val: Object|Array (optional) Sort options to be applied to the data
- Returns
- Array Current sort descriptors
-
-
total()Get the total number of records -
totalPages() : NumberGet the number of available pages.- Returns
- Number Number of available pages.
-
view() : ArrayReturns a view of the data with operation such as in-memory sorting, paring, grouping and filtering are applied. To ensure that data is available this method should be use from within change event of the dataSource.Example
dataSource.bind("change", function() { renderView(dataSource.view()); });- Returns
- Array Array of items