Commit 7005264d authored by 徐俊's avatar 徐俊

xujun

parent dd4bbb3e
...@@ -54,12 +54,17 @@ export default { ...@@ -54,12 +54,17 @@ export default {
}; };
}, },
onChange (e) { onChange (e) {
if (!e || typeof e !== 'object') {
console.warn('尝试操作不存在的元素');
return;
}
e.selected = !e.selected e.selected = !e.selected
if (e.selected) { if (e.selected) {
this.selectKeys.push(e) this.selectKeys.push(e)
} else { } else {
for (let j = 0; j < this.selectKeys.length; j++) { for (let j = 0; j < this.selectKeys.length; j++) {
if (e.key == this.selectKeys[j].key) { if (e.key && e.key === this.selectKeys[j].key) {
this.selectKeys.splice(j, 1) this.selectKeys.splice(j, 1)
break break
} }
...@@ -71,10 +76,12 @@ export default { ...@@ -71,10 +76,12 @@ export default {
onCheckAllChange (e) { onCheckAllChange (e) {
if (e.target.checked) { if (e.target.checked) {
for (let i = 0; i < this.dataList.length; i++) { for (let i = 0; i < this.dataList.length; i++) {
if (!this.dataList[i]) continue;
this.dataList[i].selected = e.target.checked this.dataList[i].selected = e.target.checked
let state = false; let state = false;
for (let j = 0; j < this.selectKeys.length; j++) { for (let j = 0; j < this.selectKeys.length; j++) {
if (this.dataList[i].key == this.selectKeys[j].key) { if (this.dataList[i].key && this.selectKeys[j] && this.dataList[i].key === this.selectKeys[j].key) {
state = true state = true
break break
} }
...@@ -86,10 +93,13 @@ export default { ...@@ -86,10 +93,13 @@ export default {
} }
else { else {
for (let i = 0; i < this.dataList.length; i++) { for (let i = 0; i < this.dataList.length; i++) {
if (!this.dataList[i]) continue;
this.dataList[i].selected = e.target.checked this.dataList[i].selected = e.target.checked
for (let j = 0; j < this.selectKeys.length; j++) { for (let j = 0; j < this.selectKeys.length; j++) {
if (this.dataList[i].key == this.selectKeys[j].key) { if (this.selectKeys[j] && this.dataList[i].key && this.dataList[i].key === this.selectKeys[j].key) {
this.selectKeys.splice(j, 1); this.selectKeys.splice(j, 1);
break;
} }
} }
} }
...@@ -100,7 +110,7 @@ export default { ...@@ -100,7 +110,7 @@ export default {
initData () { initData () {
this.nowSelectKeys = [] this.nowSelectKeys = []
for (let i = 0; i < this.dataList.length; i++) { for (let i = 0; i < this.dataList.length; i++) {
if (this.dataList[i].selected) { if (this.dataList[i] && this.dataList[i].selected) {
this.nowSelectKeys.push(this.dataList[i]) this.nowSelectKeys.push(this.dataList[i])
} }
} }
...@@ -108,15 +118,22 @@ export default { ...@@ -108,15 +118,22 @@ export default {
this.checkAll = this.nowSelectKeys.length === this.dataList.length && this.nowSelectKeys.length > 0 this.checkAll = this.nowSelectKeys.length === this.dataList.length && this.nowSelectKeys.length > 0
}, },
ensureUniqueKeys() { ensureUniqueKeys() {
if (!Array.isArray(this.dataList)) {
console.warn('dataList 不是数组');
return;
}
const keyMap = new Map(); const keyMap = new Map();
let hasDuplicates = false; let hasDuplicates = false;
this.dataList.forEach((item, index) => { const validDataList = this.dataList.filter(item => item && typeof item === 'object');
validDataList.forEach((item, index) => {
if (!item.key) { if (!item.key) {
item.key = 'generated-' + index; item.key = 'generated-' + Date.now() + '-' + index;
} else if (keyMap.has(item.key)) { } else if (keyMap.has(item.key)) {
console.warn(`发现重复键: ${item.key},已自动修复`); console.warn(`发现重复键: ${item.key},已自动修复`);
item.key = item.key + '-' + index; item.key = item.key + '-' + Date.now() + '-' + index;
hasDuplicates = true; hasDuplicates = true;
} }
keyMap.set(item.key, true); keyMap.set(item.key, true);
...@@ -125,16 +142,39 @@ export default { ...@@ -125,16 +142,39 @@ export default {
if (hasDuplicates) { if (hasDuplicates) {
this.initData(); this.initData();
} }
},
cleanupSelectKeys() {
if (!Array.isArray(this.selectKeys)) return;
const validKeys = new Set();
this.dataList.forEach(item => {
if (item && item.key) validKeys.add(item.key);
});
this.selectKeys = this.selectKeys.filter(item => {
return item && item.key && validKeys.has(item.key);
});
} }
}, },
watch: { watch: {
dataList: { dataList: {
handler(dataList) { handler(dataList) {
this.ensureUniqueKeys(); this.ensureUniqueKeys();
this.cleanupSelectKeys();
this.initData(); this.initData();
}, },
immediate: true immediate: true
}, },
value: {
handler(val) {
if (Array.isArray(val)) {
this.selectKeys = val;
this.cleanupSelectKeys();
this.initData();
}
},
immediate: true
}
} }
} }
</script> </script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment