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.效果图,可以点击颜色按钮切换颜色,双击单元格改变成对应的背景色。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值