element-ui table中使用type=‘selection‘ 实现禁用,勾选,默认选中不可修改 三种状态显示问题

element-ui table中使用type='selection' 实现禁用,勾选,默认选中不可修改 三种状态显示问题

实现效果



需求

1.status='CheckOk' 时 勾选框默认选中但不可修改勾选状态

2.status='CheckFail' 时 勾选框禁用

3.status='' 时 勾选框可以勾选

实现思路

采用el-table表格自带的勾选框type='selection' 实现

实现 1.status='CheckOk' 时 勾选框默认选中但不可修改勾选状态

  1. 首先要实现默认选中

toggleRowSelection : 有两个参数(row, selected)

用于多选表格,切换某一行的选中状态,如果使用了第二个参数,则是设置这一行选中与否(selected 为 true 则选中)

javascript 复制代码
methods:{
	    // status='CheckOk' 默认选中
     defaultChecked() {
            var that = this;
            that.$nextTick(() => {
                this.tableData.forEach((v, i) => {
                    if (v.status == 'CheckOk') {
                        this.$refs.table.toggleRowSelection(v, true);
                    }
                });

            });
           } 
   },

将表格列的type类型设置为selection,添加:selectable='方法名'

html 复制代码
  <el-table-column type="selection" width="50" :selectable="selectable" />
javascript 复制代码
methods: {
	 //判断勾选
        selectable(row, index) {
            if (row.status == 'CheckFail' || row.status == 'CheckOk') {
                return false;
            } else {
                return true;
            }
        },
}
        

完整代码

html 复制代码
     <el-table ref="table" 
               :header-cell-style="{ color: '#FFF', background: '#333' }" 
               :data="tableData"
               style="width: 100%" :row-key="getRowKey" 
               :row-style="tableRowStyle"
               @selection-change="handleSelectionChange">
        <template slot="empty">
            <span style="color: #969799">{{ $t("NeoLight.empty") }}</span>
        </template>
        <el-table-column type="selection" width="50" :selectable="selectable" />
        <el-table-column prop="ri" :label="$t('outSideShelf.ri')" />
        <el-table-column prop="needNum" :label="$t('outSideShelf.needNum')" />
        <el-table-column prop="status" :label="$t('outSideShelf.status')" />
</el-table>
javascript 复制代码
mounted(){
this.defaultChecked() // 该方法可以在拿到表格数据后进行调用
}
methods: {
    // status='CheckOk' 默认选中
     defaultChecked() {
            var that = this;
            that.$nextTick(() => {
                this.tableData.forEach((v, i) => {
                    if (v.status == 'CheckOk') {
                        this.$refs.table.toggleRowSelection(v, true);
                    }
                });
            });
          } 
	 //判断勾选
        selectable(row, index) {
            if (row.status == 'CheckFail' || row.status == 'CheckOk') {
                return false; // 禁用
            } else {
                return true; //不禁用
            }
        },
        // status='CheckFail' 该行背景为灰色
         tableRowStyle({ row, rowIndex }) {
            let rowBackground = {};
            if (row.status == 'CheckFail') {
                rowBackground.background = '#6d6d6d';
                rowBackground.color = "#fff";
            } else {
                rowBackground.background = "#333";
                rowBackground.color = "#fff";
            }
            return rowBackground;
        },
}
相关推荐
大团米线4 分钟前
vite多环境合并打包config
javascript·vue.js·ecmascript
喜欢吃豆10 分钟前
我开源了 Agent UI Lab 用交互实验理解 AG-UI A2UI 与 MCP Apps
ui·开源·大模型·交互
定栓1 小时前
Vite-legacy产物console删除机制
前端·javascript·vue.js
康小汪9 小时前
AI 写 Vue 总写成同一个味道:给它装 Skills,装的不是知识是边界
前端·vue.js·人工智能·ai编程·最佳实践
染指111013 小时前
158.Agent-LangGraph-子图(共享状态和不共享状态模式)
开发语言·人工智能·python·ui·agent·agents
北冥有鱼13 小时前
发版后动态路由点击报 `Failed to fetch dynamically imported module`」问题上的完整排查与修复过程
前端·vue.js
apollo_qwe16 小时前
大模型 API 接通了,AI 聊天页面为什么还是不好用?
前端·vue.js·人工智能
小郎君65821 小时前
keep-alive 页面怎么“真刷新”?别改外层 key——一个 refreshVersion 的完整实现
vue.js·算法
徐小夕1 天前
JitWord 4.3 发布!Word 内嵌 Visio 图表、原生流程图一键无损解析
前端·vue.js·github