- Kendo UI Cookbook
- Sagar Ganatra
- 705字
- 2021-12-08 12:47:21
Displaying data from a local or remote DataSource component in a Grid view
A Grid component can be created using the data present in the page or by referring to model data, that is, data in the JSON format. The Kendo UI library provides a DataSource
component that can be used to store a model data. The DataSource
component can be bound to a local data source or to a remote service that returns data in either the XML or JSON format.
How to do it…
As mentioned in the previous recipe, we can either use a table
element or a div
element to construct a Grid:
<div id="grid"> </div>
Now, when we initialize the Grid using the kendoGrid
function, we need to specify the configuration and data for the table:
$("#grid").kendoGrid({ columns: [ { field : 'movieName', title : 'Movie' }, { field : 'year', title : 'Year' }, { field : 'rating', title : 'Rating' } ], dataSource: [ { movieName : 'The Shawshank Redemption', year : 1994, rating : 9.2 }, { movieName : 'The Godfather', year : 1972, rating : 9.2 }, { movieName : 'The Godfather - Part 2', year : 1974, rating : 9.0 }, { movieName : 'Pulp Fiction', year : 1994, rating : 8.9 }, { movieName : 'The Good, the Bad and the Ugly', year : 1966, rating : 8.9 } ] });
In the preceding code snippet, the Grid is being initialized with two objects, namely columns
and dataSource
. The columns
object is a collection of JavaScript objects that specify the column configuration. The configuration contains two properties, field
and title
. The field
property refers to the field in dataSource
that the column is bound to and title
specifies the text to be displayed as a header in the Grid.
The dataSource
object is also a collection of JavaScript objects that contains the data that will be displayed in the Grid. In this example, there are five objects and hence, the Grid will contain five rows.
Note
Please note, the columns
object can be an array of strings as well:
columns: ['movieName', 'year', 'rating']
In this case, the entries in the columns array should match the field name in the dataSource
object. The title for the columns will be the same as the field names.
How it works…
As mentioned previously, we chose a div
element to create a Grid. This element would serve as a container for the Grid. While initializing the Grid, the framework would look at the provided configuration and generate the markup, which will then be inserted inside the div
element. It creates two div elements—one for the header and the other for the content. The header is shown as follows:
<div class="k-grid-header" style="padding-right: 16px;"> <div class="k-grid-header-wrap"> <table role="grid"> <colgroup> <col> <col> <col> </colgroup> <thead> <tr> <th role="columnheader" data-field="movieName" data-title="Movie" class="k-header"> Movie </th> <th role="columnheader" data-field="year" data-title="Year" class="k-header"> Year </th> <th role="columnheader" data-field="rating" data-title="Rating" class="k-header"> Rating </th> </tr> </thead> </table> </div> </div>
Based on the configuration provided in the columns
object, a table that contains the header columns is generated (the thead
and th
tags). To display the Grid data, the library inserts another div
element:
<div class="k-grid-content"> <table role="grid"> <colgroup> <col> <col> <col> </colgroup> <tbody> <tr data-uid="d71ac3fd-9769-4161-9277-432f151490c3" role="row"> <td role="gridcell"> The Shawshank Redemption</td> <td role="gridcell">1994</td> <td role="gridcell">9.2</td> </tr> <tr class="k-alt" data-uid="3240f52d-f4bb-4a2c-9985-b6ce44d75be6" role="row"> <td role="gridcell">The Godfather</td> <td role="gridcell">1972</td> <td role="gridcell">9.2</td> </tr> . . . </tbody> </table> </div>
The preceding code contains a table with rows that display data from the dataSource
object. Now, when you view the Grid on the page, it will be the same as the Grid that was explained in the previous recipe. This is shown in the following screenshot:

There's more…
The dataSource
object in this example contained the local data, that is, the data for the Grid was mentioned right within the JavaScript object. However, in most of the scenarios, the data will be fetched from a remote service. The dataSource
object can be used to fetch data from a remote service by specifying the URL for it:
$("#grid").kendoGrid({ columns: [ { field : 'movieName', title : 'Movie' }, { field : 'year', title : 'Year' }, { field : 'rating', title : 'Rating' } ], dataSource: { transport: { read: 'http://localhost/kendo/code/chapter2/remote.json' } } });
In the preceding code snippet, the DataSource
object has changed. It now contains a reference to the URL of a remote service (under the transport
option). When the Grid is initialized, a GET
request to the specified URL is sent and the data received as a response is then used to populate the content of the Grid.
- VMware View Security Essentials
- Linux C/C++服務(wù)器開發(fā)實踐
- Effective C#:改善C#代碼的50個有效方法(原書第3版)
- Vue.js 3.0源碼解析(微課視頻版)
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優(yōu)化計算
- Python神經(jīng)網(wǎng)絡(luò)項目實戰(zhàn)
- Java Web應(yīng)用開發(fā)技術(shù)與案例教程(第2版)
- 零基礎(chǔ)學(xué)Java程序設(shè)計
- C語言程序設(shè)計
- JavaScript入門經(jīng)典
- Python忍者秘籍
- SQL基礎(chǔ)教程(第2版)
- 零基礎(chǔ)輕松學(xué)C++:青少年趣味編程(全彩版)
- Android高級開發(fā)實戰(zhàn):UI、NDK與安全
- Spring Boot從入門到實戰(zhàn)