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>
相关推荐
Keepreal4966 分钟前
使用Canvas绘制转盘
javascript·vue.js·canvas
mapbar_front12 分钟前
我们需要前端架构师这个职位吗?
前端
ScriptBIN20 分钟前
Javaweb--Vue
前端·vue.js
KenXu25 分钟前
React Conf 2025 - 核心更新
前端
前端Hardy29 分钟前
Vue 高效开发技巧合集:10 个实用技巧让代码简洁 50%+,面试直接加分!
前端·javascript·vue.js
ᖰ・◡・ᖳ1 小时前
JavaScript:神奇的ES6之旅
前端·javascript·学习·es6
app出海创收老李1 小时前
海外独立创收日记(5)-上个月收入回顾与本月计划
前端·后端·程序员
前端Hardy1 小时前
HTML&CSS:一眼心动的 SVG 时钟
前端·javascript·css
TTGGGFF1 小时前
Streamlit:CSS——从基础到实战美化应用
前端·css
app出海创收老李1 小时前
海外独立创收日记(4)-第一笔汇款
前端·后端·程序员