目录

grid

Overview

The grid element is used to display the recommended offering list, trending content and so on. This element has the following features:

The grid is formed by multiple small grids, which consists of three parts: image, text and badge. You can use the data parameter to design the grid element. Parameters of data are as follows:

{
    text:'Grid 1', // Displayed text
    uri: 'https://www.huawei.com', // URI. Users are redirected to it by tapping the grid.
    image: '/Common/c1.png',        // Displayed image.
    badge: '5',                     // Displayed badge.
    badgeColor: '#007AFF'           // Background color of the badge.
},

The following figure shows the layout.

The sample code for the layout is as follows:

<text>Square mode</text>
<div class="example-body">
      <grid data="{{data}}" mode="square" column="{{3}}" show-border="{{true}}"  border-color="#FF0000" @change="change"></grid>
</div>

Development Procedure

[h2]Customizing the Subelements

  1. Design the layout.

    The grid UI is universal, but the content layout is not. The content layout cannot be fixed. Otherwise, once the UI of the content part changes, the subelements have to be modified, which violates the open-closed principle in programming.

    Therefore, multiple style attributes are supported in grid.ux for developers to customize the style.

    <import name="flex-row" src="./flex_row"></import>
    <template>
      <flex-row wrap="wrap" h-style="{{hStyle}}">
        <block for="{{item in compData}}">
          <div style="{{compStyle}}" class="border" onclick="{{handleClick(item)}}">
            <!--Render the image, text and badge in the grid by using the image and text elements.-->
            <image if="{{item.image !== ''}}" src="{{item.image}}" style="{{compImageStyle}}"></image>
            <text if="{{item.text !== ''}}" style="{{textStyle}}">{{ item.text }}</text>
            <text if="{{item.badge !== ''}}" class="badge" style="background-color:{{item.badgeColor?item.badgeColor:'#dd524d'}}">{{ item.badge }}</text>
          </div>
        </block>
      </flex-row>
    </template>
  2. Customize the attributes and supported events.

    Supported attributes

    Attribute

    Type

    Default Value

    Description

    data

    Array

    []

    Grid data.

    mode

    String

    square

    Layout mode.

    column

    Number

    3

    Number of columns.

    show-border

    Boolean

    true

    Indicates whether to display the border.

    border-color

    String

    #d8e4ee

    Border color.

    Parameters of data

    Attribute

    Type

    Example

    Description

    text

    String

    Grid 1

    Text displayed in the small grid.

    uri

    String

    https://www.huawei.com

    URL.

    image

    String

    /Common/c1.png

    Image URI.

    badge

    String

    5

    Badge.

    badgeColor

    String

    #007AFF

    Background color of the badge.

    Supported events

    Event

    Parameter

    Description

    change

    evt

    Tap event in a grid.

[h2]Implementing Data Communication Between the Parent Element and Subelements

  1. Define the parameters in props and pass the values when the parent element references the subelements. Call this.xxx in the subelements to obtain the values for further use.
    props: {
      data: {},
      mode: {
        default: 'square'
      },
      column: {
        default: 3
      },
      showBorder: {
        default: true
      },
      borderColor: {
        default: "#d8e4ee"
      }
    },
  2. Define and bind the tap event in the parent element.
    <text style="padding-top: 20px;padding-bottom: 20px;font-size: 35px">Tap the grid to trigger the event:</text>
    <grid data="{{data}}" mode="square" column="{{3}}" @change="change"></grid>
    change: function (evt) {
        console.log("evt.detail : " + JSON.stringify(evt.detail));
        if (evt.detail.params.uri) {
            router.push({
                uri: evt.detail.params.uri
            });
        }
        else {
            prompt.showToast({
                message: `${evt.detail.params.text} tapped.`,
                duration: 2000,
                gravity: 'center'
            })
        }
    },
  3. In the subelements, trigger the change event of the parent element by tapping the grid.
    handleClick(item) {
      console.log("handleClick : " + JSON.stringify(item));
      this.$emit('change', { params: item })
    }

[h2]Computing Data and Style

Use the computed attribute to dynamically compute the parameters of props and return the data and style to the element.

  1. Generate the grid data.
    compData() {
      var arr = []
      for (var i = 0, len = this.data.length; i < len; i++) {
        var text = !this.data[i]['text'] ? '' : this.data[i]['text']
        var image = !this.data[i]['image'] ? '' : this.data[i]['image']
        var uri = !this.data[i]['uri'] ? '' : this.data[i]['uri']
        var badge = !this.data[i]['badge'] ? '' : this.data[i]['badge']
        var badgeColor = !this.data[i]['badgeColor'] ? '' : this.data[i]['badgeColor']
        var item = {
          text: text,
          image: image,
          uri: uri,
          badge: badge,
          badgeColor: badgeColor
        }
        arr.push(item)
      }
      return arr
    },
  2. Generate the style of the grid.
    colWidth() {
      var that = this
      device.getInfo({
        success: function (ret) {
          that.width = ret.windowLogicWidth - 66
          console.log("width:" + that.width);
        },
        fail: function (erromsg, errocode) {
          console.log("device.getInfo fail:", erromsg, errocode);
        }
      })
      return that.width / this.column
    },
    colHeight() {
      return this.mode === 'square' ? this.width / this.column : 120
    },
    compStyle() {
      var style = 'align-items:center;align-content:center;justify-content:center;'
      if (this.showBorder === true) {
        style += 'border-width:1px;border-style:solid;'
        style += 'border-color:' + this.borderColor + ';'
      }
      if (this.column > 3) {
        style += 'flex-direction: column;'
      } else {
        if (this.mode === 'square') {
          style += 'flex-direction: column;'
        }
      }
      style += 'width:' + this.colWidth + 'px;'
      style += 'height:' + this.colHeight + 'px;'
      console.log("compStyle : " + style);
      return style
    },
  3. Generate the style of the text and image in the small grid.
    avatarSize() {
      if (this.column > 3) {
        return 46
      } else {
        if (this.mode === 'square') {
          return 86
        } else {
          return 46
        }
      }
    },
    textStyle() {
      return (this.column <= 3 && this.mode === 'rect') ? 'margin-left:10px;' : ''
    },
    compImageStyle() {
      var style = 'width:' + this.avatarSize + 'px;'
      style += 'height:' + this.avatarSize + 'px;'
      style += 'align-items: center;justify-content:center;'
      return style
    },

Sample Code

hello.ux file:

<import name="grid" src="../Grid/grid"></import>
<template>
  <div class="container">
    <text style="padding-top: 20px;padding-bottom: 20px;font-size: 35px">Square layout:</text>
    <grid data="{{data}}" mode="square" column="{{3}}"></grid>

    <text style="padding-top: 20px;padding-bottom: 20px;font-size: 35px">Rectangle layout:</text>
    <grid data="{{data}}" mode="rect" column="{{3}}"></grid>

    <text style="padding-top: 20px;padding-bottom: 20px;font-size: 35px">Tap the grid to trigger the event:</text>
    <grid data="{{data}}" mode="square" column="{{3}}" @change="change"></grid>

    <text style="padding-top: 20px;padding-bottom: 20px;font-size: 35px">Red border:</text>
    <grid data="{{data}}" mode="square" column="{{3}}" border-color="#FF0000"></grid>

    <text style="padding-top: 20px;padding-bottom: 20px;font-size: 35px">No border:</text>
    <grid data="{{data}}" mode="rect" column="{{4}}" show-border="{{false}}"></grid>

    <text style="padding-top: 20px;padding-bottom: 20px;font-size: 35px">Swipe the grid:</text>
    <swiper style="height: 750px">
      <grid data="{{data}}" mode="square" column="{{3}}"></grid>
      <grid data="{{data}}" mode="square" column="{{3}}"></grid>
      <grid data="{{data}}" mode="square" column="{{3}}"></grid>
    </swiper>

    <div class="grid" style="height: 1000px">
      <text style="padding-top: 20px;padding-bottom: 20px;font-size: 35px">Dynamic loading:</text>
      <grid data="{{dynamicList}}" mode="square" column="{{3}}"></grid>
      <input type="button" @click="add" value="Tap to add a grid."></input>
      <input type="button" if="dynamicList.length !== 0" style="margin-top: 15px;" @click="del" value="Tap to delete a grid."></input>
    </div>
  </div>
</template>

<style lang="less">
    .container {
      margin-left: 33px;
      margin-right: 33px;
      flex-direction: column;
    }
    .grid {
      flex-direction: column;
    }
</style>

<script>
    import router from '@system.router';
    import prompt from '@system.prompt';
    export default {
        private: {
            data: [
                {
                    text: 'Grid 1',
                    uri: 'https://developer.huawei.com/consumer/en/doc/development/quickApp-Guides/quickapp-introduction-0000001126786237',
                    image: '/Common/c1.png',
                    badge: '5',
                    badgeColor: '#007AFF'
                },
                {
                    text: 'Grid 2',
                    uri: 'www.huawei.com',
                    image: '/Common/c2.png',
                    badge: '10',
                    badgeColor: '#FFCC00'
                },
                {
                    text: 'Grid 3',
                    image: '/Common/c3.png',
                    badge: '99'
                },
                {
                    text: 'Grid 4',
                    image: '/Common/c4.png'
                },
                {
                    text: 'Grid 5',
                    image: '/Common/c5.png'
                },
                {
                    text: 'Grid 6',
                    image: '/Common/c6.png'
                },
                {
                    text: 'Grid 7',
                    image: '/Common/c7.png'
                },
                {
                    text: 'Grid 8',
                    image: '/Common/c8.png'
                },
                {
                    text: 'Grid 9',
                    image: '/Common/c9.png'
                }
            ],
            dynamicList: [],
        },
        change: function (evt) {
            console.log("evt.detail : " + JSON.stringify(evt.detail));
            if (evt.detail.params.uri) {
                router.push({
                    uri: evt.detail.params.uri
                });
            }
            else {
                prompt.showToast({
                    message: `${evt.detail.params.text} tapped.`,
                    duration: 2000,
                    gravity: 'center'
                })
            }
        },
        add() {
            if (this.dynamicList.length < 9) {
                this.dynamicList.push({
                    image: `/Common/c${this.dynamicList.length + 1}.png`,
                    text: `Grid ${this.dynamicList.length + 1}`,
                    badge: `${this.dynamicList.length + 1}`
                })
            } else {
                prompt.showToast({
                    message:'You can add no more than 9 grids.'
                });
            }
        },
        del() {
            this.dynamicList.splice(this.dynamicList.length - 1, 1)
        }
    }
</script>

grid.ux file:

<import name="flex-row" src="./flex_row"></import>
<template>
  <flex-row wrap="wrap" h-style="{{hStyle}}">
    <block for="{{item in compData}}">
      <div style="{{compStyle}}" class="border" onclick="{{handleClick(item)}}">
        <image if="{{item.image !== ''}}" src="{{item.image}}" style="{{compImageStyle}}"></image>
        <text if="{{item.text !== ''}}" style="{{textStyle}}">{{ item.text }}</text>
        <text if="{{item.badge !== ''}}" class="badge" style="background-color:{{item.badgeColor?item.badgeColor:'#dd524d'}}">{{ item.badge }}</text>
      </div>
    </block>
  </flex-row>
</template>

<style>
  .border:active {
    background-color: #f1f1f1;
  }
  .badge {
    position: absolute;
    top: 5px;
    right: 15px;
    color: #fff;
    background-color: #dd524d;
    border-radius: 20px;
    font-size: 30px;
    width: 40px;
    text-align: center;
  }
</style>

<script>
  import device from '@system.device'
  export default {
    data: {
      width: ''
    },
    props: {
      // Grid data.
      data: {},
      mode: {
        // Layout. The value can be square and rect.
        default: 'square'
      },
      column: {
        // Number of the columns.
        default: 3
      },
      showBorder: {
        // Whether to display the border.
        default: true
      },
      borderColor: {
        // Customize the border.
        default: "#d8e4ee"
      },
      hStyle: {
        default: ''
      }
    },
    computed: {
      compData() {
        var arr = []
        for (var i = 0, len = this.data.length; i < len; i++) {
          var text = !this.data[i]['text'] ? '' : this.data[i]['text']
          var image = !this.data[i]['image'] ? '' : this.data[i]['image']
          var uri = !this.data[i]['uri'] ? '' : this.data[i]['uri']
          var badge = !this.data[i]['badge'] ? '' : this.data[i]['badge']
          var badgeColor = !this.data[i]['badgeColor'] ? '' : this.data[i]['badgeColor']

          var item = {
            text: text,
            image: image,
            uri: uri,
            badge: badge,
            badgeColor: badgeColor
          }
          arr.push(item)
        }
        return arr
      },
      colWidth() {
        var that = this
        device.getInfo({
          success: function (ret) {
            that.width = ret.windowLogicWidth - 66 // 66 is the sum of margin-left and margin-right in container set in the hello.ux file.
            console.log("width:" + that.width);
          },
          fail: function (erromsg, errocode) {
            console.log("device.getInfo fail:", erromsg, errocode);
          }
        })
        return that.width / this.column
      },
      colHeight() {
        return this.mode === 'square' ? this.width / this.column : 120
      },
      compStyle() {
        var style = 'align-items:center;align-content:center;justify-content:center;'

        if (this.showBorder === true) {
          style += 'border-width:1px;border-style:solid;'
          style += 'border-color:' + this.borderColor + ';'
        }

        if (this.column > 3) {
          style += 'flex-direction: column;'
        } else {
          if (this.mode === 'square') {
            style += 'flex-direction: column;'
          }
        }

        style += 'width:' + this.colWidth + 'px;'
        style += 'height:' + this.colHeight + 'px;'
        console.log("compStyle : " + style);
        return style
      },
      avatarSize() {
        if (this.column > 3) {
          return 46
        } else {
          if (this.mode === 'square') {
            return 86
          } else {
            return 46
          }
        }
      },
      textStyle() {
        return (this.column <= 3 && this.mode === 'rect') ? 'margin-left:10px;' : ''
      },
      compImageStyle() {
        var style = 'width:' + this.avatarSize + 'px;'
        style += 'height:' + this.avatarSize + 'px;'
        style += 'align-items: center;justify-content:center;'
        return style
      },
    },
    handleClick(item) {
      console.log("handleClick : " + JSON.stringify(item));
      this.$emit('change', { params: item })
    }
  }
</script>

flex-row.ux file:

<template>
  <div class="{{bgColor}}" style="{{compStyle}}">
    <slot></slot>
  </div>
</template>

<style lang="less">
    @import "../Common/bg-color.less";
</style>

<script>
    import device from '@system.device'
    export default {
        data: {
            width: ''
        },
        props: {
            reverse: {
                default: false        // Project orientation.
            },
            wrap: {
                default: 'nowrap' // Line wrap. The value can be nowrap (default), wrap, or wrap-reverse.
            },
            justify: {
                default: 'left'        // Specify the main axis alignment mode of the project. The value can be left (default), center, right, between, or around.
            },
            align: {
                default: 'stretch'     // Specify the cross axis alignment mode of the project. The value can be stretch (default), top, middle, bottom, or baseline.
            },
            bgColor: {
                default: ''
            },
            hStyle: {
                default: ''
            }
        },
        computed: {
            compStyle() {
                var style = 'flex-direction: ' + (this.reverse ? 'row-reverse' : 'row') + ';'

                style += 'flex-wrap: ' + this.wrap + ';'

                switch (this.justify) {
                    case 'right':
                        style += 'justify-content: flex-end;'
                        break
                    case 'center':
                        style += 'justify-content: center;'
                        break
                    case 'between':
                        style += 'justify-content: space-between;'
                        break
                    case 'around':
                        style += 'justify-content: space-around;'
                        break
                    default:
                        style += 'justify-content: flex-start;'
                        break
                }

                switch (this.align) {
                    case 'top':
                        style += 'align-items: flex-start;'
                        break
                    case 'middle':
                        style += 'align-items: center;'
                        break
                    case 'bottom':
                        style += 'align-items: flex-end;'
                        break
                    case 'baseline':
                        style += 'align-items: baseline;'
                        break
                    default:
                        style += 'align-items: stretch;'
                        break
                }
                return style
            },
        }
    }
</script>