el-table 设置只能单选效果(2)

el-table中添加:header-cell-class-name="cellClass"表头增加class函数

<el-table
  :data="tableData"
  ref="table"
  border
  :header-cell-class-name="cellClass"
>
   <el-table-column
    type="selection"
    align="center"
   />
</el-table>

methods中增加对应函数,可根据是否单选(this.radio)来控制全选框显示与否

this.radio 根据不同的业务场景可以动态控制

methods: {
   cellClass(row){
     if (row.columnIndex === 0 && this.radio) {
       return 'disableheadselection'
     }
   }
}

添加css样式隐藏选框
因为添加的class属于在el-table的表头中,/deep/需要写在刚增加的class名前才会生效.(若需要在scoped中修改elementUI中的样式,则需要在选择器前面加上/deep/才会生效)

<style lang="scss" scoped>
    /deep/ .disableheadselection > .cell .el-checkbox__inner {
      display: none;
    }
<style>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容