Problem DescriptionIn 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:
|
<<: Various methods to restart Mysql under CentOS (recommended)
>>: How to set up remote access to a server by specifying an IP address in Windows
Table of contents 1. Easy to read code 1. Unified...
background Sometimes we need to get the creation ...
Recently, I need to implement a cascading selecti...
Common scenarios of MySQL: getting the intersecti...
Many people say that IE6 does not support PNG tra...
First, what is box collapse? Elements that should...
Application nesting of unordered lists Copy code T...
Table of contents Introduction to Samba Server Sa...
I recently upgraded MySQL to 5.7, and WordPress r...
Copy code The code is as follows: li {width:300px...
Data URI Data URI is a scheme defined by RFC 2397...
JDK Installation I won't go into too much det...
This article shares the summary of the JS mineswe...
<br />Most of the time when we talk about na...
In Linux, there are two types of file connections...