目录

分组组件

简介

分组(group)组件是将组件分组,添加间隔,以产生明显的区块,主要用于负一屏分类、通讯录分组等场景。

分组一般具有如下特点:

效果图如下:

基本布局代码如下:

<template>
  <div class="container">
    <text class="title">基础分组</text>
    <group>
      <text class="txt" for="{{listdata}}">{{ $item }}</text>
    </group>
  </div>
</template>

开发指引

[h2]自定义子组件

  1. 定义布局样式。

    分组组件布局包括两个部分:标题部分、分组内容部分。

    • 标题部分由text组件实现。
    • 分组内容部分由slot组件实现。
    <template>
      <div class="{{mode?'card':'container'}}" style="margin-top: {{top}}px;">
        <div style="flex-direction: column;">
          <text class="title">{{ title }}</text>
          <div style="margin-top: {{top}}px;flex-direction: column;">
            <slot></slot>
          </div>
        </div>
      </div>
    </template>
  2. 规划属性和支持的事件。
    支持的属性:

    属性

    类型

    默认值

    描述

    title

    String

    -

    标题内容。

    top

    Number

    0

    标题与分组内容的间隔。

    mode

    Boolean

    false

    分组组件的模式。

    • false:基础模式
    • true:卡片模式

    支持的事件:

    无

[h2]父组件引用子组件

  1. 子组件中定义title、top、mode属性。
    data: {
      title: "",
      top: 0,
      mode: false
    },
  2. 父组件引用子组件的属性。
    <group top="20" mode="{{mode}}" title="卡片模式">
      <text class="txt" for="{{listdata}}">{{ $item }}</text>
    </group>

示例代码

分组group.ux代码:

<template>
  <!-- Only one root node is allowed in template. -->
  <div class="{{mode?'card':'container'}}" style="margin-top: {{top}}px;">
    <div style="flex-direction: column;">
      <text class="title">{{ title }}</text>
      <div style="margin-top: {{top}}px;flex-direction: column;">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<style>
  .container {
    flex-direction: column;
    background-color: white;
  }

  .title {
    font-size: 50px;
    background-color: #afd44a;
  }
  .card {
    border-radius: 15px;
    margin-left: 20px;
    margin-right: 20px;
    flex-direction: column;
    background-color: rgb(230, 172, 119);
  }
</style>

<script>
  module.exports = {
    data: {
      title: "",
      top: 0,
      mode: false
    },
    onInit() {
      this.$page.setTitleBar({
        text: 'menu',
        textColor: '#ffffff',
        backgroundColor: '#007DFF',
        backgroundOpacity: 0.5,
        menu: true
      });
    }
  }
</script>

页面hello.ux代码:

<import name="group" src="../Group/group.ux"></import>
<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container">
    <text class="title">基础分组</text>
    <group>
      <text class="txt" for="{{listdata}}">{{ $item }}</text>
    </group>
    <group top="20" title="基础模式">
      <text class="txt" for="{{listdata}}">{{ $item }}</text>
    </group>
    <text class="title">卡片分组</text>
    <group mode="{{mode}}">
      <text class="txt" for="{{listdata}}">{{ $item }}</text>
    </group>
<group top="20" mode="{{mode}}" title="卡片模式">
  <text class="txt" for="{{listdata}}">{{ $item }}</text>
</group>
  </div>
</template>

<style>
  .container {
    flex-direction: column;
    background-color: #cececc;
  }

  .title {
    font-size: 50px;
    width: 100%;
    margin-bottom: 20px;
    margin-top: 30px;
    background-color:  #4ab9d4;
  }
  .txt {
    font-size: 50px;
  }
</style>

<script>
  module.exports = {
    data: {
      mode: true,
      listdata: ["分组内容", "分组内容", "分组内容"]
    },
    onInit() {
      this.$page.setTitleBar({
        text: 'menu',
        textColor: '#ffffff',
        backgroundColor: '#007DFF',
        backgroundOpacity: 0.5,
        menu: true
      });
    }
  }
</script>