|
| 1 | +--- |
| 2 | +title: Adding Dropdown Validation for a Whole Column in Kendo UI Spreadsheet |
| 3 | +description: Learn how to configure dropdown validation for a whole column in Kendo UI Spreadsheet and maintain it when adding new rows. |
| 4 | +type: how-to |
| 5 | +page_title: How to Set Dropdown Validation for Entire Columns in Kendo UI Spreadsheet |
| 6 | +slug: dropdown-validation-column-kendo-ui-spreadsheet |
| 7 | +tags: spreadsheet, kendo-ui, validation, column, dropdown |
| 8 | +res_type: kb |
| 9 | +ticketid: 1689145 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | +<table> |
| 14 | +<tbody> |
| 15 | +<tr> |
| 16 | +<td>Product</td> |
| 17 | +<td>Kendo UI Spreadsheet</td> |
| 18 | +</tr> |
| 19 | +<tr> |
| 20 | +<td>Version</td> |
| 21 | +<td>2025.2.520</td> |
| 22 | +</tr> |
| 23 | +</tbody> |
| 24 | +</table> |
| 25 | + |
| 26 | +## Description |
| 27 | + |
| 28 | +I want to make an entire column in the [Kendo UI Spreadsheet](https://docs.telerik.com/kendo-ui/spreadsheet/overview) function as a dropdown menu, preserving dropdown validation for newly added rows. |
| 29 | + |
| 30 | +This knowledge base article also answers the following questions: |
| 31 | +- How to set dropdown validation for a column in Kendo UI Spreadsheet? |
| 32 | +- How to maintain dropdown validation when adding new rows in Kendo UI Spreadsheet? |
| 33 | + |
| 34 | +## Solution |
| 35 | + |
| 36 | +### Adding Dropdown Validation for a Whole Column |
| 37 | + |
| 38 | +To add dropdown validation for an entire column, configure the validation for the column range using the [`sheets.rows.cells.validation.from`](/api/javascript/ui/spreadsheet/configuration/sheets.rows.cells.validation#sheetsrowscellsvalidationfrom) property. For newly added rows, ensure the same validation is applied to the respective cell in the column by using the [`insertRow`](/api/javascript/ui/spreadsheet/events/insertrow) event. Below is an example of how to configure a custom editor for a column upon adding a new row: |
| 39 | + |
| 40 | +```javascript |
| 41 | +insertRow: function (e) { |
| 42 | + setTimeout(function(){ |
| 43 | + e.sender.activeSheet().range(e.index, 1, 1, 1).background("#fef0cd"); // Example: Add custom background |
| 44 | + e.sender.activeSheet().range(e.index, 1, 1, 1).editor("color"); // Example: Set custom editor |
| 45 | + }); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Use the `Range.editor()` method for applying custom editors. For more details, refer to [Range.editor() documentation](https://docs.telerik.com/kendo-ui/api/javascript/spreadsheet/range/methods/editor). |
| 50 | + |
| 51 | +### Example |
| 52 | + |
| 53 | +```dojo |
| 54 | + <div id="spreadsheet" style="width: 100%"></div> |
| 55 | + <script> |
| 56 | + kendo.spreadsheet.registerEditor("color", function () { |
| 57 | + var context, dlg, model; |
| 58 | + // Further delay the initialization of the UI until the `edit` method is |
| 59 | + // actually called, so here just return the object with the required API. |
| 60 | + return { |
| 61 | + edit: function (options) { |
| 62 | + context = options; |
| 63 | + open(); |
| 64 | + }, |
| 65 | + icon: "droplet", |
| 66 | + }; |
| 67 | + // This function actually creates the UI if not already there, and |
| 68 | + // caches the dialog and the model. |
| 69 | + function create() { |
| 70 | + if (!dlg) { |
| 71 | + model = kendo.observable({ |
| 72 | + value: "#000000", |
| 73 | + ok: function () { |
| 74 | + // This is the result when OK is clicked. |
| 75 | + // Invoke the callback with the value. |
| 76 | + context.callback(model.value); |
| 77 | + dlg.close(); |
| 78 | + }, |
| 79 | + cancel: function () { |
| 80 | + dlg.close(); |
| 81 | + }, |
| 82 | + }); |
| 83 | + var el = $( |
| 84 | + "<div data-visible='true' data-role='window' data-modal='true' data-resizable='false' data-title='Select color'>" + |
| 85 | + " <div data-role='flatcolorpicker' data-bind='value: value'></div>" + |
| 86 | + " <div style='margin-top: 1em; text-align: right'>" + |
| 87 | + " <button style='width: 5em' class='k-button' data-bind='click: ok'>OK</button>" + |
| 88 | + " <button style='width: 5em' class='k-button' data-bind='click: cancel'>Cancel</button>" + |
| 89 | + " </div>" + |
| 90 | + "</div>", |
| 91 | + ); |
| 92 | + kendo.bind(el, model); |
| 93 | +
|
| 94 | + // Cache the dialog. |
| 95 | + dlg = el.getKendoWindow(); |
| 96 | + } |
| 97 | + } |
| 98 | + function open() { |
| 99 | + create(); |
| 100 | + dlg.open(); |
| 101 | + dlg.center(); |
| 102 | + // If the selected cell already contains some value, reflect |
| 103 | + // it in the custom editor. |
| 104 | + var value = context.range.value(); |
| 105 | + if (value != null) { |
| 106 | + model.set("value", value); |
| 107 | + } |
| 108 | + } |
| 109 | + }); |
| 110 | +
|
| 111 | + $(function () { |
| 112 | + $("#spreadsheet").kendoSpreadsheet({ |
| 113 | + sheetsbar: false, |
| 114 | + insertRow: function (e) { |
| 115 | + setTimeout(function () { |
| 116 | + e.sender |
| 117 | + .activeSheet() |
| 118 | + .range(e.index, 1, 1, 1) |
| 119 | + .background("#fef0cd"); |
| 120 | + e.sender.activeSheet().range(e.index, 1, 1, 1).editor("color"); |
| 121 | + }); |
| 122 | + }, |
| 123 | + excel: { |
| 124 | + // Required to enable Excel Export in some browsers. |
| 125 | + proxyURL: "//demos.telerik.com/kendo-ui/service/export", |
| 126 | + }, |
| 127 | + sheets: [ |
| 128 | + { |
| 129 | + rows: [ |
| 130 | + { |
| 131 | + cells: [ |
| 132 | + { value: "Select color:", bold: true }, |
| 133 | + { background: "#fef0cd", editor: "color" }, |
| 134 | + ], |
| 135 | + }, |
| 136 | + ], |
| 137 | + }, |
| 138 | + ], |
| 139 | + }); |
| 140 | + }); |
| 141 | + </script> |
| 142 | +``` |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +## See Also |
| 147 | + |
| 148 | +- [Spreadsheet Overview](https://www.telerik.com/kendo-jquery-ui/documentation/controls/spreadsheet/overview) |
| 149 | +- [Validation Configuration](https://docs.telerik.com/kendo-ui/api/javascript/ui/spreadsheet/configuration/sheets.rows.cells.validation) |
| 150 | +- [Range Editor Method](https://docs.telerik.com/kendo-ui/api/javascript/spreadsheet/range/methods/editor) |
| 151 | +- [Spreadsheet Custom Cell Editors (Demo)](https://demos.telerik.com/kendo-ui/spreadsheet/custom-editors) |
0 commit comments