很奇怪 在_confirm方法末尾得到了有值的数组this.selectionData,但一进入onSearch方法,this.selectionData数组被清空了。
解决办法:_confirm方法最后拷贝
this.selectionDataCopy = deepClone(this.selectionData)
// An highlighted block
onSearch () { //弹框搜索 <el-input v-model="form.keyword" auto-complete="off" placeholder='输入关键字搜索' @keyup.native.enter="onSearch">
console.log(this.selectionData, 'onSearch刚进') //打印出空数组 想不通
this.getPickData((data) => {
// 更新选中的数据,搜索时已选中的数据需要保留
// this.toggleSection(this.selectionData)
if (data) {
console.log(this.selectionData, 'onSearch')
this.toggleSection(this.selectionDataCopy)
}
})
},
_confirm (row) { //<el-table :data="tableData" height="300" @selection-change='_confirm' ref="table">
console.log(this.selectionData, 'selectionData _confirm刚进')
let ssaId = []
let ssaNo = []
let ssaName = []
let ssaRows = []
row.map(item => {
ssaId.push(item.Ssa_Id)
ssaNo.push(item.Ssa_No)
ssaName.push(item.Ssa_Name)
})
this.paramsList = {
ssaId: ssaId,
ssaNo: ssaNo,
ssaName: ssaName,
ssaRows: [].concat(row)
}
if (this.selectionData.length === 0) {
this.selectionData = row
} else {
let array = this.selectionData.map(item => item.Ssa_Id)
let tArray = this.tableData.map(item => item.Ssa_Id)
let rArray = row.map(item => item.Ssa_Id)
row.forEach(item => {
if (array.indexOf(item.Ssa_Id) === -1) {
this.selectionData.push(item)
}
})
for (let i = 0; i < this.selectionData.length; i++) {
if (tArray.indexOf(this.selectionData[i].Ssa_Id) > -1 && rArray.indexOf(this.selectionData[i].Ssa_Id) === -1) {
this.selectionData.splice(i--, 1)
}
}
}
this.selectionDataCopy = deepClone(this.selectionData)
console.log(this.selectionData, 'selectionData _confirm刚走完')
},
toggleSection (row) { // 列表勾选上
this.$nextTick(() => {
let tempData = []
let that = this
if (row) {
tempData = row
this.tableData.map((temp, index) => {
tempData.map((item, i) => {
if (item.Ssa_Id === temp.Ssa_Id) {
that.$refs['table'].toggleRowSelection(temp, true)
}
})
})
} else {
that.$refs['table'].clearSelection()
}
})
}