Data Source Methods
view()
/*
**returns **the current state of the items in the DataSource with all applied settings such as paging, sorting, filtering, and grouping.to ensure that data is available,
this method should be used from within the change event of the DataSource
*/
change: function( e ) {
// ...
kendo.render(template, dataSource.view());
}
data()
/*
retrieve an observable array of items
(the current data within the DataSource)
DataSource.data method will returns an ObservableArray
which wraps all the items currently in the DataSource
and respectively returned from the server.
That ObservableArray acts as a real JavaScript array.
You can find the number of items in it: length property
and you can "unwrap" the data to its original state
by calling toJSON method.
*/
var movies = dataSource.data();
read()
/* Kendo.data.js - 1627 */
/*
read data into the DataSource using the transport.read setting
The read method always makes a request to the remote service,
unless the Data Source is offline.
Optional data to pass to the remote service. If you need
to filter,
it is better to use the filter() method or the query()
method with a filter parameter.
It calls JQuery Ajax function
*/
var optionalData = { foo: 42, bar: "baz" };
dataSource.read(optioanlData);
// "read()" will fire the "change" event
// of the dataSource and the widget will be bound
/*
also conveniently causes the change event to fire
difference between read and fetch
The read method is used in cases when you want latest data
from the server side.
On the other hand fetch is used to initially populate
the data source.
Any subsequent calls return the initial data.
*/
fetch()
/*
Reads the data items from a remote service
(if the transport option is set) or from a JavaScript array (if the data option is set).
fetches data using the current filter/sort/group/paging information.
will fetch data from transport if data is not already available inmemory.
*/
dataSource.fetch();
/*
can optionally take a callback function
which is executed once the data is ready.
it returns a promise
*/
dataSource.fetch(function () {
// do something
}
)
dataSource.fetch().then(function () {
// do something
}
)
query()
/*
Executes the specified query over the data items.
Makes a HTTP request if bound to a remote service.
Executes a query over the data (i.e.
paging/sorting/filtering/grouping)
this effects what the call to dataSource.view() will return.
*/
dataSource.query({
page: 5,
pageSize: 20,
group:{field:'year',dir:'asc'
}});
/*
iterate over a grids datasource in it's sorted state
you could achieve this using kendo.data.Query which
allows you to manipulate the data separately
from the dataSource itself.
*/
var dataSource = $("#grid").data("kendoGrid").dataSource;
var allData = dataSource.data();
var query = new kendo.data.Query(allData);
var data = query.sort({ field: "UnitPrice", dir: "asc" }).data;
sync()
/*
synchronizes changes through the transport for
any pending CRUD operations.
if batch mode is enabled, it uses only one call per
operation type (create, read, update, destroy)
*/
dataSource.sync();
set()
//get ref to Grid DataSource instance
var gridDataSource = $("#grid").data("kendoGrid").dataSource;
//get first row data item - using `view` method
var firstRowData = gridDataSource.view()[0];
//change the value of `text` fied using `set` method so
// tha the data source triggers `change` event
firstRowData.set("text", "fooo bar");
get()
isNew, check if the model is new or not. This is determined by whether or not the id field is still set at the default value.
toJSON, return a JSON representation of the complete model's properties and values.
group()
```
// get the current group descriptors
var g = dataSource.group();
```
filter
```
// set a new value for filtering
dataSource.filter({ field: 'year', operator: 'gt', value: 1990 });
```
add()
```javascript
// add a new item
dataSource.add({ year: 1997, title: 'The Fifth Element', rating: 10
})
```
insert
// insert an item at the 6th position in the DataSource
dataSource.insert(5, {year: 1995, title: 'Twelve Monkeys', rating
9.5});
remove()
// remove an item from the DataSource
var movie = dataSource.at(5);
dataSource.remove(movie);
at/get/getByUid
// get the 3rd item in the DataSource
var movie = dataSource.at(2);
// get the model instance with an id of 5
// (id is determined by the value of the schema.model.id property)
var movie = dataSource.get(5);
// get the model instance, or ObservableObject if no
// model has been set
// uid is a property inherited from ObservableObject
varuid = $("tr").data("uid");
var movie = dataSource.getByUid(uid);
cancelChanges()
```
// discards all un-synced changes made to the DataSource
dataSource.cancelChanges();
```
```javascript
// set the DataSource to some new data
datSource.data([{year: 2009, title: 'Cargo', rating: 6.8}, {year: ...
]);
```
Iteration()
// Interate the dataSource
var dataSource = $("#grid").data("kendoGrid").dataSource;
// data() function is used to get and set data of the
Kendo data source.
var datasourcedata = dataSource.data()
// iterate or modify the data
for (var i = 0; i < datasourcedata.length; i++) {
var dataitem = datasourcedata[i].myField;
// modify data
datasourcedata[i].myField = 'some value';
}
// refresh grid
$("#grid").data("kendoGrid").setDataSource(dataSource);
$("#grid").data("kendoGrid").dataSource.refresh();
/*
1. get an array to iterate over all data: dataSource.data()
2. access a specific item: dataSource.at(1)
3. get filtered data: datasource.view()
4. get a pure JS array back: dataSource.data().toJSON()
*/
total()
/* get, but not set, the total number of items in the
DataSource
*/
var total = dataSource.total();
totalPages()
/*
get, but not set, the total number of pages of items
in the DataSource
*/
var pages = dataSource.totalPages();