多选

type设置selectable,也就是我们表格的选择框 绑定了一个selectable

 <el-table
    :data="tableData"
    ref="table"
    @row-click='rowClick'
    :row-class-name="tableRowClassName"
  >
    <el-table-column
        type="selection"
        :selectable="selected"
        width="55">
    </el-table-column>
    <el-table-column
        prop="name"
        label="名称"
        width="180">
    </el-table-column>
</el-table>
selected(row, index) {
  if (row.status == 1) { // 根据你的条件来设置
    return true //可选择
  } else {
    return false; //不可选择
  }
}

第一个参数是当前这一条数据的对象,第二个是下标
然后方法返回true 则代表这条数据可选,为false则不可选

还有一种方法:

<el-table
  ref="multipleTable"
  :data="dataList"
  tooltip-effect="dark"
  style="width: 100%"
  @selection-change="handleSelectionChange">
  <el-table-column
    type="selection"
    :selectable="checkSelectable"
    width="55">
  </el-table-column>
  <el-table-column
    prop="name"
    label="名称">
  </el-table-column>
</el-table>

实现多选:
@selection-change="handleSelectionChange"

<el-table-column
   type="selection"
   width="55">
</el-table-column>

实现置灰:

<el-table-column
	type="selection"
	:selectable="checkSelectable"
	width="55"
>

:selectable="checkSelectable"
method中方法

// this.checkedInitQuestions 为需要置灰的数据列表
checkSelectable (row) {
  let mark = 0
  this.checkedInitQuestions.forEach((item) => {
    if (item.id === row.id) {
      mark = mark + 1
      return false
    }
  })
  return mark <= 0
}

单选

在这里插入图片描述
el-table:row-class-name="tableRowClassName",用来设置当前行的样式
el-radio:disabled="!checkSelectable(scope.row)",用来置灰单选框
methods方法:tableRowClassName定义class类名,row是当前行的数据,设置判断条件

<el-table border :data="tableData" row-key="id" :row-class-name="tableRowClassName">
	<el-table-column label="请选择" width="80">
		<template slot-scope="scope">
			<el-radio
				v-mode1="form.selectRadio"
				:disabled="!checkSelectable(scope.row)"
				:label="scope.row.id"
				@click.native.stop.prevent="getcurrentRow(scope.row)"
				>&nbsp;</el-radio>
		</template>
	</el-table-column>
</el-table>

这里写 &nbsp 的目的是为了页面不显示内容,只显示单选操作

tableRowClassName ({row, rowIndex}) {
	if (!row.status) {
		return 'disabledRow'
	} else {
		return ''
	}
},
checkSelectable (row) {
	return row.status
}

css设置

/deep/ .disabledRow {
	cursor: not-allowed;
	pointer-events: none;
	color: #ccc; // 这个会更改当前行的字体颜色,如果不需要可以删掉
}

参考:
element-ui 表格单选及表格多选,设置某些选项置灰禁止选择
element-ui 实现el-table表格多选、置灰表格中多选框的某个选项

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐