1e41f4b71Sopenharmony_ci# Creating a Grid (Grid/GridItem)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## Overview
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciThe grid layout consists of cells formed by rows and columns. You can specify the cells where items are located to create 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.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ciArkUI provides the [Grid](../reference/apis-arkui/arkui-ts/ts-container-grid.md) and [GridItem](../reference/apis-arkui/arkui-ts/ts-container-griditem.md) components for building grid layouts. **Grid** is a container for defining the grid layout, while **GridItem** is a child component in the container. The **Grid** component allows creation of child components using methods such as [if/else](../quick-start/arkts-rendering-control-ifelse.md), [ForEach](../quick-start/arkts-rendering-control-foreach.md), and [LazyForEach](../quick-start/arkts-rendering-control-lazyforeach.md).
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## Layout and Constraints
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciEach item in the **Grid** container corresponds to a **GridItem** component, as shown below.
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci**Figure 1** Relationship between \<Grid> and \<GridItem> components
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci![en-us_image_0000001511900472](figures/en-us_image_0000001511900472.png)
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci>**NOTE**
20e41f4b71Sopenharmony_ci>
21e41f4b71Sopenharmony_ci>The **Grid** component accepts only **GridItem** as its child.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciThe 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 it has its size changed, its child components and spacing are adjusted proportionally. By leveraging these layout capabilities, you can build grid layouts of different styles, as shown below.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**Figure 2** Grid layout
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci![en-us_image_0000001562700473](figures/en-us_image_0000001562700473.png)
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ciThe size of the **Grid** component follows its width and height settings (if configured) or adapts to the size of its parent component.
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ciDepending on the settings of the quantity and proportion of rows and columns, the **Grid** component behaves as follows:
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci- If both the quantity and proportion are set for rows or columns, the **Grid** component displays elements only in the set number of rows or columns, and it cannot be scrolled. (This layout mode is recommended.)
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci- If only the quantity or proportion is set for rows or columns, the **Grid** component lays out elements in the specified direction, and it can be scrolled to display excess elements.
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci- If neither the quantity nor the proportion is set for rows or columns, the **Grid** component lays out elements in the layout direction. The number of rows and columns is determined by the layout direction and the width and height of the grid. Elements that exceed the range of rows and columns are not displayed, and the **Grid** component cannot be scrolled.
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci## Setting the Arrangement Mode
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci### Setting the Number and Proportion of Rows and Columns
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ciYou 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.
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ciThe values of **rowsTemplate** and **columnsTemplate** are a string consisting of 'number+fr' segments, separated by spaces. Wherein **fr** indicates the number of rows or columns in the grid layout, and the number in front of **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.
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci**Figure 3** Example of the proportion of rows and columns
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci![en-us_image_0000001562820833](figures/en-us_image_0000001562820833.png)
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ciThe 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.
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ciThis layout can be implemented by setting **rowsTemplate** to **'1fr 1fr 1fr'** and **columnsTemplate** to **'1fr 2fr 1fr'**.
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci```ts
59e41f4b71Sopenharmony_ciGrid() {
60e41f4b71Sopenharmony_ci  ...
61e41f4b71Sopenharmony_ci}
62e41f4b71Sopenharmony_ci.rowsTemplate('1fr 1fr 1fr')
63e41f4b71Sopenharmony_ci.columnsTemplate('1fr 2fr 1fr')
64e41f4b71Sopenharmony_ci```
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci>**NOTE**
67e41f4b71Sopenharmony_ci>
68e41f4b71Sopenharmony_ci>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/apis-arkui/arkui-ts/ts-container-grid.md#attributes).
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci### Setting the Number of Rows and Columns Occupied by a Child Component
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ciIn 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 allow a single grid cell in a grid to span multiple rows or columns, passing appropriate [GridLayoutOptions](../reference/apis-arkui/arkui-ts/ts-container-grid.md#gridlayoutoptions10) when creating the grid. Use **irregularIndexes** and **onGetIrregularSizeByIndex** for grids with only **rowsTemplate** or **columnsTemplate**, and **onGetRectByIndex** for grids with both.
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci**Figure 4** Uneven grid layout
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci![en-us_image_0000001511900480](figures/en-us_image_0000001511900480.png)
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ciA 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 0 and increase incrementally.
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci**Figure 5** Calculator 
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci![en-us_image_0000001511421292](figures/en-us_image_0000001511421292.png)
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ciIn the grid, use the **onGetRectByIndex** callback to return the array [rowStart, columnStart, rowSpan, columnSpan] to achieve a layout that spans rows and columns, wherein **rowStart** and **rowEnd** indicate the start and end row numbers of the current element, and **columnStart** and **columnEnd** indicate the start and end column numbers of the current element.
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ciTo make the **0** key span across the first and second columns, and the **=** key span across the fifth and sixth rows, set **onGetRectByIndex** for **0** and **=** as follows: for **0**, set **rowStart** and **columnStart** at **5** and **0**, and **rowSpan** and **columnSpan** at **1** and **2**; for **=**, set **rowStart** and **columnStart** at **4** and **3**, and **rowSpan** and **columnSpan** at **2** and **1**.
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci```ts
91e41f4b71Sopenharmony_cilayoutOptions: GridLayoutOptions = {
92e41f4b71Sopenharmony_ci  regularSize: [1, 1],
93e41f4b71Sopenharmony_ci  onGetRectByIndex: (index: number) => {
94e41f4b71Sopenharmony_ci    if (index = = key1) { // key1 is the index of the 0 key.
95e41f4b71Sopenharmony_ci      return [5, 0, 1, 2]
96e41f4b71Sopenharmony_ci    } else if (index == key2) { // key2 is the index of the = key.
97e41f4b71Sopenharmony_ci      return [4, 3, 2, 1]
98e41f4b71Sopenharmony_ci    }
99e41f4b71Sopenharmony_ci    // ...
100e41f4b71Sopenharmony_ci    // Here, you need to return the positions of other items based on the specific layout.
101e41f4b71Sopenharmony_ci  }
102e41f4b71Sopenharmony_ci}
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ciGrid(undefined, this.layoutOptions) {
105e41f4b71Sopenharmony_ci  // ...
106e41f4b71Sopenharmony_ci}
107e41f4b71Sopenharmony_ci.columnsTemplate('1fr 1fr 1fr 1fr')
108e41f4b71Sopenharmony_ci.rowsTemplate('2fr 1fr 1fr 1fr 1fr 1fr')
109e41f4b71Sopenharmony_ci```
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci### Setting the Main Axis Direction
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ciWhen 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.
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci**Figure 6** Main axis direction 
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci![en-us_image_0000001562700469](figures/en-us_image_0000001562700469.png)
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ciWhen **layoutDirection** is set to **Row**, child components are arranged from left to right. When a row is full, a new row is added. When **layoutDirection** is set to **Column**, child components are arranged from top to bottom. When a column is full, a new column is 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.
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci```ts
124e41f4b71Sopenharmony_ciGrid() {
125e41f4b71Sopenharmony_ci  ...
126e41f4b71Sopenharmony_ci}
127e41f4b71Sopenharmony_ci.maxCount(3)
128e41f4b71Sopenharmony_ci.layoutDirection(GridDirection.Row)
129e41f4b71Sopenharmony_ci```
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci>**NOTE**
132e41f4b71Sopenharmony_ci>
133e41f4b71Sopenharmony_ci>- 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**.
134e41f4b71Sopenharmony_ci>- 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.
135e41f4b71Sopenharmony_ci>- 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.
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci## Displaying Data in a Grid Layout
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ciThe grid layout organizes its internal elements in two-dimensional layout mode, as shown in the following figure.
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci**Figure 7** General office services 
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci![en-us_image_0000001563060729](figures/en-us_image_0000001563060729.png)
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ciThe **Grid** component can display a group of **GridItem** child components in two-dimensional layout mode.
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci```ts
150e41f4b71Sopenharmony_ciGrid() {
151e41f4b71Sopenharmony_ci  GridItem() {
152e41f4b71Sopenharmony_ci    Text('Conference')
153e41f4b71Sopenharmony_ci      ...
154e41f4b71Sopenharmony_ci  }
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci  GridItem() {
157e41f4b71Sopenharmony_ci    Text('Sign-in')
158e41f4b71Sopenharmony_ci      ...
159e41f4b71Sopenharmony_ci  }
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci  GridItem() {
162e41f4b71Sopenharmony_ci    Text ('Vote')
163e41f4b71Sopenharmony_ci      ...
164e41f4b71Sopenharmony_ci  }
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci  GridItem() {
167e41f4b71Sopenharmony_ci    Text ('Print')
168e41f4b71Sopenharmony_ci      ...
169e41f4b71Sopenharmony_ci  }
170e41f4b71Sopenharmony_ci}
171e41f4b71Sopenharmony_ci.rowsTemplate('1fr 1fr')
172e41f4b71Sopenharmony_ci.columnsTemplate('1fr 1fr')
173e41f4b71Sopenharmony_ci```
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ciFor multiple **GridItem** components with similar content structures, you are advised to nest them in **ForEach** statements to reduce repeated code.
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci```ts
179e41f4b71Sopenharmony_ci@Entry
180e41f4b71Sopenharmony_ci@Component
181e41f4b71Sopenharmony_cistruct OfficeService {
182e41f4b71Sopenharmony_ci  @State services: Array<string> = ['Conference', 'Vote','Sign-in', 'Print']
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci  build() {
185e41f4b71Sopenharmony_ci    Column() {
186e41f4b71Sopenharmony_ci      Grid() {
187e41f4b71Sopenharmony_ci        ForEach(this.services, (service:string) => {
188e41f4b71Sopenharmony_ci          GridItem() {
189e41f4b71Sopenharmony_ci            Text(service)
190e41f4b71Sopenharmony_ci          }
191e41f4b71Sopenharmony_ci        }, (service:string):string => service)
192e41f4b71Sopenharmony_ci      }
193e41f4b71Sopenharmony_ci      .rowsTemplate(('1fr 1fr') as string)
194e41f4b71Sopenharmony_ci      .columnsTemplate(('1fr 1fr') as string)
195e41f4b71Sopenharmony_ci    }
196e41f4b71Sopenharmony_ci  }
197e41f4b71Sopenharmony_ci}
198e41f4b71Sopenharmony_ci```
199e41f4b71Sopenharmony_ci
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci## Setting the Gap Between Rows and Columns
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ciThe horizontal spacing between two grid cells is called row spacing, and the vertical spacing is called column spacing, as shown in the following figure.
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci**Figure 8** Row spacing and column spacing 
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci![en-us_image_0000001511580908](figures/en-us_image_0000001511580908.png)
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ciYou 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.
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci```ts
213e41f4b71Sopenharmony_ciGrid() {
214e41f4b71Sopenharmony_ci  ...
215e41f4b71Sopenharmony_ci}
216e41f4b71Sopenharmony_ci.columnsGap(10)
217e41f4b71Sopenharmony_ci.rowsGap(15)
218e41f4b71Sopenharmony_ci```
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci## Building a Scrollable Grid Layout
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ciThe 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.
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci**Figure 9** Horizontal scrollable grid layout
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci![en-us_image_0000001511740512](figures/en-us_image_0000001511740512.gif)
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ciIf **columnsTemplate** is set, the grid scrolls vertically. If **rowsTemplate** is set, the grid scrolls horizontally.
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ciIn 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.
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci```ts
235e41f4b71Sopenharmony_ci@Entry
236e41f4b71Sopenharmony_ci@Component
237e41f4b71Sopenharmony_cistruct Shopping {
238e41f4b71Sopenharmony_ci  @State services: Array<string> = ['Live', 'Premium']
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci  build() {
241e41f4b71Sopenharmony_ci    Column({ space: 5 }) {
242e41f4b71Sopenharmony_ci      Grid() {
243e41f4b71Sopenharmony_ci        ForEach(this.services, (service: string, index) => {
244e41f4b71Sopenharmony_ci          GridItem() {
245e41f4b71Sopenharmony_ci          }
246e41f4b71Sopenharmony_ci          .width('25%')
247e41f4b71Sopenharmony_ci        }, (service:string):string => service)
248e41f4b71Sopenharmony_ci      }
249e41f4b71Sopenharmony_ci      .rowsTemplate('1fr 1fr') // Set only the rowsTemplate attribute. When the content exceeds the display area of the grid, the grid can be scrolled horizontally.
250e41f4b71Sopenharmony_ci      .rowsGap(15)
251e41f4b71Sopenharmony_ci    }
252e41f4b71Sopenharmony_ci  }
253e41f4b71Sopenharmony_ci}
254e41f4b71Sopenharmony_ci```
255e41f4b71Sopenharmony_ci
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_ci## Controlling the Scrolling Position
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ciSimilar 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.
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci**Figure 10** Page turning in the calendar application 
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci![en-us_image_0000001562940549](figures/en-us_image_0000001562940549.gif)
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ciWhen the **Grid** component is initialized, it can be bound to a [Scroller](../reference/apis-arkui/arkui-ts/ts-container-scroll.md#scroller) object for scrolling control. In this example, the [scrollPage](../reference/apis-arkui/arkui-ts/ts-container-scroll.md#scrollpage9) API of the **Scroller** object is used to turn pages.
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci```ts
269e41f4b71Sopenharmony_ciprivate scroller: Scroller = new Scroller()
270e41f4b71Sopenharmony_ci```
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ciOn 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.
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ci
275e41f4b71Sopenharmony_ci```ts
276e41f4b71Sopenharmony_ciColumn({ space: 5 }) {
277e41f4b71Sopenharmony_ci  Grid(this.scroller) {
278e41f4b71Sopenharmony_ci  }
279e41f4b71Sopenharmony_ci  .columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr 1fr')
280e41f4b71Sopenharmony_ci
281e41f4b71Sopenharmony_ci  Row({space: 20}) {
282e41f4b71Sopenharmony_ci    Button ('Previous')
283e41f4b71Sopenharmony_ci      .onClick(() => {
284e41f4b71Sopenharmony_ci        this.scroller.scrollPage({
285e41f4b71Sopenharmony_ci          next: false
286e41f4b71Sopenharmony_ci        })
287e41f4b71Sopenharmony_ci      })
288e41f4b71Sopenharmony_ci
289e41f4b71Sopenharmony_ci    Button ('Next')
290e41f4b71Sopenharmony_ci      .onClick(() => {
291e41f4b71Sopenharmony_ci        this.scroller.scrollPage({
292e41f4b71Sopenharmony_ci          next: true
293e41f4b71Sopenharmony_ci        })
294e41f4b71Sopenharmony_ci      })
295e41f4b71Sopenharmony_ci  }
296e41f4b71Sopenharmony_ci}
297e41f4b71Sopenharmony_ci```
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci## Performance Optimization
301e41f4b71Sopenharmony_ci
302e41f4b71Sopenharmony_ciJust 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.
303e41f4b71Sopenharmony_ci
304e41f4b71Sopenharmony_ciFor details about the implementation, see the example in [LazyForEach: Lazy Data Loading](../quick-start/arkts-rendering-control-lazyforeach.md).
305e41f4b71Sopenharmony_ci
306e41f4b71Sopenharmony_ciWhen 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**.
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_ci  Specifically, 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.
309e41f4b71Sopenharmony_ci
310e41f4b71Sopenharmony_ci```ts
311e41f4b71Sopenharmony_ciGrid() {
312e41f4b71Sopenharmony_ci  LazyForEach(this.dataSource, () => {
313e41f4b71Sopenharmony_ci    GridItem() {
314e41f4b71Sopenharmony_ci    }
315e41f4b71Sopenharmony_ci  })
316e41f4b71Sopenharmony_ci}
317e41f4b71Sopenharmony_ci.cachedCount(3)
318e41f4b71Sopenharmony_ci```
319e41f4b71Sopenharmony_ci
320e41f4b71Sopenharmony_ci>**NOTE**
321e41f4b71Sopenharmony_ci>
322e41f4b71Sopenharmony_ci>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.
323e41f4b71Sopenharmony_ci
324