Detailed explanation of three ways to wrap text in el-table header

Detailed explanation of three ways to wrap text in el-table header

Problem Description

The headers in a table usually do not wrap, but sometimes in some business scenarios, you need to wrap the text in the header. Let's take a look at the effect diagram first.

Rendering

Three types of code

Just read the comments.
For demonstration, just copy and paste and run it.

<template>
  <div class="vueWrap">
    <el-table
      style="width: 900px"
      :data="tableBody"
      border
      :header-cell-style="{
        background: '#FAFAFA',
        color: '#333333',
        fontWeight: 'bold',
        fontSize: '14px',
      }"
    >
      <el-table-column
        type="index"
        label="Serial number"
        width="58"
        align="center"
      ></el-table-column>

      <!-- Header wrapping method 1: Use the header slot method to split the header text into two divs. Because the div box is a block element, the two divs will wrap, so the header will wrap. This method is suitable for header wrapping of fixed data-->
      <el-table-column prop="toolName" width="180" align="center">
        <template slot="header">
          <div>Toolbox</div>
          <div>Part Name</div>
        </template>
        <template slot-scope="scope">
          <span>{{ scope.row.toolName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="Supplier" prop="supplier" width="120" align="center">
      </el-table-column>

      <!-- Table header line break method 2, compared with method 1, this method uses /n line break character, plus CSS white-space blank style control -->
      <el-table-column
        :label="labelFn()"
        prop="supplierCountry"
        width="180"
        align="center"
      >
      </el-table-column>

      <!-- Table header line break method three, dynamic method-->
      <el-table-column
        v-for="(item, index) in tableHeader"
        :key="index"
        :label="item.labelName"
        :prop="item.propName"
        width="180"
        align="center"
        :render-header="renderheader"
      ></el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // The dynamic data table header needs to be returned by the backend, and separated by commas where line breaks are needed tableHeader: [
        {
          labelName: "Model 001, Price (yuan)",
          propName: "typeOne",
        },
        {
          labelName: "Model 002, Price (Yuan)",
          propName: "typeTwo",
        },
      ],
      // Table body data tableBody: [
        {
          id: "2021111101",
          toolName: "5G Services",
          supplier: "Huawei",
          supplierCountry: "China",
          typeOne: "8888888",
          typeTwo: "9999999",
        },
        {
          id: "2021111101",
          toolName: "6G-SERVER",
          supplier: "China has a bright future",
          supplierCountry: "CHINA",
          typeOne: "678678678",
          typeTwo: "789789789",
        },
      ],
    };
  },
  methods: {
    labelFn() {
      // Add line breaks where needed, and set return `supplier_ncountry` in the white-space style at the bottom;
    },

    // The header function rendering method of the Ele.me UI is somewhat similar to the header slot method // It also divides the header data text into two pieces, and then renders the content into two divs (div automatically wraps)
    renderheader(h, { column, $index }) {
      return h("div", {}, [
        h("div", {}, column.label.split(",")[0]),
        h("div", {}, column.label.split(",")[1]),
      ]);
    },
    
  },
};
</script>
<style lang="less" scoped>
/deep/ .el-table th.el-table__cell > .cell {
  white-space: pre;
  // white-space: pre-wrap; // also works.
  
}
</style>

I won’t go into details about white-space. For more information, please refer to the official document developer.mozilla.org/zh-CN/docs/Web/CSS/white-space

Summarize

The three methods have their own characteristics, but render-header will consume a little bit of performance.
If the header data is fixed, it is recommended to use the header slot method first, and the line break plus CSS method is secondly recommended.
If it is dynamic data, you can only use the header renderheader function

This is the end of this article about the three ways to wrap the header text of el-table. For more information about the three ways to wrap the header text of el-table, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The el-table header adapts to the content to perfectly solve the header misalignment and fixed column misalignment
  • vue el-table implements custom table header
  • VUE2.0+ElementUI2.0 table el-table implements header extension el-tooltip

<<:  Simple comparison of meta tags in html

>>:  CSS to achieve particle dynamic button effect

Recommend

Superficial Web Design

<br />I have always believed that Yahoo'...

Sample code for making desktop applications with vue + Electron

1.vue packaging Here we use the vue native packag...

Detailed explanation of the use of MySQL select cache mechanism

MySQL Query Cache is on by default. To some exten...

Let you understand how HTML and resources are loaded

All content in this blog is licensed under Creati...

Tutorial on downloading, installing, configuring and using MySQL under Windows

Overview of MySQL MySQL is a relational database ...

What should I do if I can't view the source file of a web page?

Q: Whether using Outlook or IE, when you right-cl...

Web Design Experience: 5 Excellent Web Design Concepts Full Analysis (Pictures)

Unlike other types of design, web design has been ...

CSS to achieve Tik Tok subscription button animation effect

I was watching Tik Tok some time ago and thought ...

MySQL 5.7 installation and configuration tutorial under CentOS7 (YUM)

Installation environment: CentOS7 64-bit, MySQL5....

How to modify the mysql table partitioning program

How to modify the mysql table partitioning progra...

How to invert the implementation of a Bezier curve in CSS

First, let’s take a look at a CSS carousel animat...