|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html xmlns="http://www.w3.org/1999/xhtml"> |
3 | 3 | <head> |
4 | | - <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.common.min.css"> |
5 | | - <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.rtl.min.css"> |
6 | | - <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.default.min.css"> |
7 | | - <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.dataviz.min.css"> |
8 | | - <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.dataviz.default.min.css"> |
9 | | - <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.mobile.all.min.css"> |
| 4 | + <link rel="stylesheet" href="http://cdn.kendostatic.com/2015.2.624/styles/kendo.common.min.css"> |
| 5 | + <link rel="stylesheet" href="http://cdn.kendostatic.com/2015.2.624/styles/kendo.default.min.css"> |
10 | 6 | <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> |
11 | | - <script src="http://cdn.kendostatic.com/2014.3.1119/js/kendo.all.min.js"></script> |
| 7 | + <script src="https://kendo.cdn.telerik.com/2015.2.624/js/kendo.all.min.js"></script> |
12 | 8 | <meta charset=utf-8 /> |
13 | 9 | <title>Odata 4 with WebAPI</title> |
14 | 10 | </head> |
15 | 11 | <body> |
16 | 12 | <div id="products"></div> |
17 | | - <div id="employees"></div> |
18 | | - |
19 | 13 | <script> |
20 | | - $(document).ready(function () { |
21 | | - $("#products").kendoGrid({ |
22 | | - dataSource: { |
23 | | - type: "odata-v4", |
24 | | - transport: { |
25 | | - read: { |
26 | | - url: "/odata/Products", |
27 | | - data: { |
28 | | - $expand: "Category" |
29 | | - } |
30 | | - }, |
31 | | - update: { |
32 | | - url: function (data) { |
33 | | - return "/odata/Products(" + data.ProductID + ")"; |
34 | | - } |
35 | | - }, |
36 | | - create: { |
37 | | - url: "/odata/Products" |
| 14 | + $("#products").kendoGrid({ |
| 15 | + dataSource: { |
| 16 | + type: "odata-v4", |
| 17 | + transport: { |
| 18 | + read: { |
| 19 | + url: "/odata/Products", |
| 20 | + data: { |
| 21 | + $expand: "Category" |
38 | 22 | } |
39 | | - }, |
40 | | - schema: { |
41 | | - model: { |
42 | | - id: "ProductID", |
43 | | - fields: { |
44 | | - UnitPrice: { type: "number" }, |
45 | | - SupplierID: { type: "number" }, |
46 | | - UnitsInStock: { type: "number" }, |
47 | | - UnitsOnOrder: { type: "number" }, |
48 | | - ProductID: { type: "number" }, |
49 | | - Discontinued: { type: "boolean" } |
50 | | - } |
51 | | - } |
52 | | - |
53 | 23 | }, |
54 | | - requestEnd: function(e) { //this is no longer needed in latest internal build |
55 | | - if (e.type == "create") { |
56 | | - delete e.response["@odata.context"]; |
| 24 | + update: { |
| 25 | + url: function (data) { |
| 26 | + return "/odata/Products(" + data.ProductID + ")"; |
57 | 27 | } |
58 | 28 | }, |
59 | | - pageSize: 10, |
60 | | - serverFiltering: true, |
61 | | - serverPaging: true, |
62 | | - serverSorting: true |
| 29 | + create: { |
| 30 | + url: "/odata/Products?$expand=Category" |
| 31 | + } |
63 | 32 | }, |
64 | | - height: 550, |
65 | | - toolbar: ["create"], |
66 | | - groupable: true, |
67 | | - sortable: true, |
68 | | - filterable: { |
69 | | - mode: "row" |
| 33 | + sort: { |
| 34 | + field: "ProductID", |
| 35 | + dir: "desc" |
70 | 36 | }, |
71 | | - pageable: { |
72 | | - refresh: true, |
73 | | - pageSizes: true, |
74 | | - buttonCount: 5 |
75 | | - }, |
76 | | - columns: [{ |
77 | | - field: "ProductName", |
78 | | - width: 300 |
79 | | - }, { |
80 | | - field: "ProductID" |
81 | | - }, |
82 | | - "UnitPrice", "UnitsOnOrder", "Discontinued", { command: "edit" } |
83 | | - ], |
84 | | - editable: "inline" |
85 | | - }); |
86 | | - |
87 | | - |
88 | | - |
89 | | - $("#employees").kendoGrid({ |
90 | | - dataSource: { |
91 | | - type: "odata-v4", |
92 | | - transport: { |
93 | | - read: { |
94 | | - url: "/odata/Employees" |
95 | | - }, |
96 | | - update: { |
97 | | - url: function (data) { |
98 | | - return "/odata/Employees(" + data.EmployeeID + ")"; |
99 | | - } |
100 | | - } |
101 | | - }, |
102 | | - schema: { |
103 | | - model: { |
104 | | - id: "EmployeeID", |
105 | | - fields: { |
106 | | - Firstname: { type: "string" }, |
107 | | - BirthDate: { type: "date" }, |
108 | | - EmployeeID: { type: "number" } |
109 | | - } |
| 37 | + schema: { |
| 38 | + model: { |
| 39 | + id: "ProductID", |
| 40 | + fields: { |
| 41 | + UnitPrice: { type: "number" }, |
| 42 | + SupplierID: { type: "number" }, |
| 43 | + UnitsInStock: { type: "number" }, |
| 44 | + UnitsOnOrder: { type: "number" }, |
| 45 | + ProductID: { type: "number", editable: false }, |
| 46 | + SupplierID: { type: "number", defaultValue: 1 }, |
| 47 | + Discontinued: { type: "boolean" }, |
| 48 | + CategoryID: { type: "number" }, |
| 49 | + Category: { defaultValue: { CategoryID: 1, CategoryName: "Beverages" } } |
110 | 50 | } |
| 51 | + } |
111 | 52 |
|
112 | | - }, |
113 | | - pageSize: 10, |
114 | | - serverFiltering: true, |
115 | | - serverPaging: true, |
116 | | - serverSorting: true |
117 | 53 | }, |
118 | | - height: 550, |
119 | | - |
120 | | - groupable: true, |
121 | | - sortable: true, |
122 | | - filterable: { |
123 | | - mode: "row" |
| 54 | + requestEnd: function(e) { |
| 55 | + if (e.type == "create") { |
| 56 | + // Make a read request to expand Category. By default the OData controller doesn't expand on create. |
| 57 | + this.read(); |
| 58 | + } |
124 | 59 | }, |
125 | | - pageable: { |
126 | | - refresh: true, |
127 | | - pageSizes: true, |
128 | | - buttonCount: 5 |
129 | | - }, |
130 | | - columns: [ |
131 | | - { field: "FirstName", width: 300 }, |
132 | | - { field: "LastName", width: 300 }, |
133 | | - { field: "BirthDate", width: 300, format: "{0: MM/dd/yyyy}" }, |
134 | | - { command: ["edit", "destroy"] } |
135 | | - ], |
136 | | - editable: "inline" |
137 | | - }); |
| 60 | + pageSize: 10, |
| 61 | + serverFiltering: true, |
| 62 | + serverPaging: true, |
| 63 | + serverSorting: true |
| 64 | + }, |
| 65 | + height: 650, |
| 66 | + toolbar: ["create"], |
| 67 | + groupable: true, |
| 68 | + sortable: true, |
| 69 | + filterable: { |
| 70 | + mode: "row" |
| 71 | + }, |
| 72 | + pageable: { |
| 73 | + refresh: true, |
| 74 | + pageSizes: true, |
| 75 | + buttonCount: 5 |
| 76 | + }, |
| 77 | + save: function(e) { |
| 78 | + // Sync Product.CategoryID with Product.Category.CategoryID to avoid referential integrity constraint errors. |
| 79 | + e.model.set("CategoryID", e.model.Category.CategoryID); |
| 80 | + }, |
| 81 | + columns: [ |
| 82 | + { field: "ProductID" }, |
| 83 | + { field: "ProductName", width: 300 }, |
| 84 | + { field: "Category", template: "#: Category ? Category.CategoryName : '' #", editor: categoryEditor }, |
| 85 | + "UnitPrice", "UnitsOnOrder", "Discontinued", { command: "edit" } |
| 86 | + ], |
| 87 | + editable: "inline" |
138 | 88 | }); |
139 | 89 |
|
| 90 | + function categoryEditor(container, options) { |
| 91 | + $('<input required data-text-field="CategoryName" data-value-field="CategoryID" data-bind="value:' + options.field + '"/>') |
| 92 | + .appendTo(container) |
| 93 | + .kendoDropDownList({ |
| 94 | + autoBind: false, |
| 95 | + dataSource: { |
| 96 | + type: "odata-v4", |
| 97 | + pageSize: 10, |
| 98 | + transport: { |
| 99 | + read: { |
| 100 | + url: "/odata/Categories" |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
140 | 106 | </script> |
141 | 107 | </body> |
142 | 108 | </html> |
0 commit comments