|
| 1 | +--- |
| 2 | +title: Hide Grid Columns When the Browser Width Is Too Small |
| 3 | +description: "An example demonstrating how to hide the Grid columns in the detail template when the width of the browser is too small." |
| 4 | +type: how-to |
| 5 | +page_title: Collapse Other Rows on Row Expand | Kendo UI Grid for jQuery |
| 6 | +slug: grid-browser-resize-hides-columns-in-detail-template |
| 7 | +tags: grid, hierarchy, collapse, expand, hide, columns, column, detail, template |
| 8 | +ticketid: 1358601 |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Progress Kendo UI Grid</td> |
| 18 | + </tr> |
| 19 | + <tr> |
| 20 | + <td>Progress Kendo UI version</td> |
| 21 | + <td>Created with the 2021.3.914 version</td> |
| 22 | + </tr> |
| 23 | +</table> |
| 24 | + |
| 25 | +## Description |
| 26 | + |
| 27 | +How can I hide the Grid columns inside the [`detailTemplate`](/api/javascript/ui/grid/configuration/detailtemplate) when the width of the browser window becomes too small? |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +1. Define the thresholds at which each individual column will be hidden. Refer to the `getThresholds` function in the example below. |
| 32 | +1. Set these thresholds to the Grid by using the [`columns.media`](/api/javascript/ui/grid/configuration/columns.media) configuration. Refer to the `setThresholds` function in the example below. |
| 33 | +1. Attach handlers for the [`dataBound`](/api/javascript/ui/grid/events/databound), [`columnHide`](/api/javascript/ui/grid/events/columnhide), and [`columnShow`](/api/javascript/ui/grid/events/columnshow) events to the Grid. The `columnHide` and `columnShow` handlers are responsible for hiding and showing the columns inside the `detailTemplate`. The `dataBound` handler is responsible for initializing the `detailTemplate`. |
| 34 | +1. Handle the `resize` event of the JavaScript `window` object. If the Grid doesn't have any hidden columns when the browser window is resized, collapse all detail rows. |
| 35 | +1. Use `CSS` to conditionally display the hierarchy column. |
| 36 | + |
| 37 | +The following example demonstrates the full implementation of the suggested approach. It is recommended that you run the demo in fullscreen—copy the code and paste it in a new [`dojo`](https://dojo.telerik.com/); then, click **Full screen**. |
| 38 | + |
| 39 | +```dojo |
| 40 | + <div id="grid"></div> |
| 41 | +
|
| 42 | + <script type="text/x-kendo-template" id="template"> |
| 43 | + <div class="detail-columns-wrapper"> |
| 44 | + <span class="hidden" data-field="Title">Title - #=data.Title#</span></br> |
| 45 | + <span class="hidden" data-field="City">City - #=data.City#</span></br> |
| 46 | + <span class="hidden" data-field="Country">Country - #=data.Country#</span></br> |
| 47 | + <span class="hidden" data-field="LastName">Last Name - #=data.LastName#</span></br> |
| 48 | + <span class="hidden" data-field="FirstName">First Name - #=data.FirstName#</span> |
| 49 | + </div> |
| 50 | + </script> |
| 51 | +
|
| 52 | + <script> |
| 53 | + // The initialWidth is used to calculate the thresholds at which each column will be hidden. |
| 54 | + let initialWidth = 1000; |
| 55 | +
|
| 56 | + $(document).ready(function () { |
| 57 | + var grid = $("#grid").kendoGrid({ |
| 58 | + dataSource: { |
| 59 | + type: "odata", |
| 60 | + transport: { |
| 61 | + read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Employees" |
| 62 | + }, |
| 63 | + pageSize: 20, |
| 64 | + serverPaging: true, |
| 65 | + serverSorting: true |
| 66 | + }, |
| 67 | + height: 550, |
| 68 | + sortable: true, |
| 69 | + pageable: false, |
| 70 | + dataBound: dataBoundHandler, |
| 71 | + detailTemplate: kendo.template($("#template").html()), |
| 72 | + columnHide: columnHideHandler, |
| 73 | + columnShow: columnShowHandler, |
| 74 | + columns: [ |
| 75 | + { |
| 76 | + field: "FirstName", |
| 77 | + title: "First Name", |
| 78 | + width: 120 |
| 79 | + }, |
| 80 | + { |
| 81 | + field: "LastName", |
| 82 | + title: "Last Name", |
| 83 | + width: 120 |
| 84 | + }, |
| 85 | + { |
| 86 | + field: "Country", |
| 87 | + width: 120 |
| 88 | + }, |
| 89 | + { |
| 90 | + field: "City", |
| 91 | + width: 120 |
| 92 | + }, |
| 93 | + { |
| 94 | + field: "Title", |
| 95 | + width: 220 |
| 96 | + } |
| 97 | + ] |
| 98 | + }).data("kendoGrid"); |
| 99 | +
|
| 100 | + let columns = grid.options.columns; |
| 101 | + let columnsCount = columns.length; |
| 102 | +
|
| 103 | + let thresholds = getThresholds(columns, columnsCount, grid, initialWidth); |
| 104 | + setThresholds(columns, columnsCount, grid, thresholds); |
| 105 | +
|
| 106 | + /* Collapse the rows if there are no hidden columns. */ |
| 107 | + $(window).on("resize", function () { |
| 108 | + let windowWidth = $(this).width(); |
| 109 | +
|
| 110 | + if (windowWidth > 780) { |
| 111 | + $(".k-master-row").each(function () { |
| 112 | + grid.collapseRow(this); |
| 113 | + }); |
| 114 | + } |
| 115 | + }); |
| 116 | +
|
| 117 | + }); |
| 118 | +
|
| 119 | + function columnHideHandler(e) { |
| 120 | + $(".detail-columns-wrapper").find("[data-field='" + e.column.field + "']").removeClass("hidden"); |
| 121 | + } |
| 122 | +
|
| 123 | + function columnShowHandler(e) { |
| 124 | + $(".detail-columns-wrapper").find("[data-field='" + e.column.field + "']").addClass("hidden"); |
| 125 | + } |
| 126 | +
|
| 127 | + function dataBoundHandler(e) { |
| 128 | + let grid = this; |
| 129 | + // Initialize all of the detail templates. |
| 130 | + $(".k-master-row").each(function () { |
| 131 | + grid.expandRow(this); |
| 132 | + grid.collapseRow(this) |
| 133 | + }); |
| 134 | +
|
| 135 | + // If the Grid is already shrunken when the page loads, find the hidden columns. |
| 136 | + let hiddenColumns = grid.columns.filter(f => f.matchesMedia == false); |
| 137 | +
|
| 138 | + // Remove the "hidden" class from the span elements that have their column counterparts hidden. |
| 139 | + hiddenColumns.forEach(function (x, i) { |
| 140 | + $(".k-detail-row").find("[data-field='" + x.field + "']").removeClass("hidden"); |
| 141 | + }); |
| 142 | + } |
| 143 | +
|
| 144 | + function setThresholds(columns, count, grid, thresholds) { |
| 145 | + // Start from 1 so the first column is always visible. |
| 146 | + for (let i = 1; i < count; i++) { |
| 147 | + columns[i].media = "(min-width: " + thresholds[i] + "px)"; |
| 148 | + } |
| 149 | +
|
| 150 | + grid.setOptions({ |
| 151 | + columns: columns |
| 152 | + }); |
| 153 | + } |
| 154 | +
|
| 155 | + function getThresholds(columns, count, grid, initialWidth) { |
| 156 | + let thresholds = []; |
| 157 | + let acc = 0; |
| 158 | +
|
| 159 | + // Get the thresholds at which the columns will be hidden. Modify the calculations in any way you see fit. |
| 160 | + // In this case the thresholds are calculated with the following formula: |
| 161 | + // initialWidth - (width of the last visible column) - (combined width of all already hidden columns.) |
| 162 | + for (let i = 0; i < count; i++) { |
| 163 | + let threshold = initialWidth - (columns[count - 1 - i].width) - acc; |
| 164 | + acc += (columns[count - 1 - i].width); |
| 165 | +
|
| 166 | + thresholds.push(threshold); |
| 167 | + } |
| 168 | +
|
| 169 | + thresholds.reverse(); |
| 170 | +
|
| 171 | + return thresholds; |
| 172 | + } |
| 173 | + </script> |
| 174 | +
|
| 175 | + <style> |
| 176 | + /* Used to remove the empty space when a column is removed. */ |
| 177 | + #grid table { |
| 178 | + min-width: 100%; |
| 179 | + } |
| 180 | +
|
| 181 | + .hidden { |
| 182 | + display: none; |
| 183 | + } |
| 184 | +
|
| 185 | + /* Hide the hierarchy column. */ |
| 186 | + colgroup col:nth-child(1) { |
| 187 | + width: 0px !important; |
| 188 | + } |
| 189 | +
|
| 190 | + .k-i-expand { |
| 191 | + display: none !important; |
| 192 | + } |
| 193 | +
|
| 194 | + /* Display the hierarchy column when the width of the browser window becomes less than 780px. */ |
| 195 | + /* 780px is the initialWidth(1000px) - the width of the last column(220px) */ |
| 196 | + @media (max-width: 780px) { |
| 197 | + colgroup col:nth-child(1) { |
| 198 | + width: 32px !important; |
| 199 | + } |
| 200 | +
|
| 201 | + .k-i-expand { |
| 202 | + display: block !important; |
| 203 | + } |
| 204 | + } |
| 205 | + </style> |
| 206 | +``` |
0 commit comments