element-ui el-table 实现全选且父级子级联动

   首先在实现这个的时候,我在网上查看了很多别人的代码,有的是按照他上面的方法是没有办法实现,有的没写清楚,有的代码看上去很不简单易懂,在此我就写了这篇博客,记录了实现的具体方法!我觉得是全网最能简单易懂并且百分之百可实现的代码了!!!

首先展示效果图:

                          

 如上图所示,我们主要实现三个功能:

     1.点击参数名称时,实现全选,再次点击,取消全选

     2.点击父的时候,所有的子和参数名称都选中,再次点击,都取消选中

     3.点击任意一个子的时候,父和参数名称都选中,再次点击,取消选中

    我们分别用三个方法实现:

     @select=”select”       //当用户手动勾选数据行的 Checkbox 时触发的事件

     @select-all=”selectAll”    //当用户手动勾选全选 Checkbox 时触发的事件

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
可以通过以下步骤在 el-table 表头的全选按钮后加上“全选”: 1. 在 el-table 组件中添加一个 slot,用于自定义表头内容: ```html <el-table :data="tableData" :row-key="row => row.id"> <el-table-column type="selection" width="55"> <template slot="header"> <el-checkbox v-model="isAllSelected" @change="selectAll">{{selectAllText}}</el-checkbox> </template> </el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> </el-table> ``` 2. 在 Vue 实例中添加以下代码: ```javascript data() { return { isAllSelected: false, selectAllText: '全选', tableData: [ { id: 1, name: 'John', age: 20 }, { id: 2, name: 'Jane', age: 25 }, { id: 3, name: 'Bob', age: 30 } ] } }, methods: { selectAll() { const isSelected = this.isAllSelected this.tableData.forEach(row => { row.$isSelected = isSelected }) } }, watch: { tableData: { handler() { const isAllSelected = this.tableData.every(row => row.$isSelected) this.isAllSelected = isAllSelected }, deep: true } } ``` 3. 在 Vue 实例的 template 中修改 selectAllText 的值: ```html <template> <div> <el-table :data="tableData" :row-key="row => row.id"> <el-table-column type="selection" width="55"> <template slot="header"> <el-checkbox v-model="isAllSelected" @change="selectAll">{{selectAllText}}</el-checkbox> </template> </el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> </el-table> </div> </template> <script> export default { data() { return { isAllSelected: false, selectAllText: '全选', tableData: [ { id: 1, name: 'John', age: 20 }, { id: 2, name: 'Jane', age: 25 }, { id: 3, name: 'Bob', age: 30 } ] } }, methods: { selectAll() { const isSelected = this.isAllSelected this.tableData.forEach(row => { row.$isSelected = isSelected }) } }, watch: { tableData: { handler() { const isAllSelected = this.tableData.every(row => row.$isSelected) this.isAllSelected = isAllSelected }, deep: true } } } </script> ``` 这样就可以在 el-table 表头的全选按钮后添加“全选”了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值