|
| 1 | +--- |
| 2 | +title: Row and Column Spanning |
| 3 | +page_title: jQuery Grid Documentation - Row and Column Spanning |
| 4 | +description: "Get started with the jQuery Grid by Kendo UI and learn all about the Row and Column Spanning feature." |
| 5 | +slug: row_column_spanning__kendoui_grid |
| 6 | +position: 15 |
| 7 | +--- |
| 8 | + |
| 9 | +# Row and Column Spanning |
| 10 | + |
| 11 | + |
| 12 | +## Row Spanning |
| 13 | + |
| 14 | +The Row spanning functionality of the Grid enables you to span a cell between multiple rows. |
| 15 | +You can use the [`attributes`](/api/javascript/ui/grid/configuration/columns.attributes) function and calculate the span of each cell. |
| 16 | + |
| 17 | +```dojo |
| 18 | + <div id="grid"></div> |
| 19 | + <script type="module"> |
| 20 | + $("#grid").kendoGrid({ |
| 21 | + columns: [ |
| 22 | + "id", |
| 23 | + "name", |
| 24 | + { |
| 25 | + field: "country", width: 800, |
| 26 | + attributes: (dataItem) => { |
| 27 | + const dataView = dataItem.parent(); |
| 28 | + let currentIndex = dataView.indexOf(dataItem); |
| 29 | + const prevDataItem = currentIndex === 0 ? null : dataView.at(currentIndex - 1); |
| 30 | + let nextDataItem = dataView.at(++currentIndex); |
| 31 | + let rowSpan = 1; |
| 32 | +
|
| 33 | + if (prevDataItem && dataItem['country'] === prevDataItem['country']) { |
| 34 | + return { |
| 35 | + hidden: 'hidden' |
| 36 | + } |
| 37 | + } |
| 38 | +
|
| 39 | + while (nextDataItem) { |
| 40 | + if (dataItem['country'] === nextDataItem['country']) { |
| 41 | + rowSpan++; |
| 42 | + } else { |
| 43 | + break; |
| 44 | + } |
| 45 | +
|
| 46 | + nextDataItem = dataView.at(++currentIndex); |
| 47 | + } |
| 48 | +
|
| 49 | + return { rowSpan }; |
| 50 | + }, |
| 51 | + template: ({ country }) => { |
| 52 | + return `<strong>${kendo.htmlEncode(country)}</strong>` |
| 53 | + } |
| 54 | + } |
| 55 | + ], |
| 56 | + dataSource: [ |
| 57 | + { id: 1, name: "Albert", country: "Belgium" }, |
| 58 | + { id: 2, name: "Noah", country: "Belgium" }, |
| 59 | + { id: 3, name: "Emma", country: "Belgium" }, |
| 60 | + { id: 4, name: "Oscar", country: "Denmark" }, |
| 61 | + { id: 5, name: "William", country: "Denmark" }, |
| 62 | + { id: 6, name: "Aksel", country: "Germany" }, |
| 63 | + { id: 7, name: "Luca", country: "Italy" }, |
| 64 | + { id: 8, name: "Michele", country: "Italy" }, |
| 65 | + { id: 9, name: "Giuseppe", country: "Italy" }, |
| 66 | + { id: 10, name: "Juan Carlos", country: "Spain" }, |
| 67 | + ] |
| 68 | + }); |
| 69 | + </script> |
| 70 | +``` |
| 71 | + |
| 72 | +## Column Spanning |
| 73 | + |
| 74 | +The Column spanning functionality enables you to span multiple columns in the Grid. This feature is equal to 'cell merging' in Excel or 'column spanning' in HTML tables. |
| 75 | +The Column spanning is available through the [`attributes`](/api/javascript/ui/grid/configuration/columns.attributes) function and its `hidden` property. |
| 76 | + |
| 77 | +``` |
| 78 | +<div id="grid"></div> |
| 79 | + <script> |
| 80 | + $("#grid").kendoGrid({ |
| 81 | + columns: [ |
| 82 | + { field: "id", width: 100 }, |
| 83 | + { field: "name", width: 150 }, |
| 84 | + { field: "pass", |
| 85 | + width: 100, |
| 86 | + attributes: ({ pass }) => { |
| 87 | + if (!pass) { |
| 88 | + return { |
| 89 | + colSpan: 2, |
| 90 | + "class": "!k-text-center" |
| 91 | + }; |
| 92 | + } |
| 93 | + }, |
| 94 | + template: "#= !pass ? 'Not passing' : 'Passing' #" |
| 95 | + }, |
| 96 | + { |
| 97 | + field: "country", |
| 98 | + width: 300, |
| 99 | + attributes: ({ pass }) => { |
| 100 | + if (!pass) { |
| 101 | + return { hidden: "hidden" }; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + ], |
| 106 | + dataSource: [ |
| 107 | + { id: 1, name: "Albert", country: "Belgium", pass: false }, |
| 108 | + { id: 2, name: "Noah", country: "Belgium", pass: true }, |
| 109 | + { id: 3, name: "Emma", country: "Belgium", pass: false }, |
| 110 | + { id: 4, name: "Oscar", country: "Denmark", pass: false }, |
| 111 | + { id: 5, name: "William", country: "Denmark", pass: true }, |
| 112 | + { id: 6, name: "Aksel", country: "Germany", pass: true }, |
| 113 | + { id: 7, name: "Luca", country: "Italy", pass: true }, |
| 114 | + { id: 8, name: "Michele", country: "Italy", pass: true }, |
| 115 | + { id: 9, name: "Giuseppe", country: "Italy", pass: false }, |
| 116 | + { id: 10, name: "Juan Carlos", country: "Spain", pass: true }, |
| 117 | + ] |
| 118 | + }); |
| 119 | + </script> |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +## See Also |
| 124 | + |
| 125 | +* [Row Spanning in the Kendo UI for jQuery Grid (Demo)](https://demos.telerik.com/kendo-ui/grid/row-spanning) |
| 126 | +* [Column Spanning in the Kendo UI for jQuery Grid (Demo)](https://demos.telerik.com/kendo-ui/grid/column-spanning) |
| 127 | +* [JavaScript API Reference of the Kendo UI for jQuery Grid](/api/javascript/ui/grid) |
| 128 | + |
0 commit comments