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>
相关推荐
Mintopia21 分钟前
🤖 AI 决策 + 意图OS:未来软件形态的灵魂共舞
前端·人工智能·react native
攀登的牵牛花21 分钟前
前端向架构突围系列 - 框架设计(四):依赖倒置原则(DIP)
前端·架构
Van_Moonlight21 分钟前
RN for OpenHarmony 实战 TodoList 项目:已完成未完成数量显示
javascript·开源·harmonyos
程序员爱钓鱼29 分钟前
Node.js 编程实战:测试与调试 —— 日志与监控方案
前端·后端·node.js
计算机学姐35 分钟前
基于SpringBoot的汉服租赁系统【颜色尺码套装+个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·mysql·信息可视化·推荐算法
+VX:Fegn089536 分钟前
计算机毕业设计|基于springboot + vue建筑材料管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Mapmost38 分钟前
数字孪生项目效率翻倍!AI技术实测与场景验证实录
前端
小酒星小杜41 分钟前
在AI时代,技术人应该每天都要花两小时来构建一个自身的构建系统-Input篇
前端·程序员·架构
雪碧聊技术43 分钟前
ElementPlus徽章组件:展示日期面板每天未完成的待办数量
vue.js·日期选择器·elementplus·el-badge徽章组件