Vue folding display multi-line text component implementation code

Vue folding display multi-line text component implementation code

Folding display multi-line text component

Fold and display multi-line text components, automatically determine whether to fold according to the incoming expand. Two modes: expand/collapse to display the full text (default), popover to display the full text

First, the code

<template>
  <div class="text-expand" ref="textExpand">
    <div v-if="!(showPopover && showPopoverJudge)">
      <span class="text-expand-content" :style="expandStyle">
        {{ (text === null || text === undefined || text === '') ? '--' : text }}
      </span>
      <div class="expander">
        <span
          v-if="showBtn && showBtnJudge"
        >
          <span
            v-if="!showFull"
            class="action action-expand"
            @click.stop="showFullFn(true)"
          >
            Expand<i v-if="showBtnIcon" class="iconfont iconxiajiantou" />
          </span>
          <span
            v-else
            class="action action-pack"
            @click.stop="showFullFn(false)"
          >
            Hide <i v-if="showBtnIcon" class="iconfont iconshangjiantou" />
          </span>
        </span>
      </div>
    </div>
    <el-popover
      v-else
      :placement="popoverPlace"
      trigger="hover">
      <div class="popover-content">
        {{ text }}
      </div>
      <span class="text-expand-content" :style="expandStyle" slot="reference">{{ text }}</span>
    </el-popover>
  </div>
</template>
<script>
export default {
  name: "TextExpand",
  props: {
    text: { // text content type: String,
      default: () => ''
    },
    expand: { // Fold and display the number of lines type: Number,
      default: () => 3
    },
    showBtn: { // Expand, collapse button type: Boolean,
      default: true
    },
    showBtnIcon: { // expand, collapse icon
      type: Boolean,
      default: true
    },
    showPopover: { // popover displays full text type: Boolean,
      default: false
    },
    popoverPlace: { // popover position type: String,
      default: 'bottom'
    }
  },
  data () {
    return {
      showFull: false, // Whether to display the full text expandStyle: '',
      showBtnJudge: false, //Judge whether to fold and display the button showPopoverJudge: false //Judge whether to fold and display the popover
    }
  },
  watch:
    text: function (val) {
      this.judgeExpand()
    }
  },
  mounted () { 
    this.judgeExpand()
  },
  methods: {
    showFullFn (value) {
      this.expandStyle = value ? '' : `display: -webkit-box;word-break: break-all;-webkit-line-clamp: ${this.expand};-webkit-box-orient: vertical;text-overflow: ellipsis;overflow: hidden;`
      this.showFull = value
    },
    judgeExpand () { //Judge whether to collapse this.$nextTick(() => {
        const { expand } = this;
        const textExpandStyle = window.getComputedStyle(this.$refs.textExpand)
        const textExpandHeight = parseFloat(textExpandStyle.height) //Get the total height const textExpandLineHeight = parseFloat(textExpandStyle.lineHeight) //Get the line height // Calculate the line height const rects = Math.ceil(textExpandHeight / textExpandLineHeight)
        if (rects <= expand) { // No need to fold and display this.showBtnJudge = false
          this.showPopoverJudge = false
        } else {
          this.showBtnJudge = true
          this.showPopoverJudge = true
          this.expandStyle = `display: -webkit-box;word-break: break-all;-webkit-line-clamp: ${this.expand};-webkit-box-orient: vertical;text-overflow: ellipsis;overflow: hidden;`
        }
      })
    }

  }
}
</script>
<style lang="less" scoped>
.text-expand{
  &-content{
    word-break: break-all;
    white-space: pre-wrap;
  }
  .expander {
    text-align: left;
    margin-top: 6px;
    .action {
      display: inline-block;
      font-size: 14px;
      color: #0281F0;
      cursor: pointer;
      i {
        display: inline;
        font-size: 12px;
      }
    }
    .action.action-pack {
      margin-left: 0;
    }
  }
}
.popover-content{
  max-width: 40vw;
  max-height: 30vh;
  overflow: hidden;
  word-break: break-all;
  overflow-y: auto;
}
</style>

usage

<text-expand :text="text" :expand="2" />

insert image description here

insert image description here

<text-expand :text="text" :expand="2" :showBtnIcon="false">

insert image description here
insert image description here

<text-expand :text="text" :expand="2" :showPopover="true">

insert image description here
insert image description here

This is the end of this article about the vue folding display multi-line text component. For more related vue folding display multi-line text component content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue2.0 collapsible list v-for loop display example
  • Vue implements an Input component that gets the key display shortcut key effect
  • Vue+elementUI component recursively implements foldable dynamic rendering multi-level sidebar navigation
  • Using Vue.js recursive components to implement a collapsible tree menu (demo)

<<:  Common structural tags in XHTML

>>:  Analysis of the implementation process of Docker intranet penetration frp deployment

Recommend

Summary of pitfalls encountered in installing mysql and mysqlclient on centos7

1. Add MySQL Yum repository MySQL official websit...

Don't forget to close the HTML tag

Building web pages that comply with Web standards ...

CSS Transition expands and collapses elements by changing the Height

A common development need is that we want to coll...

XHTML Getting Started Tutorial: Using the Frame Tag

<br />The frame structure allows several web...

Detailed tutorial on installing and using Kong API Gateway with Docker

1 Introduction Kong is not a simple product. The ...

Example code for implementing div concave corner style with css

In normal development, we usually use convex roun...

Summary of CSS3 practical methods (recommended)

1. Rounded border: CSS CodeCopy content to clipbo...

React Hook usage examples (6 common hooks)

1. useState: Let functional components have state...

JavaScript operation elements teach you how to change the page content style

Table of contents 1. Operation elements 1.1. Chan...

Data Structure - Tree (III): Multi-way Search Tree B-tree, B+ tree

Multi-way search tree Height of a complete binary...

Getting Started with MySQL - Concepts

1. What is it? MySQL is the most popular relation...

js implements a simple English-Chinese dictionary

This article shares the specific code of js to im...

Tomcat components illustrate the architectural evolution of a web server

1. Who is tomcat? 2. What can tomcat do? Tomcat i...

CSS3 achieves cool 3D rotation perspective effect

CSS3 achieves cool 3D rotation perspective 3D ani...

A brief analysis of how to use border and display attributes in CSS

Introduction to border properties border property...