el-table多选表格type=“selection“获取当前行数据给多选框增加插槽逻辑

本文介绍了如何在el-table的多选模式下,通过row-class-name属性和CSS伪元素实现根据某一字段为新标识的动态样式。作者分享了遇到的问题、解决方案和具体的代码实现,适用于需要在表格中添加行级标识的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.需求 效果图

在这里插入图片描述
如图 需要做成这个样子
对于el-table需要增加new的标识 根据当前行的某个字段判断。如果是普通的el-table很好做直接在每行第一个td的插槽里写逻辑就行了,但是因为这个是selection多选的表格,用插槽在type="selection"里写插槽后就导致多选框没了,单独插槽里写多选框就要重写写多选的逻辑。
后来想到用::before给每一行第一个td增加样式,直接在el-table-column增加样式,像这样:
在这里插入图片描述
但是发现el-table-column上获取不到当前行 获取不到row.isRed

3.解决方案:

给el-table上增加:row-class-name=“rowClassName” 动态判断 这里可以获取当前行

4.代码:

<template>
  <el-table
    ref="multipleTable"
    :data="tableData"
    :row-class-name="rowClassName"
    :header-cell-style="{'text-align':'center'}"
    :cell-style="{'text-align':'center'}"
    tooltip-effect="dark"
    @selection-change="handleSelectionChange">
    <el-table-column
      type="selection"
      width="60">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名"
      width="200">
    </el-table-column>
    <el-table-column
      label="是否是新消息"
      width="200">
      <template slot-scope="scope">
        {{ scope.row.isRed?'新':'-' }}
      </template>
    </el-table-column>
    <el-table-column
      label="日期"
      width="200">
      <template slot-scope="scope">
        {{ scope.row.date }}
      </template>
    </el-table-column>
    <el-table-column
      prop="address"
      label="地址"
      width="200"
      show-overflow-tooltip>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [{
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄',
        isRed: true
      }, {
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄',
        isRed: false
      }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄',
        isRed: true
      }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄',
        isRed: false
      }, {
        date: '2016-05-08',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄',
        isRed: false
      }, {
        date: '2016-05-06',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄',
        isRed: true
      }, {
        date: '2016-05-07',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄',
        isRed: false
      }],
      multipleSelection: []
    }
  },

  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    rowClassName({ row }) {
      if (row.isRed) {
        return 'red-style'
      } else {
        return ''
      }
    }
  }
}
</script>

<style lang="scss">
  .red-style {
    border: 1px solid red;
    background-color: blue;
    td:first-child {
      position: relative;
      &::before {
        content: url("./new.png");
        position: absolute;
        top: 0;
        left: 0;
        z-index: 99;
      }
    }
  }
</style>
<template> <div> <div> <!--搜索栏--> <el-input v-model="name" placeholder="请输入菜单名称" style="width: 200px" prefix-icon="el-icon-user"></el-input> <el-button class="el-icon-search" style="margin-left: 10px" type="primary" @click="load">查询</el-button> <el-button class="el-icon-refresh" style="margin-left: 10px" type="warning" @click="reset">重置</el-button> </div> <div style="margin-top: 20px;margin-bottom:35px;"> <!-- 新增、批量删除 --> <el-button class="el-icon-plus" style="margin-right: 10px" type="success" @click="save(null)">新增</el-button> <el-button class="el-icon-close" style="margin-left: 10px" type="danger" @click="deleteByids" >批量删除</el-button> </div> <el-table :data="tableData" border stripe :header-cell-style="getRowClass" @selection-change="handleSelectionChange" row-key="id" border default-expand-all> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="id" label="id"></el-table-column> <el-table-column prop="name" label="菜单名称"></el-table-column> <el-table-column prop="path" label="菜单路径"></el-table-column> <el-table-column prop="icon" label="菜单图标"> <template v-slot:default="scope"> <i :class="scope.row.icon"></i> </template> </el-table-column> <el-table-column prop="pagePath" label="页面路径"></el-table-column> <el-table-column prop="description" label="菜单描述"></el-table-column> <el-table-column prop="sortNum" label="排序"></el-table-column> <el-table-column label="操作" width="300"> <template v-slot="scope"> <div class="action-buttons"> <!-- 添加容器方便布局 --> <el-button type="success" size="mini" class="compact-btn" @click="save(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单</el-button> <el-button type="prima
03-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值