vue table 点击单元格修改单元格背景色 使用 customCell 改变单元格背景色(样式)

解决思路是定义一个可选颜色数组,一个当前颜色字段,一个已设置颜色数组(对象属性包含name: 属性名,value:属性值,index:下标行,color:显示颜色,实际有属性名和下标就可以定义到哪行哪列的单元格了),点击事件(自己定,我用的双击)。

1.data内定义以下属性

        colorData: ['red', 'orange', 'yellow', 'green','#7777ff','#a028f8','violet', '#ff8b91', '#ffd47d', '#6bff6b', '#ffff9b', '#80fffd', '#b5b5b5', '#fafafa'],
        nowColor: 'red',
        // name: 属性名,value:属性值,index:下标行,color:显示颜色
        setColorData: [],

2.在columns里定义customCell(这里是一列)。

{
            title: '物料供应商',
            dataIndex: 'customerName',
            key: 'customerName',
            customCell: (record, index) => {
              return {
                style: {
                  'background-color': this.getColor(record, index, 'customerName')
                },
                on: {
                  dblclick: () => {
                    this.customCell(record, index, 'customerName')
                  }
                }
              }
            },
            scopedSlots: {
              filterDropdown: 'string',
              filterIcon: 'filterIcon'
            }
         },

3.methods内定义方法

      customCell(record, index, colName) {
        const find = this.setColorData.find(ele => {
          return ele.name === colName && ele.value === record[colName] && ele.index === index
        })
        if (find) {
          find.color = this.nowColor
        } else {
          const line = {
            name: colName,
            value: record[colName],
            index: index,
            color: this.nowColor
          }
          this.setColorData.push(line)
        }
      },
      getColor (record, index, colName) {
        const find = this.setColorData.find(ele => {
          return ele.name === colName && ele.value === record[colName] && ele.index === index
        })
        if (find) {
          return find.color
        }
        return ''
      },

4.选择颜色组件

    <a-row>
      <a-col :span="24">
        <a-radio-group v-model="nowColor">
          <a-radio-button
            v-for="item in colorData"
            :style="{'background': item, 'color': item === nowColor ? item : 'black'}"
            :key="item"
            :value="item">{{ item }}</a-radio-button>
        </a-radio-group>
      </a-col>
    </a-row>

4.效果图,可以点击颜色按钮切换颜色,双击单元格改变成对应的背景色。

Vue3中,如果你想要给`el-table`组件的单元格添加背景颜色,你可以通过`cell-class-name`属性或者直接在模板中使用`v-bind:class`指令来动态设置。以下是两种常见的方式: **1. 使用`cell-class-name`属性** 在`el-table-column`标签中,你可以设置`cell-class-name`属性,然后在对应的值里返回一个计算函数,这个函数会接收当前行的数据作为参数,并根据数据的某个字段的值来决定背景色。 ```html <template> <el-table-column prop="yourDataField" label="列名" cell-class-name="getClassByData" /> </template> <script> export default { methods: { getClassByData(row) { // 根据row.yourDataField的值来决定背景色 if (row.yourDataField === '条件1') { return 'bg-color-1'; } else if (row.yourDataField === '条件2') { return 'bg-color-2'; } // 如果没有特定匹配,可以返回默认颜色或者其他默认处理 return 'default-bg-color'; }, }, }; </script> ``` **2. 使用`v-bind:class`指令** 在每个单元格的模板上,你可以直接使用`v-bind:class`绑定一个对象,对象的键对应预设的颜色类名,值是一个计算属性,根据实际数据决定应该显示哪种颜色。 ```html <template> <el-table-cell v-for="(item, index) in yourData" :key="index"> <span :class="{ backgroundColor: determineColor(item) }">{{ item.label }}</span> </el-table-cell> </template> <script> export default { methods: { determineColor(item) { // 根据item的值判断并返回颜色类名 if (item.field === '值1') { return 'color-1'; } else if (item.field === '值2') { return 'color-2'; } return ''; }, }, }; </script> ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值