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

xujun

parent dd4bbb3e
......@@ -54,12 +54,17 @@ export default {
};
},
onChange (e) {
if (!e || typeof e !== 'object') {
console.warn('尝试操作不存在的元素');
return;
}
e.selected = !e.selected
if (e.selected) {
this.selectKeys.push(e)
} else {
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)
break
}
......@@ -71,10 +76,12 @@ export default {
onCheckAllChange (e) {
if (e.target.checked) {
for (let i = 0; i < this.dataList.length; i++) {
if (!this.dataList[i]) continue;
this.dataList[i].selected = e.target.checked
let state = false;
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
break
}
......@@ -86,10 +93,13 @@ export default {
}
else {
for (let i = 0; i < this.dataList.length; i++) {
if (!this.dataList[i]) continue;
this.dataList[i].selected = e.target.checked
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);
break;
}
}
}
......@@ -100,7 +110,7 @@ export default {
initData () {
this.nowSelectKeys = []
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])
}
}
......@@ -108,15 +118,22 @@ export default {
this.checkAll = this.nowSelectKeys.length === this.dataList.length && this.nowSelectKeys.length > 0
},
ensureUniqueKeys() {
if (!Array.isArray(this.dataList)) {
console.warn('dataList 不是数组');
return;
}
const keyMap = new Map();
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) {
item.key = 'generated-' + index;
item.key = 'generated-' + Date.now() + '-' + index;
} else if (keyMap.has(item.key)) {
console.warn(`发现重复键: ${item.key},已自动修复`);
item.key = item.key + '-' + index;
item.key = item.key + '-' + Date.now() + '-' + index;
hasDuplicates = true;
}
keyMap.set(item.key, true);
......@@ -125,16 +142,39 @@ export default {
if (hasDuplicates) {
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: {
dataList: {
handler(dataList) {
this.ensureUniqueKeys();
this.cleanupSelectKeys();
this.initData();
},
immediate: true
},
value: {
handler(val) {
if (Array.isArray(val)) {
this.selectKeys = val;
this.cleanupSelectKeys();
this.initData();
}
},
immediate: true
}
}
}
</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