Vue3(elementPlus) el-table替换/隐藏行箭头,点击整行展开

element文档链接:

https://element-plus.org/zh-CN/component/form.html

一、el-table表格行展开关闭箭头替换成加减号

注:Vue3在样式中修改箭头图标无效,可能我设置不对,欢迎各位来交流指导

转变思路:隐藏箭头,添加一行显示展开关闭所需图标

1、隐藏箭头

复制代码
.el-table__expand-icon .el-icon svg {
  display: none;
}

此时只是箭头不可见,但是箭头的占位还在,显得很空

2、去掉箭头空白,添加替换箭头的图标列

在显示展开内容的列标签中设置width="1"

复制代码
<el-table-column type="expand" width="1" >
  <template #default="props">
     <div class="tableItem" :style="{ width: 'calc(100%)'}" >
       <el-table :data="props.row.family">
         <el-table-column type="index" width="70" label="排名" prop="name" align="center"/>
         <el-table-column prop="projectNum" label="项目编号" align="left"/>
          <el-table-column prop="projectName" label="项目名称"  align="left"/>
       </el-table>
     </div>
  </template>
</el-table-column>

<el-table-column width="40" align="center" >
   <template #default="scope" >
      <el-icon :size="15" v-if="scope.row.expanded" color="#000000">
         <Minus/>
      </el-icon>
      <el-icon :size="15" v-else color="#000000">
          <Plus/>
      </el-icon>
   </template>
 </el-table-column>

二、点击整行展开数据

表格数据:

复制代码
const tableData = ref([
  {projectNum:'YCA20241120001',
    id:'5862458213',
    projectName:'项目名称项目名称项目名称',
    month: '2024-10',
    expanded:false,
    family: [
      {
        projectNum:'YCA20241120001',
        projectName:'项目名称项目名称项目名称',
      },{
        projectNum:'YCA20241120001',
        projectName:'项目名称项目名称项目名称',
      }
    ]
  },
  {
    id:'5862456248',
    projectNum:'YCA20241120001',
    projectName:'项目名称项目名称项目名称',
    month: '2024-11',
    expanded:false,
  }
])

使用到el-table的三个属性,含义请看element文档

row-key="id"

:expand-row-keys="expands"

@row-click="clickRowHandle"

复制代码
<el-table :data="tableData" 
     v-loading="state.loading"  
     @selection-change="selectionChangHandle"
     @sort-change="sortChangeHandle"
     :border="false" 
     style="width: 100%" 
     row-key="id"
     :expand-row-keys="expands"
      @row-click="clickRowHandle">
</el-table>

逻辑代码:

复制代码
const expands = ref([])
//点击事件
const clickRowHandle = (row: any) => {
 row.expanded=!row.expanded
 if (expands.value.includes(row.id)) {
   expands.value = expands.value.filter(val => val !== row.id)
 }else {
   expands.value.push(row.id)
 }
}

三、外部表格序号和排名序号对齐

设置表格el-table-column的padding-left和magin-left是无效的

解决方法:

:cell-style="productiontableStyle"

:headerCellStyle="productiontableStyle"

复制代码
<el-table-column type="expand" width="1" >
  <template #default="props">
     <div class="tableItem" :style="{ width: 'calc(100%)'}" >
       <el-table :data="props.row.family" :cell-style="productiontableStyle" 
                :headerCellStyle="productiontableStyle">
         <el-table-column type="index" width="70" label="排名" prop="name" align="center"/>
         <el-table-column prop="projectNum" label="项目编号" align="left"/>
          <el-table-column prop="projectName" label="项目名称"  align="left"/>
       </el-table>
     </div>
  </template>
</el-table-column>

逻辑代码:

复制代码
const productiontableStyle=(column:any) =>{
    if(column.columnIndex === 0) {
        return {'padding-left':'15px'}
    }
}
相关推荐
东方小月4 小时前
从零开发一个 Coding Agent(四):使用状态机校验大模型事件流
前端·人工智能·后端
Csvn5 小时前
🧩 ESM vs CJS 混用的 7 个「天坑」——从 TypeScript 编译到 Node 与浏览器
前端
Csvn5 小时前
🎯 Web 性能 API 集合:Performance Observer 的 5 个冷门妙用
前端
Csvn5 小时前
深入 React 闭包陷阱:从根源上理解并根治 stale closure
前端
whyfail5 小时前
前端学 Spring Boot(8):接口为什么越用越慢?
前端·spring boot·后端
用户059540174466 小时前
LangChain 记忆测试踩坑实录:这两个坑让我排查了 4 小时
前端·css
程序员黑豆6 小时前
鸿蒙应用开发:@Monitor 装饰器使用教程
前端·harmonyos
SamChan907 小时前
在Web应用中集成PDF多语言翻译功能:PDFTranslator API实战指南
前端·python·ai·pdf·yapi·机器翻译
kyriewen7 小时前
AI Agent 9秒删光了生产数据库——我给自己的项目做了5个紧急检查
前端·ai编程·claude
IT_陈寒7 小时前
JavaScript的this又双叒叕让我怀疑人生了
前端·人工智能·后端