当前位置:   article > 正文

Element UI & Element Plus之改变表格单元格颜色_elementplus el-table如何交替变化颜色

elementplus el-table如何交替变化颜色

前言

首先官网文档的 Table 表格 组件有相关说明,只需要在el-table标签中加上:cell-style="xxx",以及实现该方法即可。Element UI框架和Element Plus框架在使用上有一点点区别,因此记录一下下。

Element UIElement - The world's most popular Vue UI frameworkElement,一套为开发者、设计师和产品经理准备的基于 Vue 2.0 的桌面端组件库https://element.eleme.cn/#/zh-CN

Element Plus一个 Vue 3 UI 框架 | Element Plusa Vue 3 based component library for designers and developershttps://element-plus.gitee.io/zh-CN/

一、示例代码

1.Element UI 示例

(1)/src/views/Example/CellStyle/index_1.vue

  1. <el-table
  2. border
  3. size="mini"
  4. row-key="id"
  5. highlight-current-row
  6. :data="feats.list"
  7. :cell-style="handleChangeCellStyle"
  8. >
  9. <el-table-column label="评分" width="100" align="center">
  10. <template slot-scope="scope">
  11. <el-link type="danger">{{ scope.row.score }}</el-link>
  12. </template>
  13. </el-table-column>
  14. </el-table>
  15. methods: {
  16. /**
  17. * 改变表格单元格颜色
  18. */
  19. handleChangeCellStyle({row, column, rowIndex, columnIndex}) {
  20. let cellStyle
  21. if (row.score > 9) {
  22. cellStyle = 'background-color: #ffdcdc'
  23. }
  24. else if (row.score > 7) {
  25. cellStyle = 'background-color: #fde2c2'
  26. }
  27. else {
  28. cellStyle = 'background-color: #fff'
  29. }
  30. if (column.label === '评分') {
  31. return cellStyle
  32. }
  33. },
  34. }

2.Element Plus 示例

(1)/src/views/Example/CellStyle/index_2.vue

  1. <el-table
  2. border
  3. size="small"
  4. row-key="id"
  5. highlight-current-row
  6. :data="feats.list"
  7. :cell-style="handleChangeCellStyle"
  8. >
  9. <el-table-column label="评分" width="100" align="center">
  10. <template slot-scope="scope">
  11. <el-link type="danger">{{ scope.row.score }}</el-link>
  12. </template>
  13. </el-table-column>
  14. </el-table>
  15. methods: {
  16. /**
  17. * 改变表格单元格颜色
  18. */
  19. handleChangeCellStyle({row, column, rowIndex, columnIndex}) {
  20. let cellStyle = {}
  21. if (row.score > 9) {
  22. cellStyle.backgroundColor = '#ffdcdc'
  23. }
  24. else if (row.score > 7) {
  25. cellStyle.backgroundColor = '#fde2c2'
  26. }
  27. else {
  28. cellStyle.backgroundColor = '#fff'
  29. }
  30. if (column.label === '评分') {
  31. return cellStyle
  32. }
  33. },
  34. }

三、运行效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/283682?site
推荐阅读
相关标签
  

闽ICP备14008679号