当前位置: 首页 > news >正文

element ui el-table嵌套el-table,实现checkbox联动效果

HTML代码:

        <el-table  header-row-class-name="my-el-table-header" row-class-name="my-el-table-body"  ref="multipleGroupTable" :data="vehicleGroupTableData" tooltip-effect="dark" style="width: 100%" v-loading="loading" @select="handleSelectionGroupChange" @select-all="handleSelectionGroupChange"  ><el-table-column type="selection" width="55"  ></el-table-column> <el-table-column label="AA" width="200"><template slot-scope="scope">{{ scope.row.groupName }}</template></el-table-column>    <el-table-column  label="BB" ><template slot-scope="scope">{{ scope.row.vehicles.length }}Vehicles</template></el-table-column><el-table-column label="" width="200" show-overflow-tooltip> <template slot-scope="scope"><el-link type="primary" style="color:#2764B0" :underline="false">View </el-link></template></el-table-column><el-table-column type="expand"> <template slot-scope="scope"> <div class="el-table-expand-table"><el-table :key="scope.$index" header-row-class-name="my-el-table-header" row-class-name="my-el-table-body" key="0" ref="multipleTable" :data="scope.row.vehicles" tooltip-effect="dark" style="width: 100%"><el-table-column label="" width="70"><template slot-scope="scope"><el-checkbox  :key="'checkbox-' +scope.$index + scope.row.vin"  @change="(val) => handleNestedCheckboxChange(val, scope.row,scope.$index)" :value="scope.row.isChecked" ></el-checkbox></template></el-table-column><el-table-column label="Nickname" min-width="16%"><template slot-scope="scope"> {{ scope.row.nickName }}</template></el-table-column><el-table-column  label="aa" min-width="16%"><template slot-scope="scope">{{ scope.row.vin }}</template></el-table-column> <el-table-column  label="bb" min-width="8%" show-overflow-tooltip><template slot-scope="scope">{{ scope.row.year }}</template></el-table-column><el-table-column  label="cc" min-width="16%" ><template slot-scope="scope"><div class="fleet-group-div" ><el-dropdown trigger="click" placement="bottom" :teleported="false"><div class="el-dropdown-link"><div class="el-dropdown-link-label" :title="123123">下拉菜单下拉菜单下拉菜单下拉菜单下拉菜单下拉菜单下拉菜单下拉菜单下拉菜单下拉菜单下拉菜单下拉菜单</div><i class="el-icon-arrow-down el-icon--right"></i></div><el-dropdown-menu slot="dropdown" class="fleet-group-div-dropdown "><el-checkbox-group v-model="scope.row.fleetGroup" class="fleet-group-div-dropdown-item" size="medium"><el-checkbox v-for="(i,j) in groupOptions" :key="j" :label="i.name" :title="i.name">{{i.name}}</el-checkbox></el-checkbox-group></el-dropdown-menu></el-dropdown></div></template></el-table-column><el-table-column label="" min-width="16%" show-overflow-tooltip><template slot-scope="scope"><el-link type="primary"  style="color:#2764B0" :underline="false">View</el-link></template></el-table-column></el-table></div></template></el-table-column></el-table>

 数据结构:

  vehicleGroupTableData:[ // Vehicle Groups 表格数据{groupName: 'group A',  // Group Nameid:1,vehicles:[{nickName:"昵称",vin:'123123123123',year:'2025',fleetGroup:['GroupA','GroupB','GroupC'],isChecked:false},{nickName:"昵称2",vin:'12312312312312',year:'20251',fleetGroup:['GroupA2','GroupB2','GroupC2'],isChecked:false}]},{groupName: 'group B',  // Group Nameid:2,vehicles:[{nickName:"昵称1",vin:'123123123123222',year:'20252',fleetGroup:['GroupAC','GroupB','GroupC'],isChecked:false}]},],

js代码:

  // 嵌套的内部的checkbox的点击handleNestedCheckboxChange(checked, row, index) {// 确保响应式更新this.$set(row, 'isChecked', checked);// 更新父表格的选中状态this.$nextTick(() => {const parentRow = this.vehicleGroupTableData.find(group =>group.vehicles.some(v => v.vin === row.vin));if (parentRow) {// 3. 计算父行的选中状态const checkedCount = parentRow.vehicles.filter(v => v.isChecked).length;const totalCount = parentRow.vehicles.length;// 4. 更新父行的选中状态const isAllChecked = checkedCount === totalCount;const isIndeterminate = checkedCount > 0 && checkedCount < totalCount; //是否是未全选// 5. 使用Element UI的API更新状态this.$refs.multipleGroupTable.toggleRowSelection(parentRow,isAllChecked);// 7. 添加row-class-name来反映状态this.$refs.multipleGroupTable.$el.querySelectorAll('.el-table__row').forEach(el => {const rowData = el.__vue__?.row;if (rowData === parentRow) {// 获取dom节点let dom = el.querySelector('.el-table-column--selection').querySelector('.el-checkbox__input')this.$nextTick(()=>{dom.classList.toggle('is-indeterminate',isIndeterminate);})}});}});setTimeout(()=>{this.$forceUpdate()})},handleSelectionGroupChange(val){this.multipleGroupSelection = val;//  // 如果外层行被选中/取消选中,同步到所有子行this.vehicleGroupTableData.forEach(row => {if(this.multipleGroupSelection.includes(row)){ // 如果当前行被选中// 则需要遍历当前行下面的vehicles下的每一项,给每一项的isChecked设置为truerow.vehicles.forEach(vehicle => {vehicle.isChecked =  this.multipleGroupSelection.includes(row)})}else{row.vehicles.forEach(vehicle => {vehicle.isChecked = false})}})},

相关文章:

  • 如何在 MX Linux 上安装 AnyDesk
  • 数据库新选择?KingbaseES在线体验详解
  • 【产线烧录太慢】爱普特APT WDO2烧录器!高速MCU+Flash全协议 离线烧录
  • AI 双轮驱动:工具革新与编程进化如何重塑技术生态
  • 【开源工具】Windows屏幕控制大师:息屏+亮度调节+快捷键一体化解决方案
  • 金仓数据库在线体验平台:开启国产数据库云端探索之旅
  • Excel数据导出小记
  • Uniapp跨端兼容性全方位解决方案
  • Eureka、Nacos、Zookeeper 优雅上下线机制
  • 大模型安全关键技术研究
  • SpringBoot 通过集成 Flink CDC 来实时追踪 MySql 数据变动
  • Kafka分区机制深度解析:架构原理、负载均衡与性能优化
  • 开源三代示波器720p虚拟界面设计,手机,电脑和Pad均可访问,专用于8通道同步数据采集处理,可玩性高,基于STM32H7(2025-06-17)
  • 02 ( chrome 浏览器插件, 立马翻译), 搭建本地 api
  • 6-16阿里前端面试记录
  • Uniapp设备API全面指南:从位置获取到扫码功能的实现
  • 【Linux】UDP与TCP协议
  • pyqt QMenuBar
  • 【前端基础】摩天之建的艺术:html(上)
  • 【Pandas】pandas DataFrame swaplevel
  • 青海企业网站开发定制/夸克搜索引擎入口
  • 网站没完成可以备案么/哈尔滨网络公司
  • 政府制作网站收费/怎么做线上销售
  • 个人网站建设论文中期报告/人工智能培训
  • 亚马逊网站入口/大连中小企业网络营销
  • 独立网站建设费用列表/互联网广告推广是什么