• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Grid Layout
2
3
4The **\<grid-container>** component is the root container of the grid layout. Within the root container, you can use **\<grid-row>** and **\<grid-col>** for the grid layout. For details, see [Grid-container](../reference/arkui-js/js-components-grid-container.md).
5
6
7## Creating a \<grid-container> Component
8
9Create a **\<grid-container>** component in the .hml file under **pages/index** and add a [\<Grid-row>](../reference/arkui-js/js-components-grid-row.md) child component.
10
11
12```html
13<!-- index.hml -->
14<div class="container">
15  <grid-container id="mygrid" columns="5" gutter="20px" style="background-color: pink;">
16    <grid-row style="height:100px;justify-content:space-around;width: 80%;background-color: #f67002;margin-left:
17      10%;"></grid-row>
18    <grid-row style="height:300px;justify-content:space-around;background-color: #ffcf00;width: 100%;"></grid-row>
19    <grid-row style="height:150px;justify-content:space-around;background-color: #032cf8;width: 100%;"></grid-row>
20  </grid-container>
21</div>
22```
23
24
25```css
26/* xxx.css */
27.container{
28  flex-direction: column;
29  background-color: #F1F3F5;
30  width: 100%;
31  height: 100%;
32  justify-content: center;
33  align-items: center;
34}
35```
36
37![en-us_image_0000001276162725](figures/en-us_image_0000001276162725.png)
38
39> **NOTE**
40>
41> **\<grid-container>** supports only **\<grid-row>** as a child component.
42
43
44## Methods
45
46Touch the **\<grid-container>** component to call the **getColumns**, **getColumnWidth**, and **getGutterWidth** methods to return the number of columns in the grid container, and column width and gutter width of the grid container. Press and hold the component to call the **getSizeType** method to return the size-responsive type of the grid container (**xs**|**sm**|**md**|**lg**).
47
48
49```html
50<!-- index.hml -->
51<div class="container">
52  <grid-container id="mygrid" columns="6" gutter="20px" style="background-color: pink;padding-top: 100px;"
53    onclick="getColumns" onlongpress="getSizeType">
54    <grid-row style="height:100px;justify-content:space-around;background-color: #4cedf3;width: 20%;margin-left:
55      40%;"></grid-row>
56    <grid-row style="height:150px;justify-content:space-around;background-color: #4cbff3;width: 50%;margin-left:
57      25%;"></grid-row>
58    <grid-row style="height:200px;justify-content:space-around;background-color: #465ff6;width: 80%;margin-left:
59      10%;"></grid-row>
60    <grid-row style="height:200px;justify-content:space-around;background-color: #5011ec;width: 100%;"></grid-row>
61  </grid-container>
62</div>
63```
64
65
66```css
67/* xxx.css */
68.container{
69  flex-direction: column;
70  background-color: #F1F3F5;
71  width: 100%;
72  height: 100%;
73  justify-content: center;
74  align-items: center;
75}
76```
77
78
79```js
80// index.js
81import promptAction from '@ohos.promptAction';
82export default {
83  data:{
84    gutterWidth:'',
85    columnWidth:'',
86    columns:'',
87  },
88  getColumns(){
89    this.$element('mygrid').getColumnWidth((result)=>{
90      this.columnWidth = result;
91    })
92    this.$element('mygrid').getGutterWidth((result)=>{
93      this.gutterWidth = result;
94    })
95    this.$element('mygrid').getColumns((result)=>{
96      this.columns= result;
97    })
98    setTimeout(()=>{
99      promptAction.showToast({duration:5000,message:'columnWidth:'+this.columnWidth+',gutterWidth:'+
100      this.gutterWidth+',getColumns:'+this.columns})
101    })
102  },
103  getSizeType(){
104      this.$element('mygrid').getSizeType((result)=>{
105      promptAction.showToast({duration:2000,message:'get size type:'+result})
106    })
107  },
108}
109```
110
111![en-us_image_0000001231843088](figures/en-us_image_0000001231843088.gif)
112
113
114## Adding \<grid-col>
115
116After adding a **\<grid-row>** child component to **\<grid-container>**, add a **\<grid-col>** child component to **\<grid-row>** to form a layout.
117
118
119```html
120<!-- index.hml -->
121<div class="container">
122  <grid-container id="mygrid" columns="4" gutter="0" style="background-color: pink;" onclick="getColumns" onlongpress="getSizeType">
123    <grid-row style="height: 100px;justify-content: space-around;background-color: #4cbff3;width: 100%;">
124      <grid-col span="0">
125        <div style="align-items: center;justify-content: center;height: 100%;width: 100%;">
126          <text style="color: dodgerblue;" onclick="getCol">top</text>
127        </div>
128      </grid-col>
129    </grid-row>
130    <grid-row style="height:500px;justify-content:space-around;background-color: #3b55ef;width: 100%;">
131      <grid-col span="0" style="width: 20%;">
132        <div style="align-items: center;justify-content: center;height: 100%;width: 100%;">
133          <text style="color: dodgerblue;">left</text>
134        </div>
135      </grid-col>
136      <grid-col span="0" style="background-color:orange;width: 80%;">
137        <div style="width: 100%;height: 100%;align-items: center;justify-content: center;">
138          <text>right</text>
139        </div>
140      </grid-col>
141    </grid-row>
142    <grid-row style="height: 100px;justify-content: space-around;background-color: #4cbff3;width: 100%;">
143      <grid-col style="background-color:#c075ef;" span="0">
144        <div style="width: 100%;height: 100%;padding: 20px;align-items: center;justify-content: center;">
145          <text>bottom</text>
146        </div>
147      </grid-col>
148    </grid-row>
149  </grid-container>
150</div>
151```
152
153
154```css
155/* xxx.css */
156.container{
157  flex-direction: column;
158  background-color: #F1F3F5;
159  width: 100%;
160  height: 100%;
161  justify-content: center;
162  align-items: center;
163}
164text{
165  color: white;
166  font-size: 40px;
167
168```
169
170![en-us_image_0000001231683124](figures/en-us_image_0000001231683124.png)
171
172> **NOTE**
173>
174> **\<grid-row>** supports only **\<grid-col>** as a child component. You can add content only to **\<grid-col>**.
175
176
177## Example Scenario
178
179In this example, the content in the list is output cyclically to create a grid layout. When the user pulls down the screen, the **refresh** method is triggered. In this case, a piece of data is added to the list and **setTimeout** is set to refresh the request data.
180
181
182```html
183<!-- index.hml -->
184<div class="container">
185  <refresh refreshing="{{fresh}}" onrefresh="refresh">
186    <grid-container id="mygrid" gutter="20" style="margin: 10px;">
187      <grid-row style="height:200px;width: 100%;background-color: #e7e7e2;margin-top: 50px; padding: 0px 20px;border-radius: 15px;" for="item in list">
188        <grid-col span="0" style="width: 40%;">
189          <div style="align-items: center;justify-content: center">
190            <image src="{{item.src}}" style="object-fit: contain;border-radius: 30px;"></image>
191          </div>
192        </grid-col>
193        <grid-col span="0" style="width: 60%;">
194          <div style="align-items: center;justify-content: center;width: 100%;height: 100%;text-align: center;">
195            <text>image{{item.id}}</text>
196          </div>
197        </grid-col>
198      </grid-row>
199    </grid-container>
200  </refresh>
201</div>
202```
203
204
205```css
206/* xxx.css */
207.container{
208  flex-direction: column;
209  background-color: #F1F3F5;
210  width: 100%;
211  height: 100%;
212}
213text{
214  color: #0a0aef;
215  font-size: 60px;
216}
217```
218
219
220```js
221// index.js
222import prompt from '@system.prompt';
223export default {
224  data:{
225    list:[
226      {src:'common/images/1.png',id:'1'},
227      {src:'common/images/2.png',id:'2'},
228      {src:'common/images/3.png',id:'3'}
229    ],
230    fresh:false
231  },
232  refresh(e) {
233    prompt.showToast({
234      message: 'refreshing'
235    })
236    var that = this;
237    that.fresh = e.refreshing;
238    setTimeout(function () {
239      that.fresh = false;
240      that.list.unshift({src: 'common/images/4.png',id:'4'});
241      prompt.showToast({
242        message: 'succeed'
243      })
244    }, 2000)
245  }
246}
247```
248
249
250![en-us_image_0000001276003501](figures/en-us_image_0000001276003501.gif)