目录

collapse

Overview

The collapse element is used to collapse a long content or list and expand it by tapping. If there is too much content to be classified, you can use it to display the primary content and collapse the secondary content.

This element has the following features:

The following figure shows the effect.

The basic layout code is as follows:

<div class="container">
    <text class="title" style="margin-top: 30px;">Basic usage</text>
    <collapse accordionarray="{{basic1}}" @change="change"></collapse>
</div>

Development Procedure

[h2]Customizing the Subelements

  1. Design the layout.

    The layout of the collapse element consists of two parts: title and content.

    • The title is implemented through two image elements and one text element in between.
    • The content is implemented through text.
    <template>
      <!-- Only one root node is allowed in template. -->
      <div class="container">
        <block for="{{accordionarray}}">
          <div class="titlebar" style="background-color: {{$item.display?'#c0c0c0':'white'}}; " onclick="click($idx)">
            <image if="{{imgdisplay}}" src="{{$item.thumb}}" class="img"></image>
            <text class="title">{{ $item.title }}</text>
            <image id="img{{$idx}}" src="{{$item.display?'/Common/xialagrey.jpg':'/Common/xiala.jpg'}}" class="image {{animationimage}}"></image>
          </div>
          <div id="{{$idx}}" class="collapse {{$item.animationslot}}">
            <text class="txt">{{ $item.content }}</text>
          </div>
        </block>
      </div>
    </template>
  2. Customize the attributes and supported events.
    Supported attributes

    Attribute

    Type

    Default Value

    Description

    title

    String

    -

    Title.

    thumb

    String

    -

    Path of the icon on the left of the title.

    disabled

    Boolean

    false

    Indicates whether to disable the element.

    open

    Boolean

    false

    Indicates whether to expand the content.

    showanimation

    Boolean

    false

    Indicates whether to enable the animation effects for expanding and collapsing the content.

    content

    String

    -

    Content.

    accordion

    Boolean

    false

    Indicates whether to enable the accordion animation.

    Supported events

    Event

    Parameter

    Triggering Condition

    change

    e

    The content is collapsed or expanded.

[h2]Implementing the Expand and Collapse Animations

  1. Rotate the icon.
    • Rotate the icon from 0 degrees to 180 degrees when the content is expanded:
      // Icon rotation animation.
      imageanimation(id) {
        var options = {
          easing: 'linear',
          fill: 'forwards',
        }
        var frames = [
          {
            transform: {
              rotate: 180
            }
          },
          {
            transform: {
              rotate: 0
            }
          }];
        var animation = this.$element('img' + id).animate(frames, options);
        animation.play();
        animation.finish();
        animation.pause();
        animation.cancel();
        animation.reverse();
        animation.onfinish = function () {
        }
        animation.oncancel = function () {
        }
      },
    • Rotate the icon from 180 degrees to 0 degrees when the content is collapsed:
      // Animation for icon restoration.
      imageanimationrecovery(id) {
        var options = {
          easing: 'linear',
          fill: 'forwards',
        }
        var frames = [
          {
            transform: {
              rotate: 0
            }
          },
          {
            transform: {
              rotate: 180
            }
          }];
        var animation = this.$element('img' + id).animate(frames, options);
        animation.play();
        animation.finish();
        animation.pause();
        animation.cancel();
        animation.reverse();
        animation.onfinish = function () {
        }
        animation.oncancel = function () {
        }
      },
  2. Modify the height of the content to implement the expand and collapse functions.
    /* Set animations for the content. */
    .noanimationslotstart {
      animation-name: noanimationslotstart;
      animation-duration: 0ms;
      animation-fill-mode: forwards;
    }
    @keyframes noanimationslotstart {
      from {
        height: 0;
      }
      to {
        height: 200px;
      }
    }
    .noanimationslotend {
      animation-name: noanimationslotend;
      animation-duration: 0ms;
      animation-fill-mode: forwards;
    }
    @keyframes noanimationslotend {
      from {
        height: 200px;
      }
      to {
        height: 0;
      }
    }
  3. Use showanimation to determine whether to enable the animation effects for expanding and collapsing the content.
    • If its value is false, the animation effects are disabled.
      // The value of showanimation is false. In this case, the animation effects are disabled.
      if (this.accordionarray[index].open === false) {
        this.imageanimation(index)
        this.accordionarray[index].animationslot = "noanimationslotstart"
        this.accordionarray[index].open = true
        this.accordionarray[index].display = true
        this.changevalue = "The content is expanded."
        this.status = true
      } else {
        this.imageanimationrecovery(index)
        this.accordionarray[index].animationslot = "noanimationslotend"
        this.accordionarray[index].open = false
        this.accordionarray[index].display = false
        this.changevalue = "The content is collapsed."
        this.status = false
      }
    • If its value is true and the value of accordion is false, the common animation effects are enabled.
      if (this.accordionarray[index].showanimation === true) {
        this.accordionarray[index].open = true
        if (this.accordionarray[index].display === false) {
          this.imageanimation(index)
          this.accordionarray[index].animationslot = "animationslotstart"
          this.accordionarray[index].display = true
          this.changevalue = "The content is expanded."
          this.status = true
        } else {
          this.imageanimationrecovery(index)
          this.accordionarray[index].animationslot = "animationslotend"
          this.accordionarray[index].display = false
          this.changevalue = "The content is collapsed."
          this.status = false
        }
      }
    • If its value and the value of accordion are both true, the accordion animation is enabled, that is, expanding one content will collapse others.
      // Accordion animation.
      accordionanimation(index) {
        this.accordionarray[index].open = true
        if (this.accordionarray[index].display === false) {
          this.accordionarray[index].display = true
          this.accordionarray[index].animationslot = "animationslotstart"
          this.imageanimation(index)
          this.changevalue = "The content is expanded."
          this.status = true
          if (this.value !== index && this.value !== null) {
            this.accordionarray[this.value].display = false
            this.accordionarray[this.value].animationslot = "animationslotend"
            this.imageanimationrecovery(this.value)
            this.changevalue = "The content is collapsed."
            this.status = false
          }
        } else {
          this.imageanimationrecovery(index)
          this.accordionarray[index].animationslot = "animationslotend"
          this.accordionarray[index].display = false
          this.changevalue = "The content is collapsed."
          this.status = false
        }
        this.value = index
      },

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

  1. Define and bind the change event in the parent element.
    <collapse accordionarray="{{animation}}" @change="change"></collapse>
    change(e) {
      console.log(e.detail.changevalue + e.detail.status);
      prompt.showToast({
        message: e.detail.changevalue + e.detail.status,
        duration: 500
      })
    }
  2. Call this.$emit in the subelements to notify the parent element of the events and the parameters.
    click(num) {
      if (this.accordion === true) {
        this.accordionanimation(num)
      } else {
        this.easyanimation(num)
      }
      this.$emit('change', { changevalue: this.changevalue, status: this.status })
    },

Sample Code

collapse.ux file:

<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container">
    <block for="{{accordionarray}}">
      <div class="titlebar" style="background-color: {{$item.display?'#c0c0c0':'white'}}; " onclick="click($idx)">
        <image if="{{imgdisplay}}" src="{{$item.thumb}}" class="img"></image>
        <text class="title">{{ $item.title }}</text>
        <image id="img{{$idx}}" src="{{$item.display?'/Common/xialagrey.jpg':'/Common/xiala.jpg'}}" class="image {{animationimage}}"></image>
      </div>
      <div id="{{$idx}}" class="collapse {{$item.animationslot}}">
        <text class="txt">{{ $item.content }}</text>
      </div>
    </block>
  </div>
</template>

<style>
  .container {
    flex-direction: column;
    width: 100%;
  }
  .image {
    width: 50px;
    height: 50px;
    margin-right: 10px;
  }
  .img {
    width: 50px;
    height: 50px;
    margin-left: 10px;
  }
  .titlebar {
    width: 100%;
    height: 100px;
    justify-content: center;
    align-items: center;
    border-bottom: 1px;
  }
  .txt {
    font-size: 35px;
    margin-top: 10px;
    margin-bottom: 10px;
    color: grey;
  }
  .title {
    flex: 1;
    font-size: 40px;
    margin-left: 10px;
  }
  .collapse {
    width: 100%;
    height: 0px;
    background-color: rgb(247, 243, 243);
  }
  .animationimageup {
    animation-name: animationimageup;
    animation-duration: 0ms;
    animation-fill-mode: forwards;
  }
  @keyframes animationimageup {
    from {
      transform: rotate(0deg);
    }
    to {
      transform: rotate(180deg);
    }
  }
  .animationimagedown {
    animation-name: animationimagedown;
    animation-duration: 0ms;
    animation-fill-mode: forwards;
  }
  @keyframes animationimagedown {
    from {
      transform: rotate(180deg);
    }
    to {
      transform: rotate(0deg);
    }
  }
  /* Set the first animation for the content. */
  .animationslotstart {
    animation-name: animationslotstart;
    animation-duration: 300ms;
    animation-fill-mode: forwards;
  }
  @keyframes animationslotstart {
    from {
      height: 0;
    }
    to {
      height: 200px;
    }
  }
  .animationslotend {
    animation-name: animationslotend;
    animation-duration: 300ms;
    animation-fill-mode: forwards;
  }
  @keyframes animationslotend {
    from {
      height: 200px;
    }
    to {
      height: 0;
    }
  }
  /* Set the second animation for the content. */
  .noanimationslotstart {
    animation-name: noanimationslotstart;
    animation-duration: 0ms;
    animation-fill-mode: forwards;
  }
  @keyframes noanimationslotstart {
    from {
      height: 0;
    }
    to {
      height: 200px;
    }
  }
  .noanimationslotend {
    animation-name: noanimationslotend;
    animation-duration: 0ms;
    animation-fill-mode: forwards;
  }
  @keyframes noanimationslotend {
    from {
      height: 200px;
    }
    to {
      height: 0;
    }
  }
</style>
<script>
  module.exports = {
    props: {
      accordionarray: {
        type: Array,
        default: [
          {
            // Set the title.
            title:'Basic usage',
            // Disable the element or not.
            disabled: false,
            // Expand the content or not.
            open: false,
            // Icon path.
            thumb: '',
            // Content.
            content: 'The content and its style can be customized',
            // Enable the animation effects for expanding and collapsing the content or not.
            showanimation: false,
            // Animation-related attributes.
            animationslot: '',
            display: false,
          }
        ]
      },
      // Enable the accordion animation or not.
      accordion: {
        type: Boolean,
        default: false
      },
      status: {
        type: Boolean,
        default: false
      },
    },
    data: {
      imgdisplay: false,
      value: null,
      default: '0',
      changevalue: ''
    },
    onInit() {
      // The content is expanded by default.
      if (this.accordionarray[this.default].open === true) {
        setTimeout(() => {
          this.imageanimation(this.default)
        }, 10);
        this.accordionarray[this.default].animationslot = "noanimationslotstart"
        this.accordionarray[this.default].open = true
        this.accordionarray[this.default].display = true
      }
      // Display the icon.
      if (this.accordionarray[this.default].thumb !== '') {
        this.imgdisplay = true
      }
    },

    click(num) {
      if (this.accordion === true) {
        this.accordionanimation(num)
      } else {
        this.easyanimation(num)
      }
      this.$emit('change', { changevalue: this.changevalue, status: this.status })
    },
    easyanimation(index) {
      if (this.accordionarray[index].disabled !== true) {
        if (this.accordionarray[index].showanimation === true) {
          this.accordionarray[index].open = true
          if (this.accordionarray[index].display === false) {
            this.imageanimation(index)
            this.accordionarray[index].animationslot = "animationslotstart"
            this.accordionarray[index].display = true
            this.changevalue = "The content is expanded."
            this.status = true
          } else {
            this.imageanimationrecovery(index)
            this.accordionarray[index].animationslot = "animationslotend"
            this.accordionarray[index].display = false
            this.changevalue = "The content is collapsed."
            this.status = false
          }
        } else {
          // The value of showanimation is false. In this case, the animation effects are disabled.
          if (this.accordionarray[index].open === false) {
            this.imageanimation(index)
            this.accordionarray[index].animationslot = "noanimationslotstart"
            this.accordionarray[index].open = true
            this.accordionarray[index].display = true
            this.changevalue = "The content is expanded."
            this.status = true
          } else {
            this.imageanimationrecovery(index)
            this.accordionarray[index].animationslot = "noanimationslotend"
            this.accordionarray[index].open = false
            this.accordionarray[index].display = false
            this.changevalue = "The content is collapsed."
            this.status = false
          }
        }
      }
    },

    // Accordion animation.
    accordionanimation(index) {
      this.accordionarray[index].open = true
      if (this.accordionarray[index].display === false) {
        this.accordionarray[index].display = true
        this.accordionarray[index].animationslot = "animationslotstart"
        this.imageanimation(index)
        this.changevalue = "The content is expanded."
        this.status = true
        if (this.value !== index && this.value !== null) {
          this.accordionarray[this.value].display = false
          this.accordionarray[this.value].animationslot = "animationslotend"
          this.imageanimationrecovery(this.value)
          this.changevalue = "The content is collapsed."
          this.status = false
        }
      } else {
        this.imageanimationrecovery(index)
        this.accordionarray[index].animationslot = "animationslotend"
        this.accordionarray[index].display = false
            this.changevalue = "The content is collapsed."
        this.status = false
      }
      this.value = index
    },

    // Icon rotation animation.
    imageanimation(id) {
      var options = {
        easing: 'linear',
        fill: 'forwards',
      }
      var frames = [
        {
          transform: {
            rotate: 180
          }
        },
        {
          transform: {
            rotate: 0
          }
        }];
      var animation = this.$element('img' + id).animate(frames, options);
      animation.play();
      animation.finish();
      animation.pause();
      animation.cancel();
      animation.reverse();
      animation.onfinish = function () {
      }
      animation.oncancel = function () {
      }
    },
    // Animation for icon restoration.
    imageanimationrecovery(id) {
      var options = {
        easing: 'linear',
        fill: 'forwards',
      }
      var frames = [
        {
          transform: {
            rotate: 0
          }
        },
        {
          transform: {
            rotate: 180
          }
        }];
      var animation = this.$element('img' + id).animate(frames, options);
      animation.play();
      animation.finish();
      animation.pause();
      animation.cancel();
      animation.reverse();
      animation.onfinish = function () {
      }
      animation.oncancel = function () {
      }
    },
  }
</script>

hello.ux file:

<import name="collapse" src="../Collapse/collapse.ux"></import>
<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container">
    <!-- Basic usage -->
    <text class="title" style="margin-top: 30px;">Basic usage</text>
    <collapse ></collapse>
    <!-- The content is expanded by default.-->
    <collapse accordionarray="{{basic1}}"></collapse>
    <!-- The element is disabled. -->
    <collapse accordionarray="{{basic2}}"></collapse>
    <!-- Animation effects. -->
    <text class="title" style="margin-top: 30px;"> Animation effects</text>
    <collapse accordionarray="{{animation}}" @change="change"></collapse>
    <!-- Accordion animation (Expanding one will collapse others.).-->
    <text class="title" style="margin-top: 30px;">Accordion animation (expanding one will collapse others)</text>
    <collapse accordion="{{accordionvalue}}" accordionarray="{{accordion}}" onchange="change"></collapse>
    <!-- Icon configuration.-->
    <text class="title" style="margin-top: 30px;">Icon configuration.</text>
    <collapse accordionarray="{{icon}}"></collapse>
  </div>
</template>

<style>
  .container {
    flex-direction: column;
    background-color: #e7e5e5;
  }
  .title {
    font-size: 40px;
    color: #00fa9a;
    height: 80px;
    background-color: #faf9f9;
  }
</style>

<script>
  import prompt from '@system.prompt';
  module.exports = {
    data: {
      // The content is expanded by default.
      basic1: [
        {
          title: 'Expand by default',
          disabled: false,
          open: true,
          thumb: '',
          display: false,
          content: 'This is the content. If it is too long, the element can collapse it and only display the title. Click the title to expand or collapse the content.',
          showanimation: false,
          animationslot: '',
        }
      ],
      // The element is disabled.
      basic2: [
        {
          title: 'Element disabled',
          disabled: true,
          open: false,
          thumb: '',
          display: true,
          content: '',
          showanimation: false,
          animationslot: '',
        }
      ],
      // Common animations.
      animation: [
        {
          title:'Animation effects',
          disabled: false,
          open: false,
          thumb: '',
          display: false,
          content: 'This is the content. If it is too long, the element can collapse it and only display the title. Click the title to expand or collapse the content.',
          showanimation: true,
          animationslot: '',
        }
      ],
      // Enable the accordion animation or not.
      accordionvalue: true,
      // Accordion animation.
      accordion: [
        {
          title: 'Accordion animation',
          disabled: false,
          open: false,
          thumb: '',
          display: false,
          content: 'This is the content. If it is too long, the element can collapse it and only display the title. Click the title to expand or collapse the content.',
          showanimation: true,
          animationslot: '',
        },
        {
          title:'Accordion animation',
          display: false,
          disabled: false,
          open: false,
          thumb: '',
          content: 'This is the content. If it is too long, the element can collapse it and only display the title. Click the title to expand or collapse the content.',
          showanimation: true,
          animationslot: '',
        },
        {
          title: 'Accordion animation',
          disabled: false,
          display: false,
          open: true,
          thumb: '',
          content: 'This is the content. If it is too long, the element can collapse it and only display the title. Click the title to expand or collapse the content.',
          showanimation: true,
          animationslot: '',
        }
      ],
      // Configure the icon.
      icon: [
        {
          title: 'Icon configuration',
          disabled: false,
          open: false,
          thumb: '/Common/logo.png',
          display: false,
          content: 'This is the content. If it is too long, the element can collapse it and only display the title. Click the title to expand or collapse the content.',
          showanimation: true,
          animationslot: '',
        }
      ]
    },
    onInit() {
    },
    // Respond to the change event.
    change(e) {
      console.log(e.detail.changevalue + e.detail.status);
      prompt.showToast({
        message: e.detail.changevalue + e.detail.status,
        duration: 500
      })
    }
  }
</script>