1# GridItem
2
3The **GridItem** component provides a single item in a grid.
4
5>  **NOTE**
6>
7>  * This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8>  * This component can be used only as a child of [Grid](ts-container-grid.md).
9>  * When this component is used with **LazyForEach**, its child components are created when it is created. When this component is used with **if/else** or **ForEach**, or when the parent component is **Grid**, its child components are created when it is laid out.
10
11
12## Child Components
13
14This component can contain a single child component.
15
16## APIs
17
18GridItem(value?: GridItemOptions)
19
20**Atomic service API**: This API can be used in atomic services since API version 11.
21
22**System capability**: SystemCapability.ArkUI.ArkUI.Full
23
24**Parameters**
25
26| Name| Type                                     | Mandatory| Description                                                    |
27| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ |
28| value<sup>11+</sup>  | [GridItemOptions](#griditemoptions11) | No  | Parameters of the grid item, containing the **style** parameter of the [GridItemStyle](#griditemstyle11) enum type.|
29
30## Attributes
31
32### rowStart
33
34rowStart(value: number)
35
36Sets the start row number of the component.
37
38**Atomic service API**: This API can be used in atomic services since API version 11.
39
40**System capability**: SystemCapability.ArkUI.ArkUI.Full
41
42**Parameters**
43
44| Name| Type  | Mandatory| Description              |
45| ------ | ------ | ---- | ------------------ |
46| value  | number | Yes  | Start row number of the component.<br>In situations where you need to designate the start row and column numbers as well as the number of rows and columns a grid item spans, you are advised to use the [layoutOptions parameter](ts-container-grid.md#gridlayoutoptions10) of the **Grid** component. For reference, see [Grid Example 1](ts-container-grid.md#example-1) and [Grid Example 3](ts-container-grid.md#example-3).|
47
48### rowEnd
49
50rowEnd(value: number)
51
52Sets the end row number of the component.
53
54**Atomic service API**: This API can be used in atomic services since API version 11.
55
56**System capability**: SystemCapability.ArkUI.ArkUI.Full
57
58**Parameters**
59
60| Name| Type  | Mandatory| Description              |
61| ------ | ------ | ---- | ------------------ |
62| value  | number | Yes  | End row number of the component.<br>In situations where you need to designate the start row and column numbers as well as the number of rows and columns a grid item spans, you are advised to use the [layoutOptions parameter](ts-container-grid.md#gridlayoutoptions10) of the **Grid** component. For reference, see [Grid Example 1](ts-container-grid.md#example-1) and [Grid Example 3](ts-container-grid.md#example-3).|
63
64### columnStart
65
66columnStart(value: number)
67
68Sets the start column number of the component.
69
70**Atomic service API**: This API can be used in atomic services since API version 11.
71
72**System capability**: SystemCapability.ArkUI.ArkUI.Full
73
74**Parameters**
75
76| Name| Type  | Mandatory| Description              |
77| ------ | ------ | ---- | ------------------ |
78| value  | number | Yes  | Start column number of the component.<br>In situations where you need to designate the start row and column numbers as well as the number of rows and columns a grid item spans, you are advised to use the [layoutOptions parameter](ts-container-grid.md#gridlayoutoptions10) of the **Grid** component. For reference, see [Grid Example 1](ts-container-grid.md#example-1) and [Grid Example 3](ts-container-grid.md#example-3).|
79
80### columnEnd
81
82columnEnd(value: number)
83
84Sets the end column number of the component.
85
86**Atomic service API**: This API can be used in atomic services since API version 11.
87
88**System capability**: SystemCapability.ArkUI.ArkUI.Full
89
90**Parameters**
91
92| Name| Type  | Mandatory| Description              |
93| ------ | ------ | ---- | ------------------ |
94| value  | number | Yes  | End column number of the component.<br>In situations where you need to designate the start row and column numbers as well as the number of rows and columns a grid item spans, you are advised to use the [layoutOptions parameter](ts-container-grid.md#gridlayoutoptions10) of the **Grid** component. For reference, see [Grid Example 1](ts-container-grid.md#example-1) and [Grid Example 3](ts-container-grid.md#example-3).|
95
96>  **NOTE**
97>
98>  In situations where you need to designate the start row and column numbers as well as the number of rows and columns a grid item spans, you are advised to use the [layoutOptions parameter](ts-container-grid.md#gridlayoutoptions10) of the **Grid** component. For reference, see [Grid Example 1](ts-container-grid.md#example-1) and [Grid Example 3](ts-container-grid.md#example-3).
99>
100>  Rules for setting **rowStart**, **rowEnd**, **columnStart**, and **columnEnd**:
101>
102>  The valid value range of **rowStart** and **rowEnd** is 0 to the total number of rows minus 1. The valid value range of **columnStart** and **columnEnd** is 0 to the total number of columns minus 1.
103>
104>  If **rowStart**, **rowEnd**, **columnStart**, or **columnEnd** is set, the grid item occupies the specified number of rows (**rowEnd** - **rowStart** + 1) or columns (**columnEnd** - **columnStart** + 1).
105>
106>  Settings outside of the valid ranges do not take effect.
107>
108>  In the grid that has both **columnTemplate** and **rowTemplate** set, grid items that have **rowStart**/**rowEnd** or **columnStart**/**columnEnd** set are laid out in a row-by-row then column-by-column manner.
109>
110>  In the grid that has only **columnTemplate** set, grid items that have **columnStart**/**columnEnd** set are laid out in the specific columns. If there are already grid items in those columns, the grid items will be laid out in another row.
111>
112>  In the grid that has only **rowTemplate** set, grid items that have **rowStart**/**rowEnd** set are laid out in the specific rows. If there are already grid items in those rows, the grid items will be laid out in another column.
113>
114>  In the grid that has only **columnTemplate** set, grid items whose row or column number settings are invalid are laid out in a row-by-row then column-by-column manner.
115>
116>  In the grid that has neither **columnTemplate** nor **rowTemplate** set, the row and column number attributes do not work.
117
118### forceRebuild<sup>(deprecated)</sup>
119
120forceRebuild(value: boolean)
121
122Whether to re-create the component when it is being built. Whether to re-create the component is automatically determined based on the component attributes and child component changes. No manual configuration is required.
123
124This API is deprecated since API version 9.
125
126**System capability**: SystemCapability.ArkUI.ArkUI.Full
127
128**Parameters**
129
130| Name| Type   | Mandatory| Description                                                   |
131| ------ | ------- | ---- | ------------------------------------------------------- |
132| value  | boolean | Yes  | Sets whether to re-create the component when it is being built.<br>Default value: **false**|
133
134### selectable<sup>8+</sup>
135
136selectable(value: boolean)
137
138Sets whether the grid item is selectable in the mouse selection box area. This attribute takes effect only when mouse box selection is enabled for the parent **Grid** container.
139
140This attribute must be used before the [style for the selected state](./ts-universal-attributes-polymorphic-style.md#statestyles) is set.
141
142**Atomic service API**: This API can be used in atomic services since API version 11.
143
144**System capability**: SystemCapability.ArkUI.ArkUI.Full
145
146**Parameters**
147
148| Name| Type   | Mandatory| Description                                                 |
149| ------ | ------- | ---- | ----------------------------------------------------- |
150| value  | boolean | Yes  | Whether the grid item is selectable in the mouse selection box area.<br>Default value: **true**|
151
152### selected<sup>10+</sup>
153
154selected(value: boolean)
155
156Sets whether the grid item is selected. This attribute supports two-way binding through [$$](../../../quick-start/arkts-two-way-sync.md).
157
158This attribute must be used before the [style for the selected state](./ts-universal-attributes-polymorphic-style.md#statestyles) is set.
159
160**Atomic service API**: This API can be used in atomic services since API version 11.
161
162**System capability**: SystemCapability.ArkUI.ArkUI.Full
163
164**Parameters**
165
166| Name| Type   | Mandatory| Description                                    |
167| ------ | ------- | ---- | ---------------------------------------- |
168| value  | boolean | Yes  | Whether the grid item is selected.<br>Default value: **false**|
169
170## GridItemOptions<sup>11+</sup>
171
172**Atomic service API**: This API can be used in atomic services since API version 12.
173
174**System capability**: SystemCapability.ArkUI.ArkUI.Full
175
176| Name | Type                 | Mandatory| Description                        |
177| ----- | -------------------- | ---- | ---------------------------- |
178| style | [GridItemStyle](#griditemstyle11) | No  | Style of the grid item.<br>Default value: **GridItemStyle.NONE**<br>If this parameter is set to **GridItemStyle.NONE**, no style is applied.<br>If this parameter is set to **GridItemStyle.PLAIN**, the grid item is in hover or press style depending on the state.|
179
180## GridItemStyle<sup>11+</sup>
181
182**Atomic service API**: This API can be used in atomic services since API version 12.
183
184**System capability**: SystemCapability.ArkUI.ArkUI.Full
185
186| Name |Value| Description                  |
187| ----- |----| ------------------------ |
188| NONE  |  0 | No style.                |
189| PLAIN |  1 | Hover or press style.|
190
191> **NOTE**
192>
193> To set the focused style for the grid item, the grid container must have paddings of greater than 4 vp for accommodating the focus frame of the grid item.
194
195## Events
196
197### onSelect<sup>8+</sup>
198
199onSelect(event: (isSelected: boolean) =&gt; void)
200
201Triggered when the selected state of the grid item changes.
202
203**Atomic service API**: This API can be used in atomic services since API version 11.
204
205**System capability**: SystemCapability.ArkUI.ArkUI.Full
206
207**Parameters**
208
209| Name    | Type   | Mandatory| Description                                                        |
210| ---------- | ------- | ---- | ------------------------------------------------------------ |
211| isSelected | boolean | Yes  | Returns **true** if the grid item is selected in the mouse selection box area; returns **false** otherwise.|
212
213## Example
214
215### Example 1
216
217```ts
218// xxx.ets
219@Entry
220@Component
221struct GridItemExample {
222  @State numbers: string[] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"]
223
224  build() {
225    Column() {
226      Grid() {
227        GridItem() {
228          Text('4')
229            .fontSize(16)
230            .backgroundColor(0xFAEEE0)
231            .width('100%')
232            .height('100%')
233            .textAlign(TextAlign.Center)
234        }.rowStart(1).rowEnd(2).columnStart(1).columnEnd(2) // Set valid row and column numbers.
235
236        ForEach(this.numbers, (item: string) => {
237          GridItem() {
238            Text(item)
239              .fontSize(16)
240              .backgroundColor(0xF9CF93)
241              .width('100%')
242              .height('100%')
243              .textAlign(TextAlign.Center)
244          }
245        }, (item: string) => item)
246
247        GridItem() {
248          Text('5')
249            .fontSize(16)
250            .backgroundColor(0xDBD0C0)
251            .width('100%')
252            .height('100%')
253            .textAlign(TextAlign.Center)
254        }.columnStart(1).columnEnd(4) // Set only the column numbers. The layout does not start from the first column.
255      }
256      .columnsTemplate('1fr 1fr 1fr 1fr 1fr')
257      .rowsTemplate('1fr 1fr 1fr 1fr 1fr')
258      .width('90%').height(300)
259    }.width('100%').margin({ top: 5 })
260  }
261}
262```
263
264![en-us_image_0000001174582870](figures/en-us_image_0000001174582870.gif)
265
266### Example 2
267
268This example shows how to use **GridItemOptions**.
269
270```ts
271// xxx.ets
272@Entry
273@Component
274struct GridItemExample {
275  @State numbers: String[] = ['0', '1', '2']
276
277  build() {
278    Column({ space: 5 }) {
279      Grid() {
280        ForEach(this.numbers, (day: string) => {
281          ForEach(this.numbers, (day: string) => {
282            GridItem({style:GridItemStyle.NONE}) {
283              Text(day)
284                .fontSize(16)
285                .width('100%')
286                .height('100%')
287                .textAlign(TextAlign.Center)
288                .focusable(true)
289            }
290            .backgroundColor(0xF9CF93)
291          }, (day: string) => day)
292        }, (day: string) => day)
293      }
294      .columnsTemplate('1fr 1fr 1fr')
295      .rowsTemplate('1fr 1fr')
296      .columnsGap(4)
297      .rowsGap(4)
298      .width('60%')
299      .backgroundColor(0xFAEEE0)
300      .height(150)
301      .padding('4vp')
302
303      Grid() {
304        ForEach(this.numbers, (day: string) => {
305          ForEach(this.numbers, (day: string) => {
306            GridItem({style:GridItemStyle.PLAIN}) {
307              Text(day)
308                .fontSize(16)
309                .width('100%')
310                .height('100%')
311                .textAlign(TextAlign.Center)
312                .focusable(true)
313            }
314            .backgroundColor(0xF9CF93)
315          }, (day: string) => day)
316        }, (day: string) => day)
317      }
318      .columnsTemplate('1fr 1fr 1fr')
319      .rowsTemplate('1fr 1fr')
320      .columnsGap(4)
321      .rowsGap(4)
322      .width('60%')
323      .backgroundColor(0xFAEEE0)
324      .height(150)
325      .padding('4vp')
326    }.width('100%').margin({ top: 5 })
327  }
328}
329```
330
331![en-us_image_griditem_griditemoptions](figures/en-us_image_griditem_griditemoptions.png)
332