第一步
刚刚开始接触Kendo UI for jQuery? 免费下载30天试用版
本指南演示了如何通过添加资源并初始化Kendo UI Grid来开始使用Kendo UI for jQuery 。
该过程分为以下几个步骤:
注意:Visual Studio Code 的用户可以使用 Kendo UI 的工具来加快项目创建速度。
1.下载控件
登录后,您可以快速下载 Kendo UI for jQuery 控件。
2. 添加所需的 JavaScript 和 CSS 文件
首先。您需要将 Kendo UI 资源添加到 HTML 文档中。在此示例中,您将使用。在其他情况下,您可能会有的需求。
您需要在 Kendo UI 脚本之前注册 jQuery,否则,当您尝试初始化 Kendo UI 组件或使用 Kendo UI API 时,您将收到Javascript错误提醒。
为了确保加载所有脚本,请您使用API 调来呈现。
提示:该$(function() { });代码块是一个j处理程序。本指南中的所有 JavaScript 代码都必须放入。
```html <link href="//kendo.cdn.telerik.com/themes/6.4.0/default/default-main.css" rel="stylesheet" /> <script src="//code.jquery.com/jquery-1.12.3.min.js"></script> <!-- Add the Kendo library by either using the JAVASCRIPT MODULES --> <script src="//kendo.cdn.telerik.com/2023.2.606/mjs/kendo.all.js" type="module"></script> <!-- OR by using the BUNDLED JAVASCRIPT --> <script src="//kendo.cdn.telerik.com/2023.2.606/js/kendo.all.min.js"></script> ```
3. 将网格绑定到数据
现在,您将使用虚拟订单来创建一个,稍后可以使用这些虚拟订单来绑定网格。
硬编码的数据项数组是创建数据源的最简单方法。在现实场景中,您需要获取或者配置。
为了保证数据操作的正确执行,您需要配置。此外,您将通过和按照OrderDate进行默认。
var orderData = [ { OrderID: 1, OrderDate: "2017-11-06T12:00:00", Freight: 12.34, ShipCity: "Antwerp", ShipCountry: "Belgium" }, { OrderID: 2, OrderDate: "2019-03-02T12:00:00", Freight: 23.45, ShipCity: "Singapore", ShipCountry: "Singapore" }, { OrderID: 3, OrderDate: "2019-06-26T12:00:00", Freight: 34.56, ShipCity: "Shanghai", ShipCountry: "China" }, { OrderID: 4, OrderDate: "2017-09-20T12:00:00", Freight: 45.67, ShipCity: "Hamburg", ShipCountry: "Germany" }, { OrderID: 5, OrderDate: "2017-12-12T12:00:00", Freight: 56.78, ShipCity: "Mumbai", ShipCountry: "India" }, { OrderID: 6, OrderDate: "2018-02-08T12:00:00", Freight: 67.89, ShipCity: "Shanghai", ShipCountry: "China" }, { OrderID: 7, OrderDate: "2018-05-05T12:00:00", Freight: 78.90, ShipCity: "Tokyo", ShipCountry: "Japan" }, { OrderID: 8, OrderDate: "2019-08-03T12:00:00", Freight: 89.01, ShipCity: "Port Klang", ShipCountry: "Malaysia" }, { OrderID: 9, OrderDate: "2018-10-29T12:00:00", Freight: 90.12, ShipCity: "Rotterdam", ShipCountry: "Netherlands" }, { OrderID: 10, OrderDate: "2018-01-23T12:00:00", Freight: 10.32, ShipCity: "Vancouver", ShipCountry: "Canada" }, { OrderID: 11, OrderDate: "2018-04-17T12:00:00", Freight: 21.43, ShipCity: "Colón", ShipCountry: "Panama" }, { OrderID: 12, OrderDate: "2017-07-11T12:00:00", Freight: 32.54, ShipCity: "Manila", ShipCountry: "Philippines" }, { OrderID: 13, OrderDate: "2017-10-24T12:00:00", Freight: 43.65, ShipCity: "Singapore", ShipCountry: "Singapore" }, { OrderID: 14, OrderDate: "2018-03-11T12:00:00", Freight: 54.76, ShipCity: "Busan", ShipCountry: "South Korea" }, { OrderID: 15, OrderDate: "2018-06-17T12:00:00", Freight: 65.87, ShipCity: "Shenzhen", ShipCountry: "China" }, { OrderID: 16, OrderDate: "2018-10-13T12:00:00", Freight: 76.98, ShipCity: "Hong Kong", ShipCountry: "China" }, { OrderID: 17, OrderDate: "2019-04-19T12:00:00", Freight: 87.09, ShipCity: "Dubai", ShipCountry: "UAE" }, { OrderID: 18, OrderDate: "2019-07-25T12:00:00", Freight: 98.21, ShipCity: "Felixstowe", ShipCountry: "UK" }, { OrderID: 19, OrderDate: "2017-09-22T12:00:00", Freight: 13.24, ShipCity: "Los Angeles", ShipCountry: "USA" }, { OrderID: 20, OrderDate: "2018-12-09T12:00:00", Freight: 35.46, ShipCity: "New York", ShipCountry: "USA" }, { OrderID: 21, OrderDate: "2018-02-04T12:00:00", Freight: 57.68, ShipCity: "Guangzhou", ShipCountry: "China" }, { OrderID: 22, OrderDate: "2019-05-16T12:00:00", Freight: 9.87, ShipCity: "Long Beach", ShipCountry: "USA" }, { OrderID: 23, OrderDate: "2019-08-18T12:00:00", Freight: 24.13, ShipCity: "Singapore", ShipCountry: "Singapore" } ]; var gridDataSource = new kendo.data.DataSource({ data: orderData, schema: { model: { fields: { OrderID: { type: "number" }, Freight: { type: "number" }, OrderDate: { type: "date" }, ShipCountry: { type: "string" }, ShipCity: { type: "string" } } } }, pageSize: 10, sort: { field: "OrderDate", dir: "desc" } });
4.初始化网格
现在您已准备好初始化。首先,您可以在页面上添加一个新元素。
<div id="ordersGrid"></div>
以下代码片段包含将并使用定义的数据源的 JavaScript 代码,将代码放在gridDataSource定义之后。
有关当前步骤之后页面的可运行版本,请参阅。
$("#ordersGrid").kendoGrid({ dataSource: gridDataSource });
5.配置网格
此时,您已经填充了 Kendo UI 网格。但是,您需要改进和完善其性能,例如:
定义用户友好的列标题和。
启用数据操作——、和。
定义高度并启用,以便网格不能无限扩展。
以下示例演示了更新后的网格配置。
$("#ordersGrid").kendoGrid({ dataSource: gridDataSource, height: 400, pageable: true, sortable: true, filterable: true, columns: [{ field:"OrderID", title: "Order ID", width: 160 }, { field: "Freight", width: 160, }, { field: "OrderDate", title: "Order Date", width: 200, }, { field: "ShipCountry", title: "Ship Country" }, { field: "ShipCity", title: "Ship City" }] });
下面的可运行示例演示了您将根据本教程创建和构建的页面的最终版本。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Getting Started with Kendo UI for jQuery</title> <link rel="stylesheet" href="//kendo.cdn.telerik.com/themes/6.4.0/default/default-main.css" /> <script src="//kendo.cdn.telerik.com/2023.2.606/js/jquery.min.js"></script> <script src="//kendo.cdn.telerik.com/2023.2.606/js/kendo.all.min.js"></script> </head> <body> <div id="ordersGrid"></div> <script> $(function() { var orderData = [ { OrderID: 1, OrderDate: "2017-11-06T12:00:00", Freight: 12.34, ShipCity: "Antwerp", ShipCountry: "Belgium" }, { OrderID: 2, OrderDate: "2019-03-02T12:00:00", Freight: 23.45, ShipCity: "Singapore", ShipCountry: "Singapore" }, { OrderID: 3, OrderDate: "2019-06-26T12:00:00", Freight: 34.56, ShipCity: "Shanghai", ShipCountry: "China" }, { OrderID: 4, OrderDate: "2017-09-20T12:00:00", Freight: 45.67, ShipCity: "Hamburg", ShipCountry: "Germany" }, { OrderID: 5, OrderDate: "2017-12-12T12:00:00", Freight: 56.78, ShipCity: "Mumbai", ShipCountry: "India" }, { OrderID: 6, OrderDate: "2018-02-08T12:00:00", Freight: 67.89, ShipCity: "Shanghai", ShipCountry: "China" }, { OrderID: 7, OrderDate: "2018-05-05T12:00:00", Freight: 78.90, ShipCity: "Tokyo", ShipCountry: "Japan" }, { OrderID: 8, OrderDate: "2019-08-03T12:00:00", Freight: 89.01, ShipCity: "Port Klang", ShipCountry: "Malaysia" }, { OrderID: 9, OrderDate: "2018-10-29T12:00:00", Freight: 90.12, ShipCity: "Rotterdam", ShipCountry: "Netherlands" }, { OrderID: 10, OrderDate: "2018-01-23T12:00:00", Freight: 10.32, ShipCity: "Vancouver", ShipCountry: "Canada" }, { OrderID: 11, OrderDate: "2018-04-17T12:00:00", Freight: 21.43, ShipCity: "Colón", ShipCountry: "Panama" }, { OrderID: 12, OrderDate: "2017-07-11T12:00:00", Freight: 32.54, ShipCity: "Manila", ShipCountry: "Philippines" }, { OrderID: 13, OrderDate: "2017-10-24T12:00:00", Freight: 43.65, ShipCity: "Singapore", ShipCountry: "Singapore" }, { OrderID: 14, OrderDate: "2018-03-11T12:00:00", Freight: 54.76, ShipCity: "Busan", ShipCountry: "South Korea" }, { OrderID: 15, OrderDate: "2018-06-17T12:00:00", Freight: 65.87, ShipCity: "Shenzhen", ShipCountry: "China" }, { OrderID: 16, OrderDate: "2018-10-13T12:00:00", Freight: 76.98, ShipCity: "Hong Kong", ShipCountry: "China" }, { OrderID: 17, OrderDate: "2019-04-19T12:00:00", Freight: 87.09, ShipCity: "Dubai", ShipCountry: "UAE" }, { OrderID: 18, OrderDate: "2019-07-25T12:00:00", Freight: 98.21, ShipCity: "Felixstowe", ShipCountry: "UK" }, { OrderID: 19, OrderDate: "2017-09-22T12:00:00", Freight: 13.24, ShipCity: "Los Angeles", ShipCountry: "USA" }, { OrderID: 20, OrderDate: "2018-12-09T12:00:00", Freight: 35.46, ShipCity: "New York", ShipCountry: "USA" }, { OrderID: 21, OrderDate: "2018-02-04T12:00:00", Freight: 57.68, ShipCity: "Guangzhou", ShipCountry: "China" }, { OrderID: 22, OrderDate: "2019-05-16T12:00:00", Freight: 9.87, ShipCity: "Long Beach", ShipCountry: "USA" }, { OrderID: 23, OrderDate: "2019-08-18T12:00:00", Freight: 24.13, ShipCity: "Singapore", ShipCountry: "Singapore" } ]; var gridDataSource = new kendo.data.DataSource({ data: orderData, schema: { model: { fields: { OrderID: { type: "number" }, Freight: { type: "number" }, OrderDate: { type: "date" }, ShipCountry: { type: "string" }, ShipCity: { type: "string" } } } }, pageSize: 10, sort: { field: "OrderDate", dir: "desc" } }); $("#ordersGrid").kendoGrid({ dataSource: gridDataSource, height: 400, pageable: true, sortable: true, filterable: true, columns: [{ field:"OrderID", title: "Order ID", width: 160 }, { field: "OrderDate", title: "Order Date", width: 200, format: "{0:dd MMMM yyyy}" }, { field: "ShipCountry", title: "Ship Country" }, { field: "ShipCity", title: "Ship City" }] }); }); </script> </body> </html>