美文网首页
【vue+elementui】el-table 内嵌表格混乱

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

作者: IrisLong | 来源:发表于2021-02-20 11:14 被阅读0次

    问题描述

    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>
    
    • row-click: 点击当前行触发此函数,可省略。expand-row-keys: 可以通过该属性设置 Table 目前的展开行,需要设置 row-key 属性才能使用,该属性为展开行的 keys 数组。
    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))
            },
        }
    }
    
    • expand-change: 当用户对某一行展开或者关闭的时候会触发该事件(展开行时,回调的第二个参数为 expandedRows;树形表格时第二参数为 expanded)

    相关文章

      网友评论

          本文标题:【vue+elementui】el-table 内嵌表格混乱

          本文链接:https://www.haomeiwen.com/subject/kdvaxltx.html