elementui的el-table自定义控制展开事件,实现“展开”“收起”的切换(两种效果)【超级完整式代码】

第一种:多行点击展开其他行不收起

先看效果图

直接上代码
【核心代码添加标注简单易懂】

 <el-table
            ref="multipleTable"
            :data="smsLogList.slice((currentPage - 1) * pageSize_, currentPage * pageSize_)"
            tooltip-effect="dark"
            style="width: 100%"
            row-key="id"   【核心代码】
            :expand-row-keys="expands"【核心代码】
          >
            <el-table-column label="短信模板" align="center" prop="templateName" />
            <el-table-column label="接收方" align="center" prop="receiverPhoneNumber" />
            <el-table-column label="发送时间" align="center" prop="sendTime" />
            【展开部分】
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-form label-position="right" inline class="demo-table-expand">
                  <el-form-item>
                    <span>{{ props.row.templateName }}</span>
                  </el-form-item>
                </el-form>
              </template>
            </el-table-column>
            【操作列】
            <el-table-column label="操作" width="120">
              <template #default="scope">
                <el-link
                  style="color:#1890ff"
                  @click="handleRowClick(scope.row)"
                >{{ scope.row.id == expands.filter((val) => val == scope.row.id) ? "收起" : "展开" }}</el-link>
              </template>
            </el-table-column>
          </el-table>

data中定义: expands:[]

handleRowClick(row) {
      if (this.expands.includes(row.id)) {
        this.expands = this.expands.filter((val) => val !== row.id)
      } else {
        this.expands.push(row.id)
      }
    },

第二种:多行点击展开其他行自动收起:手风琴效果
【修改】标注就是跟第一种代码不同的地方:两处修改

 <el-table
            ref="multipleTable"
            :data="smsLogList.slice((currentPage - 1) * pageSize_, currentPage * pageSize_)"
            tooltip-effect="dark"
            style="width: 100%"
            row-key="id" 
            :expand-row-keys="expands"
          >
            <el-table-column label="短信模板" align="center" prop="templateName" />
            <el-table-column label="接收方" align="center" prop="receiverPhoneNumber" />
            <el-table-column label="发送时间" align="center" prop="sendTime" />
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-form label-position="right" inline class="demo-table-expand">
                  <el-form-item>
                    <span>{{ props.row.templateName }}</span>
                  </el-form-item>
                </el-form>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="120">
              <template #default="scope">
                <el-link
                  style="color:#1890ff"
                  @click="handleRowClick(scope.row)"
                >{{ scope.row.id == expands[0]) ? "收起" : "展开" }}</el-link>   【修改】
              </template>
            </el-table-column>
          </el-table>

data中定义: expands:[]

handleRowClick(row) {
      if (this.expands.includes(row.id)) {
        this.expands = this.expands.filter((val) => val !== row.id)
      } else {
        this.expands=[]    【修改】
        this.expands.push(row.id)
      }
    },

隐藏elementUI 展开行 > 图标

::v-deep .el-table__expand-icon{
  visibility: hidden;
}

注意

  row-key="id"   这里是设置了每行的唯一值,所以表格数组对象中是需要有id的,如果没有可以手动添加

代码

 for (let i = 0; i < this.smsLogList.length; i++) {
          this.smsLogList[i].id = i + 1
        }
相关推荐
豆豆40 分钟前
为什么用PageAdmin CMS建设网站?
服务器·开发语言·前端·php·软件构建
JUNAI_Strive_ving1 小时前
番茄小说逆向爬取
javascript·python
看到请催我学习1 小时前
如何实现两个标签页之间的通信
javascript·css·typescript·node.js·html5
twins35202 小时前
解决Vue应用中遇到路由刷新后出现 404 错误
前端·javascript·vue.js
qiyi.sky2 小时前
JavaWeb——Vue组件库Element(3/6):常见组件:Dialog对话框、Form表单(介绍、使用、实际效果)
前端·javascript·vue.js
煸橙干儿~~2 小时前
分析JS Crash(进程崩溃)
java·前端·javascript
哪 吒2 小时前
华为OD机试 - 几何平均值最大子数(Python/JS/C/C++ 2024 E卷 200分)
javascript·python·华为od
安冬的码畜日常2 小时前
【D3.js in Action 3 精译_027】3.4 让 D3 数据适应屏幕(下)—— D3 分段比例尺的用法
前端·javascript·信息可视化·数据可视化·d3.js·d3比例尺·分段比例尺
l1x1n03 小时前
No.3 笔记 | Web安全基础:Web1.0 - 3.0 发展史
前端·http·html
Q_w77423 小时前
一个真实可用的登录界面!
javascript·mysql·php·html5·网站登录