|
| 1 | +--- |
| 2 | +title: AutoWidth for Kendo UI for jQuery MultiColumnComboBox with Multiple Columns |
| 3 | +description: Learn how to ensure a set column width dynamically in the Kendo UI for jQuery MultiColumnComboBox when not all columns fit on the screen. |
| 4 | +type: how-to |
| 5 | +page_title: How to Autofit Columns in MultiColumnComboBox |
| 6 | +slug: how-to-autofit-columns-multicolumncombobox-kendo-ui-jquery |
| 7 | +tags: kendo-ui-for-jquery, multicolumncombobox, scrollbar, columns, configuration |
| 8 | +res_type: kb |
| 9 | +ticketid: 1683335 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>Kendo UI for jQuery MultiColumnComboBox</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>2025.1.227</td> |
| 23 | +</tr> |
| 24 | +</tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | + |
| 29 | +When binding multiple columns to the MultiColumnComboBox, the columns may not fit properly on the screen, and a scrollbar does not appear. This knowledge base article also answers the following questions: |
| 30 | +- How can I adjust the column widths dynamically based on the screen size in the MultiColumnComboBox? |
| 31 | +- Is there a way to automatically adjust the dropdown width of the MultiColumnComboBox to ensure all columns are visible? |
| 32 | +- How to handle column and dropdown widths in the MultiColumnComboBox for responsive designs? |
| 33 | + |
| 34 | +## Solution |
| 35 | + |
| 36 | +To handle scenarios where all columns in the MultiColumnComboBox do not fit properly on the screen, resulting in the need for a scrollbar, follow these steps: |
| 37 | + |
| 38 | +1. Use the [`open`](/api/javascript/ui/multicolumncombobox/events/open) event of the Kendo UI for jQuery MultiColumnComboBox to dynamically adjust the column widths based on the screen size. |
| 39 | +2. Within the event handler, calculate the new width for the columns. |
| 40 | +3. Use the [`setOptions`](/api/javascript/ui/widget/methods/setoptions) method to apply the new width to the columns. |
| 41 | + |
| 42 | +Here is an example of how to dynamically set the column widths: |
| 43 | + |
| 44 | +```javascript |
| 45 | +open: function (e) { |
| 46 | + var newWidth = window.innerWidth / 7 - 5; |
| 47 | + e.sender.setOptions({ |
| 48 | + columns: [ |
| 49 | + { |
| 50 | + field: "CompanyName", |
| 51 | + title: "Company Name3", |
| 52 | + width: newWidth, |
| 53 | + }, |
| 54 | + { |
| 55 | + field: "CompanyName", |
| 56 | + title: "Company Name4", |
| 57 | + width: newWidth, |
| 58 | + }, |
| 59 | + ], |
| 60 | + }); |
| 61 | +}, |
| 62 | +``` |
| 63 | + |
| 64 | +Below is a runnable example: |
| 65 | + |
| 66 | +```dojo |
| 67 | + <select id="multicolumncombobox"></select> |
| 68 | +
|
| 69 | + <script> |
| 70 | + $(document).ready(function () { |
| 71 | + var multiComboBox = $("#multicolumncombobox") |
| 72 | + .kendoMultiColumnComboBox({ |
| 73 | + dataTextField: "ContactName", |
| 74 | + dataValueField: "CustomerID", |
| 75 | + height: 400, |
| 76 | + width: 500, |
| 77 | + dropDownWidth: 400, |
| 78 | + left: 0, |
| 79 | + columns: [ |
| 80 | + { |
| 81 | + field: "ContactName", |
| 82 | + title: "Contact Name", |
| 83 | + template: |
| 84 | + "<span class='customer-photo'" + |
| 85 | + "style='background-image: url(../content/web/Customers/#:data.CustomerID#.jpg);'></span>" + |
| 86 | + "<span class='customer-name'>#: ContactName #</span>", |
| 87 | + width: 200, |
| 88 | + }, |
| 89 | + { field: "ContactTitle", title: "Contact Title", width: 200 }, |
| 90 | + { field: "CompanyName", title: "Company Name", width: 200 }, |
| 91 | + { field: "CompanyName", title: "Company Name1", width: 200 }, |
| 92 | + { field: "CompanyName", title: "Company Name2", width: 200 }, |
| 93 | + { field: "CompanyName", title: "Company Name3", width: 200 }, |
| 94 | + { field: "CompanyName", title: "Company Name4", width: 200 }, |
| 95 | + ], |
| 96 | + footerTemplate: |
| 97 | + "Total #: instance.dataSource.total() # items found", |
| 98 | + filter: "contains", |
| 99 | + filterFields: [ |
| 100 | + "ContactName", |
| 101 | + "ContactTitle", |
| 102 | + "CompanyName", |
| 103 | + "CompanyName", |
| 104 | + "CompanyName", |
| 105 | + "CompanyName", |
| 106 | + "CompanyName", |
| 107 | + ], |
| 108 | + dataSource: { |
| 109 | + type: "odata", |
| 110 | + transport: { |
| 111 | + read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Customers", |
| 112 | + }, |
| 113 | + }, |
| 114 | + open: function (e) { |
| 115 | + var newWidth = window.innerWidth / 7 - 5; |
| 116 | + e.sender.setOptions({ |
| 117 | + columns: [ |
| 118 | + { |
| 119 | + field: "ContactName", |
| 120 | + title: "Contact Name", |
| 121 | + template: |
| 122 | + "<span class='customer-photo'" + |
| 123 | + "style='background-image: url(../content/web/Customers/#:data.CustomerID#.jpg);'></span>" + |
| 124 | + "<span class='customer-name'>#: ContactName #</span>", |
| 125 | + width: newWidth, |
| 126 | + }, |
| 127 | + { |
| 128 | + field: "ContactTitle", |
| 129 | + title: "Contact Title", |
| 130 | + width: newWidth, |
| 131 | + }, |
| 132 | + { |
| 133 | + field: "CompanyName", |
| 134 | + title: "Company Name", |
| 135 | + width: newWidth, |
| 136 | + }, |
| 137 | + { |
| 138 | + field: "CompanyName", |
| 139 | + title: "Company Name1", |
| 140 | + width: newWidth, |
| 141 | + }, |
| 142 | + { |
| 143 | + field: "CompanyName", |
| 144 | + title: "Company Name2", |
| 145 | + width: newWidth, |
| 146 | + }, |
| 147 | + { |
| 148 | + field: "CompanyName", |
| 149 | + title: "Company Name3", |
| 150 | + width: newWidth, |
| 151 | + }, |
| 152 | + { |
| 153 | + field: "CompanyName", |
| 154 | + title: "Company Name4", |
| 155 | + width: newWidth, |
| 156 | + }, |
| 157 | + ], |
| 158 | + }); |
| 159 | + }, |
| 160 | + }) |
| 161 | + .data("kendoMultiColumnComboBox"); |
| 162 | + }); |
| 163 | + </script> |
| 164 | +``` |
| 165 | + |
| 166 | +For more detailed information on configuring the columns and dropdown width of the MultiColumnComboBox, refer to the official documentation: |
| 167 | +- [Configuring columns in MultiColumnComboBox](https://docs.telerik.com/kendo-ui/api/javascript/ui/multicolumncombobox/configuration/columns.width) |
| 168 | +- [Setting dropdown width](https://docs.telerik.com/kendo-ui/api/javascript/ui/multicolumncombobox/configuration/dropdownwidth) |
| 169 | + |
| 170 | +## See Also |
| 171 | + |
| 172 | +- [Kendo UI for jQuery MultiColumnComboBox Overview]https://docs.telerik.com/kendo-ui/controls/multicolumncombobox/overview) |
| 173 | +- [MultiColumnComboBox Open Event](https://docs.telerik.com/kendo-ui/api/javascript/ui/multicolumncombobox/events/open) |
| 174 | +- [MultiColumnComboBox setOptions Method](https://docs.telerik.com/kendo-ui/api/javascript/ui/widget/methods/setoptions) |
0 commit comments