|
| 1 | +--- |
| 2 | +title: Toggle Edit Mode |
| 3 | +page_title: Toggle Edit Mode |
| 4 | +description: "Learn how to toggle the edit mode of the Telerik UI for {{ site.framework }} Grid." |
| 5 | +position: 7 |
| 6 | +slug: toggle_edit_mode_grid |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +# Toggle Edit Mode |
| 11 | + |
| 12 | +As of the R3 2024 release, the {{ site.product }} Grid enables you to toggle its editable state. The feature provides the ability to switch the Grid between Readonly and Editable mode. The Grid can be initialized in either of the states and they can be toggled on the client-side, depending on the application logic. |
| 13 | + |
| 14 | +For a runnable example, refer to the [Grid Toggle Edit Mode demo](https://demos.telerik.com/{{ site.platform }}/grid/toggle-edit-mode). |
| 15 | + |
| 16 | +## Setting the Readonly Mode |
| 17 | + |
| 18 | +To enable the Readonly mode, use the `Editable.Readonly()` configuration method. |
| 19 | + |
| 20 | +```HtmlHelper |
| 21 | + @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>() |
| 22 | + .Name("grid") |
| 23 | + .Columns(columns => |
| 24 | + { |
| 25 | + columns.Bound(p => p.ProductName); |
| 26 | + columns.Bound(p => p.UnitPrice).Width(140); |
| 27 | + columns.Bound(p => p.UnitsInStock).Width(140); |
| 28 | + columns.Bound(p => p.Discontinued).Width(100); |
| 29 | + columns.Command(command => |
| 30 | + { |
| 31 | + command.Edit(); |
| 32 | + command.Destroy(); |
| 33 | + }).Width(150); |
| 34 | + }) |
| 35 | + .ToolBar(toolbar => |
| 36 | + { |
| 37 | + toolbar.Create(); |
| 38 | + }) |
| 39 | + .Editable(editable => editable |
| 40 | + .Mode(GridEditMode.InLine) |
| 41 | + .Readonly(true) |
| 42 | + ) |
| 43 | + .DataSource(dataSource => dataSource |
| 44 | + .Ajax() |
| 45 | + .PageSize(20) |
| 46 | + .ServerOperation(false) |
| 47 | + .Model(model => model.Id(p => p.ProductID)) |
| 48 | + .Create("Editing_Create", "Grid") |
| 49 | + .Read("Editing_Read", "Grid") |
| 50 | + .Update("Editing_Update", "Grid") |
| 51 | + .Destroy("Editing_Destroy", "Grid") |
| 52 | + ) |
| 53 | + ) |
| 54 | +``` |
| 55 | +{% if site.core %} |
| 56 | +```TagHelper |
| 57 | + <kendo-grid name="Grid"> |
| 58 | + <datasource type="DataSourceTagHelperType.Ajax" page-size="20" |
| 59 | + server-operation="false"> |
| 60 | + <schema data="Data" total="Total"> |
| 61 | + <model id="ProductID"> |
| 62 | + <fields> |
| 63 | + <field name="ProductID" type="number" editable="false"></field> |
| 64 | + <field name="ProductName" type="string"></field> |
| 65 | + <field name="UnitPrice" type="number"></field> |
| 66 | + <field name="UnitsInStock" type="number"></field> |
| 67 | + <field name="Discontinued" type="boolean"></field> |
| 68 | + </fields> |
| 69 | + </model> |
| 70 | + </schema> |
| 71 | + <transport> |
| 72 | + <read url="@Url.Action("Editing_Read", "Grid")" /> |
| 73 | + <update url="@Url.Action("Editing_Update", "Grid")" /> |
| 74 | + <create url="@Url.Action("Editing_Create", "Grid")" /> |
| 75 | + <destroy url="@Url.Action("Editing_Destroy", "Grid")" /> |
| 76 | + </transport> |
| 77 | + </datasource> |
| 78 | + <columns> |
| 79 | + <column field="ProductName" /> |
| 80 | + <column field="UnitPrice" width="140" /> |
| 81 | + <column field="UnitsInStock" width="140" /> |
| 82 | + <column field="Discontinued" width="100" /> |
| 83 | + <column width="150"> |
| 84 | + <commands> |
| 85 | + <column-command text="Edit" name="edit"></column-command> |
| 86 | + <column-command text="Delete" name="destroy"></column-command> |
| 87 | + </commands> |
| 88 | + </column> |
| 89 | + </columns> |
| 90 | + <toolbar> |
| 91 | + <toolbar-button name="create"></toolbar-button> |
| 92 | + </toolbar> |
| 93 | + <editable mode="inline" readonly="true" /> |
| 94 | + </kendo-grid> |
| 95 | +``` |
| 96 | +{% endif %} |
| 97 | + |
| 98 | +## Toggling the Edit Mode |
| 99 | + |
| 100 | +The {{ site.product }} Grid allows you to programmatically alter the editable state of the component through the following methods: |
| 101 | + |
| 102 | +* [`disableEditing()`](/api/javascript/ui/grid/methods/disableediting)—Disables editing operations. |
| 103 | +* [`enableEditing()`](/api/javascript/ui/grid/methods/enableediting)—Enables editing operations. |
| 104 | + |
| 105 | + |
| 106 | +```JavaScript |
| 107 | + $(document).ready(function(){ |
| 108 | + // Determine whether either of the methods should be invoked. |
| 109 | + $("#grid").getKendoGrid().disableEditing(); |
| 110 | + $("#grid").getKendoGrid().enableEditing(); |
| 111 | + }) |
| 112 | +``` |
| 113 | + |
| 114 | +## See Also |
| 115 | + |
| 116 | +* [Toggle Edit Mode of the Grid HtmlHelper for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/grid/toggle-edit-mode) |
| 117 | +* [Server-Side API](/api/grid) |
| 118 | +* [Client-Side API](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid) |
0 commit comments