【vue+elementui】el-table 内嵌表格混乱

2021-02-20  本文已影响0人  IrisLong

问题描述

  1. 内嵌表格数据混乱,点击展开一行,结果展开了所有行
    原因::row-key="getRowKeys" 绑定错了数据,此处应该绑定父级table的主键,而不是子级别table的主键
  2. 调用 getList 接口重新查询父级表格数据后,子级表格数据丢失
    原因:页面数据重新加载了,但是子级表格数据没有再次加载,所以数据丢失了

核心代码:

<el-table
    :data="tableData.list"
    @selection-change="selectChange"
    ref="_table"
    border
    highlight-current-row
    header-cell-class-name="tableHeader"
    size="mini"
    :row-key="getRowKeys"
        @row-click="handleRowClick"
    :expand-row-keys="expands"
    @expand-change="expandChange"
>
    <el-table-column type="selection"></el-table-column>
    <el-table-column prop="wealthId" label="合并编号" width="90"> </el-table-column>
    <el-table-column type="expand" label="展开">
        <template slot-scope="props">
            <el-table
                :data="props.row.detailList"
                border
                header-cell-class-name="tableHeader"
                size="mini"
            >
                <el-table-column prop="businessDate" label="日期" width="100" :formatter="formatTime">
                </el-table-column>
                <el-table-column prop="contractNo" label="本金编号" width="100"> </el-table-column>
                <el-table-column prop="businessTypeName" label="业务类型" width="120">
                </el-table-column>
                <el-table-column
                    prop="principalBalanceMoney"
                    label="本金"
                    :formatter="formatterAmount"
                    width="150"
                >
                </el-table-column>
                <el-table-column prop="expectedReturnRate" label="预期收益率" width="90">
                </el-table-column>
                <el-table-column prop="interestChangeMoney" label="利息变动" width="120">
                </el-table-column>
                <el-table-column prop="changeType" label="计息截止日" width="100"> </el-table-column>
            </el-table>
        </template>
    </el-table-column>
</el-table>
export default {
    data() {
        return {
       expands: [], //要展开的行,数值的元素是row的key值
            openDetailList: [],
      }
    },
    methods: {
    // 获取父级table,行数据的key值
        getRowKeys(row) {
            return row.wealthId
        },
        handleRowClick(row) {
            console.log('点击行: ', row)
            this.$refs._table.toggleRowSelection(row)
            this.$refs._table.toggleRowExpansion(row)
            this.expands.includes(row.wealthDetailId)
                ? (this.expands = this.expands.filter(v => v !== row.wealthDetailId))
                : this.expands.push(row.wealthDetailId)
        },
        async expandChange(row, expandedRows) {
            if (expandedRows.length > 0) {
                if (!this.openDetailList.includes(row.wealthId)) {
                    this.getWealthDetail(row.wealthId)
                }
            }
        },
        async getWealthDetail(wealthId) {
            let { data } = await findWealthDetail({
                wealthId: wealthId,
            })
            if (data.ret === 1) {
                this.openDetailList.push(wealthId)
                console.log('所有展开行:', this.openDetailList)
                let curDetailObj = data.data
                if (curDetailObj.length) {
                    curDetailObj = data.data.map((item, index) => {
                        let key = 'hiddenDel'
                        let val = index === curDetailObj.length - 1 ? false : true
                        item[key] = val
                        return item
                    })
                }
        // 比较是点击的父级table内部具体哪一个行数据的明细数据!
                for (let i = 0, len = this.tableData.list.length; i < len; i++) {
                    if (wealthId === this.tableData.list[i].wealthId) {
                        this.$set(this.tableData.list[i], 'detailList', curDetailObj)
                        break
                    }
                }
            } else {
                this.$message.error(data.msg)
            }
        },
    // 解决问题2
    resetDetailList() {
            this.getList()
            if (!this.openDetailList.length) return
            let exitExpandsList = [...new Set(this.openDetailList)]
            exitExpandsList.forEach(v => this.getWealthDetail(v))
        },
    }
}
上一篇下一篇

猜你喜欢

热点阅读