vue2之el-table表格多选改单选

vue2之el-table表格多选改单选

模板代码

html 复制代码
<el-table ref="tableRef" :data="tableData" border class="order-plan-table" 
:show-summary="showSummary" @selection-change="handleSelectionChange" @select="handleSelect">
      <el-table-column type="selection" width="55" align="center" />
 </el-tabel>

script代码

js 复制代码
data() {
    return {
      ids: [],
      selectedRow: [],
    }
  },
//-----------------下面是methods--------------------------
// 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      if (selection.length > 0) {
        this.selectedRow = selection[selection.length - 1];
      } else {
        this.selectedRow = null;
      }
      this.$emit('selection-change', this.selectedRow);
    },
    handleSelect(selection, row) {
      // 点击单行时强制单选
      if (selection.length > 1) {
        this.$refs.tableRef.clearSelection();
        this.$refs.tableRef.toggleRowSelection(row, true);
      }
    },

样式,需要隐藏全选按钮, 这里使用了样式穿透, scss需留意

css 复制代码
<style scoped>
::v-deep .el-table__header-wrapper .el-checkbox {
  display: none;
  /* 隐藏全选复选框 */
}
::v-deep .el-table__header-wrapper .el-table-column--selection .cell {
  padding-left: 0;
  /* 调整列间距 */
}
</style>
相关推荐
A黄俊辉A20 小时前
uniapp webview中实现 app和内嵌的H5双向通信
vue.js·json
三十而立洋20 小时前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁21 小时前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
李少兄21 小时前
JavaScript 隐式全局变量解析
javascript
honkun61 天前
vue 表格组件 vxe-table 配置 ajax 请求自动加载数据与表单查询
vue.js·vxe-table
kybs19911 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
雪芽蓝域zzs1 天前
第26章:ECharts 自定义主题 + 页面一键切换主题(深色 / 浅色两套)
vue.js·echars
雪芽蓝域zzs1 天前
第22章:ECharts 图表联动(多图表交互联动)
vue.js·echars
daols881 天前
vue 表格组件 vxe-table 实现虚拟滚动与无需滚动加载
vue.js·vxe-table
雪芽蓝域zzs1 天前
第18章:ECharts 折线图 + 标记点、标记线,自定义 tooltip
vue.js·echars