iview-table組件嵌套input?select數(shù)據(jù)無法雙向綁定解決
一、前言
本篇主要介紹關于iview-ui組件庫中的table表格組件嵌套input組件,數(shù)據(jù)無法及時更新問題的解決辦法。
二、問題描述
在我們開發(fā)的過程中,經(jīng)常會遇到需要在表格內(nèi)操作數(shù)據(jù)的情況,但是在vue2中,雙向綁定的值必須是在data中聲明的變量,然而我們在table中展示的每一行數(shù)據(jù),通常都是使用的scope中的row去獲取的當前行數(shù)據(jù),但是row卻并沒有在data中聲明,這樣就出現(xiàn)了,無法實現(xiàn)數(shù)據(jù)的雙向綁定。
三、解決辦法
因為在使用場景中,有時候我們用的一維的表格,還有一種就是樹狀結構的表格,所以這里我們分兩種情況說。
1.基礎數(shù)據(jù)表格
第一種就是一維數(shù)組的基礎型數(shù)據(jù)的table展示。如下圖

這種情況,為了實現(xiàn)數(shù)據(jù)的雙向綁定,我們可以這么做,代碼如下
<template>
<Table :columns="columns" :data="tableData">
<template #name="{ row, index }">
<Input v-model="tableData[index].name" />
</template>
<template #desc="{ row, index }">
<Input v-model="tableData[index].desc" />
</template>
<template #action="{ row, index }">
<Button type="text">刪除</Button>
</template>
</Table>
</template>
export default {
data () {
return {
tableData: []
columns: [
{
type: 'index',
title: '序號',
width: 80,
align: 'center'
},
{
title: '參數(shù)名稱',
slot: 'name'
},
{
title: '參數(shù)描述',
slot: 'desc'
},
{
title: '操作',
slot: 'action',
width: 120
}
]
}
}
}
這種基礎型數(shù)據(jù)結構的表格,我們就可以直接利用tableData[index]來替代row,因為tableData在data中聲明了,所以這時候,input上綁定的數(shù)據(jù),是可以實現(xiàn)雙向綁定的。
2.樹形數(shù)據(jù)表格
第二種樹形結構的數(shù)據(jù)表格,展示情況如下圖所示

這種情況,我們就不能用上面的方法了,因為我們不能通過index去獲取更多維深度的數(shù)據(jù)了,因此這里我們可以通過事件監(jiān)聽的方式,去查詢當前改變數(shù)據(jù)是哪個,利用樹型數(shù)據(jù)的唯一_key值,去這個綁定數(shù)組tabelData中修改對應的值,代碼如下
<template>
<Table row-key="_key" :columns="columns" :data="tableData">
<template #label="{ row }">
<Input v-model="row.label" @on-change="updateTableData(row, 'label')" />
</template>
<template #componentName="{ row }">
<Select v-model="row.componentName" transfer @on-change="updateTableData(row, 'componentName')">
<Option v-for="item in componentList" :value="item.value" :key="item.value">{{ item.label }}</Option>
</Select>
</template>
<template #required="{ row }">
<Checkbox v-model="row.required" @on-change="updateTableData(row, 'required')" />
</template>
<template #hidden="{ row }">
<Checkbox v-model="row.hidden" @on-change="updateTableData(row, 'hidden')" />
</template>
<template #defaultValue="{ row }">
<Input v-model="row.defaultValue" @on-change="updateTableData(row, 'defaultValue')" />
</template>
</Table>
</template>
export default {
data () {
return {
tableData: [],
componentList: [
{
value: 'TextField',
label: '文本組件'
}
],
columns: [
{
title: '字段名稱',
key: 'name',
tree: true
},
{
title: '顯示名稱',
slot: 'label',
},
{
title: '字段類型',
slot: 'componentName'
},
{
title: '必填',
slot: 'required',
width: 60
},
{
title: '隱藏',
slot: 'hidden',
width: 60
},
{
title: '默認值 ',
slot: 'defaultValue'
}
]
}
},
methods: {
// 找到對應值遞歸---手動更新
repeatDoit (list, row , key) {
list.forEach(item => {
if (item._key === row._key) {
item[key] = row[key]
} else {
if (item.children && item.children.length) {
this.repeatDoit(item.children, row , key)
}
}
})
},
// 手動更新表格中的數(shù)據(jù)==body
updateTableData (row, key) {
this.repeatDoit(this.tableData, row, key)
console.log('this.tableData ====', this.tableData)
}
}
}
這里我們通過監(jiān)聽表單組件的事件,然后通過樹形結構的唯一_key值,去修改data中聲明tableData數(shù)組變量,從而實現(xiàn)數(shù)據(jù)的更新。
四、后記
以上我們就實現(xiàn)了解決iview-table組件嵌套input、select數(shù)據(jù)無法雙向綁定問題。另外還有一種解決方法,是使用render去構造表格,我不太喜歡那種寫法,所以這里就不說了,更多關于iview table數(shù)據(jù)雙向綁定的資料請關注腳本之家其它相關文章!
相關文章
基于vue3?vue-cli4?線上部署及優(yōu)化的問題
這篇文章主要介紹了基于vue3?vue-cli4?線上部署及優(yōu)化的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue3中的createGlobalState用法及示例詳解
createGlobalState 是 Vue 3 中一種管理全局狀態(tài)的簡便方式,通常用于管理多個組件間共享的狀態(tài),由 @vueuse/core 提供的,允許創(chuàng)建一個響應式的全局狀態(tài),本文給大家介紹了Vue3中的createGlobalState用法及示例,需要的朋友可以參考下2024-10-10
Vue讀取本地靜態(tài)文件json的2種方法以及優(yōu)缺點
這篇文章主要介紹了Vue讀取本地靜態(tài)文件json的2種方法以及優(yōu)缺點說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

