• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
9
10## Child Components
11
12Supported
13
14
15## APIs
16
17GridItem()
18
19
20## Attributes
21
22| Name| Type| Description|
23| -------- | -------- | -------- |
24| rowStart | number | Start row number of the component.|
25| rowEnd | number | End row number of the component.|
26| columnStart | number | Start column number of the component.|
27| columnEnd | number | End column number of the component.|
28| forceRebuild<sup>(deprecated)</sup> | boolean | Whether to re-create the component when it is being built.<br>This API is deprecated since API version 9. Whether to re-create the component is automatically determined based on the component attributes and child component changes. No manual configuration is required.<br>Default value: **false**|
29| selectable<sup>8+</sup> | boolean | Whether the grid item is selectable by the mouse.<br>**NOTE**<br>This attribute takes effect only when mouse frame selection is enabled for the parent **\<Grid>** container.<br>Default value: **true** |
30| selected<sup>10+</sup> | boolean | Whether the grid item is selected. This attribute supports [$$](../../quick-start/arkts-two-way-sync.md) for two-way binding of variables.<br>**NOTE**<br>This attribute must be used before the [style for the selected state](./ts-universal-attributes-polymorphic-style.md) is set. Otherwise, the style settings will not take effect.<br>Default value: **false**|
31
32>  **NOTE**
33>
34>  Rules for setting **rowStart**, **rowEnd**, **columnStart**, and **columnEnd**:
35>
36>  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.
37>
38>  Settings outside of the valid ranges do not take effect.
39>
40>  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.
41>
42>  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.
43>
44>  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.
45>
46>  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.
47>
48>  In the grid that has neither **columnTemplate** nor **rowTemplate** set, the row and column number attributes do not work.
49
50
51## Events
52
53| Name| Description|
54| -------- | -------- |
55| onSelect(event: (isSelected: boolean) =&gt; void)<sup>8+</sup> | Triggered when the selected state of the grid item changes.<br>**isSelected**: returns **true** if the grid item is being selected by the mouse; returns **false** otherwise. |
56
57
58## Example
59
60```ts
61// xxx.ets
62@Entry
63@Component
64struct GridItemExample {
65  @State numbers: string[] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"]
66
67  build() {
68    Column() {
69      Grid() {
70        GridItem() {
71          Text('4')
72            .fontSize(16)
73            .backgroundColor(0xFAEEE0)
74            .width('100%')
75            .height('100%')
76            .textAlign(TextAlign.Center)
77        }.rowStart(1).rowEnd(2).columnStart(1).columnEnd(2) // Set valid row and column numbers.
78
79        ForEach(this.numbers, (item: string) => {
80          GridItem() {
81            Text(item)
82              .fontSize(16)
83              .backgroundColor(0xF9CF93)
84              .width('100%')
85              .height('100%')
86              .textAlign(TextAlign.Center)
87          }
88        }, (item: string) => item)
89
90        GridItem() {
91          Text('5')
92            .fontSize(16)
93            .backgroundColor(0xDBD0C0)
94            .width('100%')
95            .height('100%')
96            .textAlign(TextAlign.Center)
97        }.columnStart(1).columnEnd(4) // Set only the column numbers. The layout does not start from the first column.
98      }
99      .columnsTemplate('1fr 1fr 1fr 1fr 1fr')
100      .rowsTemplate('1fr 1fr 1fr 1fr 1fr')
101      .width('90%').height(300)
102    }.width('100%').margin({ top: 5 })
103  }
104}
105```
106
107![en-us_image_0000001174582870](figures/en-us_image_0000001174582870.gif)
108