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

The difference and usage of Vue2 and Vue3 brother component communication bus

Table of contents vue2.x vue3.x tiny-emitter plug...

Comparison of the efficiency of different methods of deleting files in Linux

Test the efficiency of deleting a large number of...

Preventing SQL injection in web projects

Table of contents 1. Introduction to SQL Injectio...

Nginx configuration SSL and WSS steps introduction

Table of contents Preface 1. Nginx installation 1...

Bundling non-JavaScript static resources details

Table of contents 1. Custom import in packaging t...

Basic commands for MySQL database operations

1. Create a database: create data data _name; Two...

Apache ab concurrent load stress test implementation method

ab command principle Apache's ab command simu...

Detailed tutorial on compiling and installing python3.6 on linux

1. First go to the official website https://www.p...

Detailed explanation of the use of nohup /dev/null 2>&1

nohup command: If you are running a process and y...

Basic installation process of mysql5.7.19 under winx64 (details)

1. Download https://dev.mysql.com/downloads/mysql...

How to use Javascript to generate smooth curves

Table of contents Preface Introduction to Bezier ...

MySQL data archiving tool mysql_archiver detailed explanation

Table of contents I. Overview 2. pt-archiver main...

How to clean up Alibaba Cloud MySQL space

Today I received a disk warning notification from...