An example of dynamically implementing different styles of data in a column of el-table of element ui

An example of dynamically implementing different styles of data in a column of el-table of element ui

Problem Description

In the framework of Ele.me UI, input data is el-form and output data is el-table. Sometimes products want to make boring tables dynamic, such as displaying different styles for different contents. There are actually many ways to meet this requirement. This article lists two for reference.

Implementation method 1

The effect diagram is as follows

The code is as follows

<template>
 <div id="app">
  <!-- Requirement: Three Kingdoms character table, requiring different countries to display different colors (red for Wei, black for Shu, blue for Wu) -->
  <el-table :data="tableData" style="width: 100%">
   <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
   <el-table-column prop="nation" label="国别" width="180"> 
    <!-- The idea is to get the corresponding data through the template slot, and different data will be displayed in different colors. Of course, only one can be displayed (controlled by v-if) -->
    <template scope="scope">
     <div v-if="scope.row.nation == '魏国'" style="color:red;font-weight:bold;">{{scope.row.nation}}</div>
     <div v-if="scope.row.nation == '蜀国'" style="color:black;font-weight:bold;">{{scope.row.nation}}</div>
     <div v-if="scope.row.nation == '吴国'" style="color:blue;font-weight:bold;">{{scope.row.nation}}</div>
    </template>
   </el-table-column>
   <el-table-column prop="bornPlace" label="Birthplace"> </el-table-column>
  </el-table>
 </div>
</template>

<script>
export default {
 name: "app",
 data() {
  return {
   tableData: [
    {
     name: "Liu Bei",
     nation: "Shu Kingdom",
     bornPlace: "Zhuo County, Zhuo County (Zhuozhou City, Hebei Province)",
    },
    {
     name: "Cao Cao",
     nation: "Wei State",
     bornPlace: "Qiao County, Pei State (Bozhou City, Anhui Province)",
    },
    {
     name: "Sun Quan",
     nation: "Wu State",
     bornPlace: "Wujun Fuchun County (Fuyang District, Hangzhou City, Zhejiang Province)",
    },
    {
     name: "Guan Yu",
     nation: "Shu Kingdom",
     bornPlace: "Hedong County Jie County (Jiezhou Town, Yanhu District, Yuncheng City, Shanxi Province)",
    },
   ],
  };
 },
};
</script>

Method 1 To summarize this first method, although it can achieve the effect, the code is written in el-table, which looks bloated. If only two or three styles are required to be displayed dynamically, it can be written, but if there are seven or eight or even more styles to be displayed dynamically, this writing method will be very bloated and difficult to maintain later. I personally recommend the second way, which is to use vue's own :style dynamic binding style to achieve it, which can not only simplify the code, but also achieve richer effects. as follows:

Implementation method 2

The effect diagram is as follows

The code is as follows

<template>
 <div id="app">
  <!-- Requirement: Three Kingdoms character table, requiring different countries to display different colors (red for Wei, black for Shu, blue for Wu) -->
  <el-table :data="tableData" style="width: 100%">
   <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
   <el-table-column prop="nation" label="国别" width="180"> 
    <!-- The idea is to obtain the corresponding data through the template slot, and dynamically display different colors through the vue dynamic style method. This method is more flexible-->
    <template scope="scope">
     <!-- means: bind a dynamic style to this div, the color attribute value is the return value of the getColorByNation() method, so you only need to dynamically set the return value of the method through the passed scope-->
     <div :style="{color:getColorByNation(scope),fontSize:getSizeByNation(scope),fontWeight:'bold'}">{{scope.row.nation}}</div>
    </template>
   </el-table-column>
   <el-table-column prop="bornPlace" label="Birthplace"> </el-table-column>
  </el-table>
 </div>
</template>

<script>
export default {
 name: "app",
 data() {
  return {
   tableData: [
    {
     name: "Liu Bei",
     nation: "Shu Kingdom",
     bornPlace: "Zhuo County, Zhuo County (Zhuozhou City, Hebei Province)",
    },
    {
     name: "Cao Cao",
     nation: "Wei State",
     bornPlace: "Qiao County, Pei State (Bozhou City, Anhui Province)",
    },
    {
     name: "Sun Quan",
     nation: "Wu State",
     bornPlace: "Wujun Fuchun County (Fuyang District, Hangzhou City, Zhejiang Province)",
    },
    {
     name: "Guan Yu",
     nation: "Shu Kingdom",
     bornPlace: "Hedong County Jie County (Jiezhou Town, Yanhu District, Yuncheng City, Shanxi Province)",
    },
   ],
  };
 },
 methods: {
  // Dynamically set the color getColorByNation(scope){
   console.log(scope); // Print the passed scope to correspond to the entire row of data in different rows of the table, as shown in the following picture: 
   if(scope.row.nation == "魏国"){
    return "red"
   }else if(scope.row.nation == "Shu"){
    return "black"
   }else if(scope.row.nation == "Wu"){
    return "blue"
   }
  },
  // Dynamically set the font size getSizeByNation(scope){
   if(scope.row.nation == "魏国"){
    return "14px"
   }else if(scope.row.nation == "Shu"){
    return "18px"
   }else if(scope.row.nation == "Wu"){
    return "24px"
   }
  }
 },
};
</script> 

Print the passed scope

Method 2 summarizes that this method of controlling by binding styles through vue is much more flexible than the first method. After all, the first way is written in el-table, and the second way is written in methods. The specific one to use depends on the scene requirements.

This is the end of this article about examples of dynamically implementing different styles of data in a column of element ui's el-table. For more examples of dynamically implementing different styles of data in a column of element ui's el-table, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The VUE table dynamically adds a column of data, and the newly added data cannot be edited (the data bound to v-model cannot be updated in real time)
  • Vue implements dynamic control of el-table table column display and hiding
  • Detailed explanation of the writing method of VUE2.0+ElementUI2.0 table el-table loop dynamic column rendering
  • How does Vue dynamically modify a column of data in el-table

<<:  Various methods to restart Mysql under CentOS (recommended)

>>:  How to set up remote access to a server by specifying an IP address in Windows

Recommend

How to write high-quality JavaScript code

Table of contents 1. Easy to read code 1. Unified...

Getting the creation time of a file under Linux and a practical tutorial

background Sometimes we need to get the creation ...

Vue realizes cascading selection of provinces, cities and districts

Recently, I need to implement a cascading selecti...

How to get the intersection/difference/union of two sets in mysql

Common scenarios of MySQL: getting the intersecti...

PNG Alpha Transparency in IE6 (Complete Collection)

Many people say that IE6 does not support PNG tra...

5 solutions to CSS box collapse

First, what is box collapse? Elements that should...

Application nesting of HTML ul unordered tables

Application nesting of unordered lists Copy code T...

When MySQL is upgraded to 5.7, WordPress reports error 1067 when importing data

I recently upgraded MySQL to 5.7, and WordPress r...

Solution to large line spacing (5 pixels more in IE)

Copy code The code is as follows: li {width:300px...

Data URI and MHTML complete solution for all browsers

Data URI Data URI is a scheme defined by RFC 2397...

Summary of the minesweeping project implemented in JS

This article shares the summary of the JS mineswe...

Navigation Design and Information Architecture

<br />Most of the time when we talk about na...

Distinguishing between Linux hard links and soft links

In Linux, there are two types of file connections...