|
| 1 | +--- |
| 2 | +title: Edit a TimeSpan Field in InCell Mode |
| 3 | +description: How to Edit a TimeSpan Field in Batch Editing Mode |
| 4 | +type: how-to |
| 5 | +page_title: How to Edit a TimeSpan Field in InCell Editing Mode | Kendo UI Grid for jQuery |
| 6 | +slug: grid-edit-timespan-field |
| 7 | +position: |
| 8 | +tags: |
| 9 | +ticketid: |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Grid for Progress® Kendo UI®</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +This sample demonstrates how to edit a TimeSpan field in InCell editing mode of the Kendo UI Grid. |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +```dojo |
| 31 | + |
| 32 | + <script> |
| 33 | + kendo.data.binders.widget.timespan = kendo.data.Binder.extend({ |
| 34 | + init: function (widget, bindings, options) { |
| 35 | + kendo.data.Binder.fn.init.call(this, widget.element[0], bindings, options); |
| 36 | + this.widget = widget; |
| 37 | + this._change = $.proxy(this.change, this); |
| 38 | + this.widget.bind("change", this._change); |
| 39 | + }, |
| 40 | + refresh: function () { |
| 41 | + var value = this.bindings.timespan.get(); |
| 42 | + var date = null; |
| 43 | + if (value) { |
| 44 | + date = new Date(); |
| 45 | + date.setHours(value.Hours); |
| 46 | + date.setMinutes(value.Minutes); |
| 47 | + date.setSeconds(value.Seconds); |
| 48 | + } |
| 49 | + this.widget.value(date); |
| 50 | + }, |
| 51 | + change: function () { |
| 52 | + var date = this.widget.value(); |
| 53 | + var value = null; |
| 54 | + if (date) { |
| 55 | + value = { |
| 56 | + Hours: date.getHours(), |
| 57 | + Minutes: date.getMinutes(), |
| 58 | + Seconds: date.getSeconds() |
| 59 | + }; |
| 60 | + } |
| 61 | + this.bindings.timespan.set(value); |
| 62 | + }, |
| 63 | + destroy: function () { |
| 64 | + this.widget.unbind("change", this._change); |
| 65 | + } |
| 66 | + }); |
| 67 | + </script> |
| 68 | +
|
| 69 | + <div id="example"> |
| 70 | + <div id="grid"></div> |
| 71 | +
|
| 72 | + <script> |
| 73 | + var products = [{ |
| 74 | + ProductID : 1, |
| 75 | + ProductName : "Chai", |
| 76 | + SupplierID : 1, |
| 77 | + CategoryID : 1, |
| 78 | + QuantityPerUnit : "10 boxes x 20 bags", |
| 79 | + UnitPrice : 18.0000, |
| 80 | + UnitsInStock : 39, |
| 81 | + UnitsOnOrder : 0, |
| 82 | + ReorderLevel : 10, |
| 83 | + Discontinued : false, |
| 84 | + Category : { |
| 85 | + CategoryID : 1, |
| 86 | + CategoryName : "Beverages", |
| 87 | + Description : "Soft drinks, coffees, teas, beers, and ales" |
| 88 | + }, |
| 89 | + End: |
| 90 | + { |
| 91 | + "Ticks":863990000000, |
| 92 | + "Days":0, |
| 93 | + "Hours":23, |
| 94 | + "Milliseconds":0, |
| 95 | + "Minutes":59, |
| 96 | + "Seconds":59, |
| 97 | + "TotalDays":0.99998842592592585, |
| 98 | + "TotalHours":23.999722222222221, |
| 99 | + "TotalMilliseconds":86399000, |
| 100 | + "TotalMinutes":1439.9833333333334, |
| 101 | + "TotalSeconds":86399 |
| 102 | + } |
| 103 | + }, { |
| 104 | + ProductID : 2, |
| 105 | + ProductName : "Chang", |
| 106 | + SupplierID : 1, |
| 107 | + CategoryID : 1, |
| 108 | + QuantityPerUnit : "24 - 12 oz bottles", |
| 109 | + UnitPrice : 19.0000, |
| 110 | + UnitsInStock : 17, |
| 111 | + UnitsOnOrder : 40, |
| 112 | + ReorderLevel : 25, |
| 113 | + Discontinued : false, |
| 114 | + Category : { |
| 115 | + CategoryID : 1, |
| 116 | + CategoryName : "Beverages", |
| 117 | + Description : "Soft drinks, coffees, teas, beers, and ales" |
| 118 | + }, |
| 119 | + End: |
| 120 | + { |
| 121 | + "Ticks":863990000000, |
| 122 | + "Days":0, |
| 123 | + "Hours":23, |
| 124 | + "Milliseconds":0, |
| 125 | + "Minutes":59, |
| 126 | + "Seconds":59, |
| 127 | + "TotalDays":0.99998842592592585, |
| 128 | + "TotalHours":23.999722222222221, |
| 129 | + "TotalMilliseconds":86399000, |
| 130 | + "TotalMinutes":1439.9833333333334, |
| 131 | + "TotalSeconds":86399 |
| 132 | + } |
| 133 | + }, { |
| 134 | + ProductID : 3, |
| 135 | + ProductName : "Aniseed Syrup", |
| 136 | + SupplierID : 1, |
| 137 | + CategoryID : 2, |
| 138 | + QuantityPerUnit : "12 - 550 ml bottles", |
| 139 | + UnitPrice : 10.0000, |
| 140 | + UnitsInStock : 13, |
| 141 | + UnitsOnOrder : 70, |
| 142 | + ReorderLevel : 25, |
| 143 | + Discontinued : false, |
| 144 | + Category : { |
| 145 | + CategoryID : 2, |
| 146 | + CategoryName : "Condiments", |
| 147 | + Description : "Sweet and savory sauces, relishes, spreads, and seasonings" |
| 148 | + }, |
| 149 | + End: |
| 150 | + { |
| 151 | + "Ticks":863990000000, |
| 152 | + "Days":0, |
| 153 | + "Hours":23, |
| 154 | + "Milliseconds":0, |
| 155 | + "Minutes":59, |
| 156 | + "Seconds":59, |
| 157 | + "TotalDays":0.99998842592592585, |
| 158 | + "TotalHours":23.999722222222221, |
| 159 | + "TotalMilliseconds":86399000, |
| 160 | + "TotalMinutes":1439.9833333333334, |
| 161 | + "TotalSeconds":86399 |
| 162 | + } |
| 163 | + }] |
| 164 | +
|
| 165 | + $(document).ready(function() { |
| 166 | + $("#grid").kendoGrid({ |
| 167 | + dataSource: { |
| 168 | + data: products, |
| 169 | + schema: { |
| 170 | + model: { |
| 171 | + fields: { |
| 172 | + ProductID: { editable: false, nullable: true }, |
| 173 | + ProductName: { type: "string" }, |
| 174 | + UnitPrice: { type: "number" }, |
| 175 | + UnitsInStock: { type: "number" }, |
| 176 | + Discontinued: { type: "boolean" }, |
| 177 | + End: { editable: true } |
| 178 | + } |
| 179 | + } |
| 180 | + }, |
| 181 | + pageSize: 20 |
| 182 | + }, |
| 183 | + toolbar: ["create", "save", "cancel"], |
| 184 | + scrollable: true, |
| 185 | + sortable: true, |
| 186 | + editable: true, |
| 187 | + filterable: true, |
| 188 | + pageable: { |
| 189 | + input: true, |
| 190 | + numeric: false |
| 191 | + }, |
| 192 | + columns: [ |
| 193 | + "ProductName", |
| 194 | + { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "130px" }, |
| 195 | + { field: "UnitsInStock", title: "Units In Stock", width: "90px" }, |
| 196 | + { field: "Discontinued", width: "90px" }, |
| 197 | + { field: "End", template: "#if (data.End) {# #:kendo.toString(End.Hours, '00')#:#:kendo.toString(End.Minutes, '00')#:#:kendo.toString(End.Seconds, '00')# #}#", editor: timePickerEditor } |
| 198 | + ] |
| 199 | + }); |
| 200 | + }); |
| 201 | +
|
| 202 | + function timePickerEditor(container, options) { |
| 203 | + $('<input data-skip="true" data-bind="timespan:End" name="' + options.field + '"/>') |
| 204 | + .appendTo(container) |
| 205 | + .kendoTimePicker({ |
| 206 | + format: "HH:mm:ss" |
| 207 | + }); |
| 208 | + } |
| 209 | + </script> |
| 210 | + </div> |
| 211 | +
|
| 212 | +``` |
0 commit comments