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