el-table表格的展开行,初始化的时候展开哪一行+设置点击行可展开功能

效果:

表格展开行官网使用:

通过设置 type="expand" 和 Scoped slot 可以开启展开行功能,el-table-column 的模板会被渲染成为展开行的内容,展开行可访问的属性与使用自定义列模板时的 Scoped slot 相同。

但是这种方法是有局限性,只是点击箭头展开,点击行没反应,这里做优化点击行也可以进行展开。

关键点(以下属性和事件缺一不可):

:row-key="getRowKeys"

:expand-row-keys="expands"

@row-click="clickRowHandle"

需要默认展开行只需要在expands存入需要展开行的id就可以了

html

html 复制代码
        <el-table :data="tableData"
                  :row-key="getRowKeys"
                  :expand-row-keys="expands"
                  @row-click="clickRowHandle"
                  border>
          <el-table-column type="expand">
            <template slot-scope="scope">
              <div class="expand-txt"><span>编号:</span> {{ scope.row.code }}</div>
              <div class="expand-txt"><span>批次:</span> {{  scope.row.batch }}</div>
              <div class="expand-txt"><span>名称:</span> {{  scope.row.name }}</div>
              <div class="expand-txt"><span>规格:</span> {{  scope.row.specifications }}</div>
              <div class="expand-txt"><span>参数:</span> {{  scope.row.parameter }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="code"
                           label="编号" width="120px">
          </el-table-column>
          <el-table-column prop="batch"
                           label="批次">
          </el-table-column>
          <el-table-column prop="name"
                           label="姓名">
          </el-table-column>
          <el-table-column prop="specifications"
                           label="规格">
          </el-table-column>
          <el-table-column prop="parameter"
                           label="参数">
          </el-table-column>
        </el-table>

script:

javascript 复制代码
<script>
export default {
  data () {
    return {
      tableData: [{
        id: 1,
        code: '2016-05-01',
        batch: 1,
        name: '王小虎',
        specifications: 'A',
        parameter: '上海市普陀区金沙江路 1518 弄'
      }, {
        id: 2,
        code: '2016-05-02',
        batch: 1,
        name: '王小虎',
        specifications: 'A',
        parameter: '上海市普陀区金沙江路 1517 弄'
      }, {
        id: 3,
        code: '2016-05-03',
        batch: 1,
        name: '王小虎',
        specifications: 'A',
        parameter: '上海市普陀区金沙江路 1519 弄'
      }, {
        id: 4,
        code: '2016-05-04',
        batch: 1,
        name: '王小虎',
        specifications: 'A',
        parameter: '上海市普陀区金沙江路 1516 弄'
      }],
      // 获取row的key值
      getRowKeys (row) {
        // console.log(row);
        return row.id;
      },
      expands: [],
    }
  },
  components: {
  },
  mounted () {
    // 在初始化的时候展开第一行都可以了
    this.expands.push(this.tableData[0].id);
  },
  methods: {
    clickRowHandle (row) { // , column, event
      // console.log(row, column, event);
      if (this.expands.includes(row.id)) {
        this.expands = this.expands.filter(val => val !== row.id)
        console.log(1, this.expands);
      } else {
        this.expands.push(row.id)
        console.log(2, this.expands);
      }
    }
  }
}
</script>
相关推荐
计算机安禾30 分钟前
【c++面向对象编程】第22篇:输入输出运算符重载:<< 与 >> 的友元实现
java·前端·c++
redreamSo35 分钟前
14 小时烧光 200 美金:Codex 和 Claude 的 /goal 命令打开了"放手跑"模式
前端
TingTing39 分钟前
Webpack5 前端工程化建设
前端
i220818 Faiz Ul44 分钟前
宠物猫之猫咖管理系统|基于java + vue宠物猫之猫咖管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·宠物猫之猫咖管理系统
gjwjuejin1 小时前
前端埋点第二弹:那些年我们踩过的坑,和填坑的正确姿势
javascript
A不落雨滴AI1 小时前
DKERP客户端重构纪实:4天自研控件库的“短命”教训,以及为什么我坚定选择原生Qt
前端
我叫黑大帅1 小时前
通过白名单解决 pnpm i 报错 Ignored build scripts
前端·javascript·面试
风止何安啊1 小时前
用 APP 背单词太无聊?我用 Trae Solo 移动端写个小游戏来准备 6级
前端·人工智能·trae
Summer不秃1 小时前
深入理解 Token 无感刷新:从并发雪崩到单例锁 + 请求队列的完整实现
前端·http
i220818 Faiz Ul1 小时前
二手交易系统|基于springboot + vue二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·二手交易系统