1# Creating a Grid 2 3 4## Overview 5 6The grid layout consists of cells formed by rows and columns. You can specify the cells where items are located to form various layouts. The grid layout excels at dividing a page into regions and defining the proportion of child components. It is a key adaptive layout and applies to scenarios such as photo gallery, calendar, and calculator. 7 8ArkUI provides the \<[Grid](../reference/arkui-ts/ts-container-grid.md)> container component and \<[GridItem](../reference/arkui-ts/ts-container-griditem.md)> child component for building grid layouts. The former is used to set parameters related to the grid layout, while the latter is used to define features related to child components. The **\<Grid>** component allows creation of child components with conditional rendering, rendering of repeated content, and [lazy data loading](../quick-start/arkts-rendering-control-lazyforeach.md). 9 10 11## Layout and Constraints 12 13Each item in the **\<Grid>** container corresponds to a **\<GridItem>** component, as shown below. 14 15 **Figure 1** Relationship between \<Grid> and \<GridItem> components 16 17 18 19>**NOTE** 20> 21>The **\<Grid>** component accepts only **\<GridItem>** as its child components. 22 23The grid layout is a two-dimensional layout. The **\<Grid>** component allows you to define the number of rows and columns, proportion of each row and column, number of rows or columns that child components span, and the horizontal and vertical alignment. When the **\<Grid>** container size changes, the child components and spacing are adjusted proportionally. By leveraging these layout capabilities, you can build grid layouts of different styles, as shown below. 24 25 **Figure 2** Grid layout 26 27 28 29The size of the **\<Grid>** component follows its width and height settings (if configured) or adapts to the size of its parent component. 30 31Depending on the number of rows and columns and the proportion, the **\<Grid>** component behaves as follows: 32 33- If both the number and proportion are set for rows or columns, the **\<Grid>** component displays only elements in the fixed number of rows or columns. Other elements are not displayed, and the component cannot be scrolled. 34 35- If only the number or proportion is set for rows or columns, elements are arranged in the specified direction, and excess elements can be displayed in scrolling mode. 36 37- If neither the number nor the proportion is set for rows or columns, elements are arranged in the layout direction. The number of rows and columns is determined by the layout direction and the width and height of a single grid. Elements that exceed the range of rows and columns are not displayed, and the **\<Grid>** component cannot be scrolled. 38 39>**NOTE** 40> 41>Whenever possible, set the number or proportion for rows or columns for layout. 42 43 44## Setting the Arrangement Mode 45 46 47### Setting the Number and Proportion of Rows and Columns 48 49You can set the number and proportion of rows and columns to determine the overall arrangement mode of the grid layout. To do so, use the **rowsTemplate** and **columnsTemplate** attributes of the **\<Grid>** component. 50 51The values of **rowsTemplate** and **columnsTemplate** are a string consisting of 'number+fr' segments, separated by spaces. Wherein **frs** indicates the number of rows or columns in the grid layout, and the number before **fr** is used to calculate the proportion of the row or column in the grid width, thereby determining the width of the row or column. 52 53 **Figure 3** Example of the proportion of rows and columns 54 55 56 57The preceding figure shows a grid layout with three rows and three columns. The grid layout is divided into three parts in the vertical direction with each row taking up 1/3, and four parts in the horizontal direction with the first column taking up 1/4, the second column 2/4, and the third column 1/4. 58 59This layout can be implemented by setting **rowsTemplate** to **'1fr 1fr 1fr'** and **columnsTemplate** to **'1fr 2fr 1fr'**. 60 61 62```ts 63Grid() { 64 ... 65} 66.rowsTemplate('1fr 1fr 1fr') 67.columnsTemplate('1fr 2fr 1fr') 68``` 69 70>**NOTE** 71> 72>When **rowsTemplate** or **columnsTemplate** is set for the **\<Grid>** component, its **layoutDirection**, **maxCount**, **minCount**, and **cellLength** attributes do not take effect. For details about the attributes, see [Grid Attributes](../reference/arkui-ts/ts-container-grid.md#attributes). 73 74 75### Setting the Number of Rows and Columns Occupied by a Child Component 76 77In real-world applications, an uneven grid layout, where grid cells span a varying number of cells and rows, is as common as its even counterpart. To implement an uneven grid layout, as shown below, you can set **rowStart**, **rowEnd**, **columnStart**, and **columnEnd** of **\<GridItem>**. 78 79 **Figure 4** Uneven grid layout 80 81 82 83A common application with an uneven grid layout is the calculator. As shown in the following figure, the **0** key spans the first and second columns, and the **=** key spans the fifth and sixth rows. For a grid layout created using the **\<Grid>** component, the row and column numbers start from 1 and increase continuously. 84 85 **Figure 5** Calculator 86 87 88 89For a single grid cell, the **rowStart** and** rowEnd** attributes indicate the start and end row numbers of the current element, and the **columnStart** and **columnEnd** attributes indicate the start and end column numbers of the current element. 90 91Therefore, for the **0** key to span the first and second columns, just set **columnStart** and **columnEnd** of the corresponding **\<GridItem>** component to **1** and **2**. 92 93 94```ts 95GridItem() { 96 Text(key) 97 ... 98} 99.columnStart(1) 100.columnEnd(2) 101``` 102 103For the **=** key to span the fifth and sixth rows, set **rowStart** and **rowEnd** of the corresponding **\<GridItem>** component to **5** and **6** 104 105 106```ts 107GridItem() { 108 Text(key) 109 ... 110} 111.rowStart(5) 112.rowEnd(6) 113``` 114 115 116### Setting the Main Axis Direction 117 118When neither the number nor proportion is set for rows and columns in a grid layout, you can use the **layoutDirection** attribute to set the main axis direction and thereby specify the arrangement mode of child components. In addition, you can use the **minCount** and **maxCount** attributes to restrict the number of grid cells along the main axis. 119 120 **Figure 6** Main axis direction 121 122 123 124When **layoutDirection** is set to **Row**, child components are arranged from left to right. When a row is full, a new row will be added. When **layoutDirection** is set to **Column**, child components are arranged from top to bottom. When a column is full, a new column will be added. In this example, the **maxCount** attribute is set to **3**, indicating that the maximum number of grid cells displayed along the main axis is 3. 125 126 127```ts 128Grid() { 129 ... 130} 131.maxCount(3) 132.layoutDirection(GridDirection.Row) 133``` 134 135>**NOTE** 136> 137>- The **layoutDirection** attribute takes effect only when **rowsTemplate** and **columnsTemplate** are not set. In this case, child components are arranged in the direction set by **layoutDirection**. 138>- When only **rowsTemplate** is set, the main axis of the grid runs in the horizontal direction, and the cross axis runs in the vertical direction. 139>- When only **columnsTemplate** is set, the main axis of the grid runs in the vertical direction, and the cross axis runs in the horizontal direction. 140 141 142## Displaying Data in a Grid Layout 143 144The grid layout organizes its internal elements in two-dimensional layout mode, as shown in the following figure. 145 146**Figure 7** General office services 147 148 149 150The **\<Grid>** component can display a group of **\<GridItem>** child components in two-dimensional layout mode. 151 152 153```ts 154Grid() { 155 GridItem() { 156 Text('Conference') 157 ... 158 } 159 160 GridItem() { 161 Text('Sign-in') 162 ... 163 } 164 165 GridItem() { 166 Text ('Vote') 167 ... 168 } 169 170 GridItem() { 171 Text ('Print') 172 ... 173 } 174} 175.rowsTemplate('1fr 1fr') 176.rowsTemplate('1fr 1fr') 177``` 178 179For multiple **\<GridItem>** components with similar content structures, you are advised to nest them in **ForEach** statements to reduce repeated code. 180 181 182```ts 183@Component 184struct OfficeService { 185 @State services: Array<string> = ['Conference', 'Vote','Sign-in', 'Print'] 186 ... 187 188 build() { 189 Column() { 190 Grid() { 191 ForEach(this.services, service => { 192 GridItem() { 193 Text(service) 194 ... 195 } 196 }, service => service) 197 } 198 .rowsTemplate('1fr 1fr') 199 .rowsTemplate('1fr 1fr') 200 ... 201 } 202 ... 203 } 204} 205``` 206 207 208## Setting the Gap Between Rows and Columns 209 210The horizontal spacing between two grid cells is called row spacing, and the vertical spacing is called column spacing, as shown in the following figure. 211 212**Figure 8** Row spacing and column spacing 213 214 215 216You can use **rowsGap** and **columnsGap** to set the row spacing and column spacing of the grid layout. In the calculator shown in Figure 5, the row spacing is 15 vp, and the column spacing is 10vp. 217 218 219```ts 220Grid() { 221 ... 222} 223.columnsGap(10) 224.rowsGap(15) 225``` 226 227 228## Building a Scrollable Grid Layout 229 230The scrollable grid layout is often used on the file list, product list, video list, and similar pages, as shown in the following figure. When only the number or proportion is set for rows and columns, that is, only the **rowsTemplate** or **columnsTemplate** attribute is set, the elements in the grid are arranged in the configured direction. When the content goes beyond the display area, the grid can be scrolled. 231 232**Figure 9** Horizontal scrollable grid layout 233 234 235 236If **columnsTemplate** is set, the grid scrolls vertically. If **rowsTemplate** is set, the grid scrolls horizontally. 237 238In the horizontal scrollable grid layout shown in the preceding figure, **rowsTemplate** is set but **columnsTemplate** is not. When the content exceeds the width of the grid, the grid can scroll horizontally to display the content outside of the display area. 239 240 241```ts 242@Component 243struct Shopping { 244 @State services: Array<string> = ['Live', 'Premium', ...] 245 ... 246 247 build() { 248 Column({ space: 5 }) { 249 Grid() { 250 ForEach(this.services, (service: string, index) => { 251 GridItem() { 252 ... 253 } 254 .width('25%') 255 }, service => service) 256 } 257 .rowsTemplate('1fr 1fr') // Set only the rowsTemplate attribute. When the content exceeds the display area of the grid, the grid can be scrolled horizontally. 258 .rowsGap(15) 259 ... 260 } 261 ... 262 } 263} 264``` 265 266 267## Controlling the Scrolling Position 268 269Similar to the Back to top button in a list layout, the feature of controlling the scrolling position is commonly used in the grid layout, for example, page turning in the calendar application, as shown below. 270 271 **Figure 10** Page turning in the calendar application 272 273 274 275When the **\<Grid>** component is initialized, it can be bound to a [Scroller](../reference/arkui-ts/ts-container-scroll.md/#scroller) object for scrolling control. In this example, the [scrollPage](../reference/arkui-ts/ts-container-scroll.md/#scrollpage) API of the **Scroller** object is used to turn pages. 276 277 278```ts 279private scroller: Scroller = new Scroller() 280``` 281 282On the calendar page, when a user clicks the **Next** button, the application responds to the click event by setting the **next** parameter in the **scrollPage** API to **true** to scroll to the next page. 283 284 285```ts 286Column({ space: 5 }) { 287 Grid(this.scroller) { 288 ... 289 } 290 .columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr') 291 ... 292 293 Row({space: 20}) { 294 Button ('Previous') 295 .onClick(() => { 296 this.scroller.scrollPage({ 297 next: false 298 }) 299 }) 300 301 Button ('Next') 302 .onClick(() => { 303 this.scroller.scrollPage({ 304 next: true 305 }) 306 }) 307 } 308} 309... 310``` 311 312 313## Performance Optimization 314 315Just as [LazyForEach](../quick-start/arkts-rendering-control-lazyforeach.md) is recommended for [handling a long list](arkts-layout-development-create-list.md#handling-a-long-list), it is also recommended for a scrolling grid layout when a large number of grid items is involved. 316 317For details about the implementation, see the example in [LazyForEach: Lazy Data Loading](../quick-start/arkts-rendering-control-lazyforeach.md). 318 319When the grid is rendered in lazy loading mode, to improve the grid scrolling experience and minimize white blocks during grid scrolling, you can use the **cachedCount** parameter of the **\<Grid>** component. This parameter sets the number of grid items preloaded outside of the screen and is valid only in **LazyForEach**. 320 321Specifically, the number of the grid items to cache before and after the currently displayed one equals the value of **cachedCount** multiplied by the number of columns. Grid items that exceed the display and cache range are released. 322 323```ts 324Grid() { 325 LazyForEach(this.dataSource, item => { 326 GridItem() { 327 ... 328 } 329 }) 330} 331.cachedCount(3) 332``` 333 334> **NOTE** 335> 336> A greater **cachedCount** value may result in higher CPU and memory overhead of the UI. Adjust the value by taking into account both the comprehensive performance and user experience. 337<!--no_check-->