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

element-plus 按钮 展开/隐藏

文章目录

  • 1、小记
  • 2、页面
  • 3、typescript事件
  • 4、测试数据
  • 5、样式

1、小记

element-plus中el-table 的 expand,箭头控制子项显示,有点丑。
想实现类似bootstrap ,用按钮 展开/隐藏子项的功能
在这里插入图片描述在这里插入图片描述

2、页面

<!-- 表内容 --><el-table:data="tableData":border="true":preserve-expanded-content="true"style="width: 100%":expand-row-keys="expands":row-key="getRowKeys"><!-- 子项 --><el-table-column type="expand" width="0"><!-- width隐藏列 --><template #default="props"><div m="4" class="expandcontent"><el-table :data="props.row.family" :border="true" :color="'red'"><el-table-column label="Name" prop="name" /><el-table-column label="State" prop="state" /><el-table-column label="City" prop="city" /><el-table-column label="Address" prop="address" /><el-table-column label="Zip" prop="zip" /></el-table></div></template></el-table-column><!-- 其他数据列 --><el-table-column label="Date" prop="date" /><el-table-column label="Name" prop="name" /><!-- 操作 --><el-table-column label="Operations"><template #default="props"><el-button type="primary" @click="toggleExpand(props.row)">{{ isExpanded(props.row) ? '收起' : '展开' }}</el-button></template></el-table-column></el-table>

3、typescript事件

<script setup lang="tsx">
const getRowKeys = (row) => {return row.id
}
//展开自定义
const expands = ref<string[]>([])
const toggleExpand = (row) => {const key = getRowKeys(row)const index = expands.value.indexOf(key)if (index > -1) {expands.value.splice(index, 1) // 收起} else {expands.value.push(key) // 展开}
}
const isExpanded = (row) => {return expands.value.includes(getRowKeys(row))
}
</script>

4、测试数据

const tableData = [{id: 1,date: '2016-05-03',name: 'Tom',state: 'California',city: 'San Francisco',address: '3650 21st St, San Francisco',zip: 'CA 94114',expand: false,family: [{name: 'Jerry',state: 'California',city: 'San Francisco',address: '3650 21st',zip: 'CA 94114'},{name: 'Spike',state: 'California',city: 'San Francisco',address: '3650 21st ',zip: 'CA 94114'},{name: 'Tyke',state: 'California',city: 'San Francisco',address: '3650 21st ',zip: 'CA 94114'}]},{id: 2,date: '2016-05-02',name: 'Tom',state: 'California',city: 'San Francisco',address: '3650 21st St, San Francisco',zip: 'CA 94114',expand: false,family: [{name: 'Jerry',state: 'California',city: 'San Francisco',address: '3650 21st St, San Francisco',zip: 'CA 94114'}]}
]

5、样式

<!-- 样式 -->
<style scoped lang="scss">
// 子项背景色
:deep(.el-table__expanded-cell) {background-color: #cbdde2 !important;
}
// 子项居中
.expandcontent {width: 95%;margin: 0 auto;
}
</style>

相关文章:

  • css 颜色与字体
  • Flux.create
  • Mac电脑如何搭建基于java后端的开发的各种工具服务
  • WebSocket 与 HTTP 的区别及 Spring Boot 实战应用
  • FANUC机器人教程:用户坐标系标定及其使用方法
  • Java——注解开发模式下的 Spring IoC/DI 与 Bean 管理实战
  • 机器学习15-规则学习-知识加强
  • 【NLP】自然语言项目设计
  • vllm加载多个Lora部署
  • 数据分享:教育数据集-预测学生辍学风险和学术成功数据集
  • 01【C++ 入门基础】命名空间/域
  • 8、做中学 | 四年级下期 Golang运算符
  • [论文阅读] 人工智能 + 软件工程 | AI 与敏捷开发的破局之路:从挫败到成功的工作坊纪实
  • Git 使用规范与命令使用场景详解
  • 【嵌入式ARM汇编基础】-ELF文件格式内部结构详解(二)
  • C语言再出发:2025年AI时代的关键语言
  • JavaWeb学习——day9(图书管理系统初级)
  • Day 2 学习主题「面向对象 + Pythonic 风格」
  • Linux服务器部署Leantime与cpolar构建低成本团队协作环境
  • 数据分享:汽车行业-汽车属性数据集