vue版本:2.6.14
ElementUI版本:^2.15.8
場景描述:在vue中使用ElementUI中的多選表格,在根據(jù)菜單項更新表格數(shù)據(jù)的時候之前選中的行狀態(tài)未被保存。
解決方法:在 type 為 selection 那一列添加 :reserver-selection="true",同時在 el-table 中添加 :row-key="getRowKeys" 并實現(xiàn) getRowKeys 方法,返回行數(shù)據(jù)唯一標(biāo)記值,我這里是使用 id 進行返回。
代碼如下:
<template>
<el-table ref="peopleTable" :data="peopleTableData" class="user-table" :row-key="getRowKeys">
<el-table-column type="selection" :reserve-selection="true">
</el-table-column>
</el-table>
...
</template>
<script>
export default {
...
methods: {
getRowKeys(row) {
return row.id; // 返回唯一標(biāo)記值,這里的 id 是唯一的
},
...
}
}
</script>