切換頁碼選中數(shù)據(jù)不清空
方法一:在當前頁遍歷,判斷選擇的數(shù)據(jù)在此頁中是否被選中,是否在緩存區(qū)
點擊保存時,將緩存的數(shù)據(jù)存進真正需要的地方
初始化時,將真實數(shù)據(jù)賦給暫存數(shù)據(jù)
取消時,恢復初始數(shù)據(jù)
selection 是當前選中的數(shù)據(jù)
RelateCacheCode緩存選中未保存的數(shù)據(jù)
projectList 當前頁的數(shù)據(jù)
dealSelect (selection) {
// selection是當前選中的數(shù)據(jù)
let ids = []
this.RelateCacheCode.forEach(item => {
ids.push(item.projectId)
})
let selectIds = []
selection.forEach(item => {
selectIds.push(item.projectId)
})
this.projectList.forEach(item => {
if (selectIds.indexOf(item.projectId) > -1) {
let index = ids.indexOf(item.projectId)
// 是當前頁的數(shù)據(jù)并且被選中
if (index === -1 || !ids.length) {
// 不在所有操作的選中的數(shù)據(jù)里面,說明是新增的一條,加到選中數(shù)據(jù)里面
this.RelateCacheCode.push(item)
}
} else {
// 當前頁的數(shù)據(jù),但沒被選中
let index = ids.indexOf(item.projectId)
if (ids.length && index > -1) {
// 判斷此數(shù)據(jù)是否在所有選中數(shù)據(jù)里,存在,則是取消勾選,應當刪掉此條
this.RelateCacheCode.splice(index, 1)
// 緩存選中數(shù)據(jù)也要相應減少一條
ids.splice(ids.indexOf(item.projectId), 1)
}
}
})
},
// 回顯時使用下面判斷
this.projectList.map((item, i) => {
if (this.RelateCacheCode.length !== 0) {
let selectedId = []
this.RelateCacheCode.forEach((item, index) => {
selectedId.push(item.projectId)
})
if (selectedId.indexOf(item.projectId) !== -1) {
item.checked = true // 是否勾選
}
}
})
方法二:使用多維數(shù)組保存選中數(shù)據(jù),此方法不需要暫存數(shù)據(jù),但是產(chǎn)品說我要加個搜索功能,就不能使用了(不推薦)
this.SelectList[this.currentPage] = selection
// 保存時降維處理
let list = this.SelectList.flat(Infinity)
// 回顯時使用SelectCodeList的降維數(shù)組判斷(初始化時也需要賦值)