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>
相关推荐
daols882 小时前
vue vxe-table 自适应列宽,根据内容自适应宽度的2种使用方式
vue.js·vxe-table
小小小小宇2 小时前
虚拟列表兼容老DOM操作
前端
悦悦子a啊2 小时前
Python之--基本知识
开发语言·前端·python
安全系统学习3 小时前
系统安全之大模型案例分析
前端·安全·web安全·网络安全·xss
涛哥码咖4 小时前
chrome安装AXURE插件后无效
前端·chrome·axure
OEC小胖胖4 小时前
告别 undefined is not a function:TypeScript 前端开发优势与实践指南
前端·javascript·typescript·web
行云&流水4 小时前
Vue3 Lifecycle Hooks
前端·javascript·vue.js
Sally璐璐4 小时前
零基础学HTML和CSS:网页设计入门
前端·css
老虎06274 小时前
JavaWeb(苍穹外卖)--学习笔记04(前端:HTML,CSS,JavaScript)
前端·javascript·css·笔记·学习·html
三水气象台5 小时前
用户中心Vue3网页开发(1.0版)
javascript·css·vue.js·typescript·前端框架·html·anti-design-vue