vue+element中table框在最前面加复选框变单选的问题

1.先看一下效果:在这里插入图片描述

就是这样的一种效果,下面就是关键代码:

ref="multipleTable" @select="handleSelect" @selection-change="handleSelectionChange"

关键代码就是这个,在table里写上就可以了

.tablessp thead tr .el-checkbox__input{
     display: none;
}

这个是控制是否显示最上面的全选框

<template>
  <div id="app">
    <el-table class="tablessp" ref="multipleTable"
     @select="handleSelect" @selection-change="handleSelectionChange" 
     :key="tableKey" :data="tableData" row-key="id" height='200' 
     border fit highlight-current-row 
     style="width: 100%;margin-top:8px;min-height:500px"
      :header-cell-style="{background:'#199ED8'}">
      <el-table-column type="selection" width="60" align="center"></el-table-column>
      <el-table-column align="center" prop="date" label="日期" min-width="80"></el-table-column>
      <el-table-column align="center" prop="name" label="名字" min-width="80"></el-table-column>
      <el-table-column align="center" prop="address" label="地址" min-width="80"></el-table-column>
     
   </el-table>
  </div>
</template>

<script>
  export default {
      data() {
        return {
          tableData: [{
            id:1,
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          }, {
            id:3,
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1517 弄'
          }, {
            id:4,
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1519 弄'
          }, {
            id:8,
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1516 弄'
          }],
          tableKey: 0,
          cutId:'',
        }
        
      },
      methods: {
        // 勾选的时候来判断 让其变成单选
          handleSelect(val,row){
             if (val.length > 1) {
                this.$refs.multipleTable.clearSelection() // 清空所有选择
                val.shift()
                this.$refs.multipleTable.toggleRowSelection(row) //  选中当前选择
           }

           let selected = val.length && val.indexOf(row) !== -1
              console.log(selected)  // true就是选中,0或者false是取消选中
          },
          handleSelectionChange(val){
           let posy = val
            let pos = []
            posy.forEach(item=>{
               pos.push(item.id)
               this.cutId = pos.join(',')//把选中的id已字符串传递出去  例如:'1,3,4,5'
            })
            console.log(this.cutId)
          }
      },
  }
</script>

<style>
.tablessp thead tr .el-checkbox__input{
     display: none;
}
.tablessp.el-table thead{
  color:#fff;
}
</style>
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue Element-UI的树形Table组件可以通过设置show-checkbox属性来显示复选框,但是默认情况下只会在非叶子节点上显示复选框,如果需要在最后一层也显示复选框,可以通过以下两种方式实现: 1. 使用slot-scope自定义单元格内容 在template使用slot-scope自定义单元格内容,通过判断当前行是否为最后一层来显示复选框。 ```html <el-table :data="data" style="width: 100%"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column label="操作"> <template slot-scope="{ row }"> <el-checkbox v-if="isLastLevel(row)" v-model="checkedNodes" :label="row.id"></el-checkbox> </template> </el-table-column> </el-table> ``` 在methods定义isLastLevel方法来判断当前行是否为最后一层: ```javascript methods: { isLastLevel(row) { return !row.children || row.children.length === 0; } } ``` 2. 使用tree-node-key属性指定叶子节点的key值 在使用树形数据时,可以通过tree-node-key属性指定叶子节点的key值,然后在show-checkbox属性使用leaf-only来显示所有叶子节点的复选框。 ```html <el-table :data="data" :tree-props="{children: 'children', hasChildren: 'hasChildren', id: 'id', label: 'name', key: 'id'}" :tree-node-key="'id'" :show-checkbox="true" :leaf-only="true" style="width: 100%"> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column label="操作"> <template slot-scope="{ row }"> <el-checkbox v-model="checkedNodes" :label="row.id"></el-checkbox> </template> </el-table-column> </el-table> ``` 在这种方式下,只需要在template直接显示复选框即可,不需要判断当前行是否为最后一层。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗的小鸟鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值