Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ac78cf

Browse files
authored
Merge pull request lin-xin#178 from lin-xin/dev
修复表格顺序改变后误删其他数据
2 parents dc34058 + ce21d13 commit 5ac78cf

File tree

2 files changed

+74
-43
lines changed

2 files changed

+74
-43
lines changed

public/vuetable.json

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
{
22
"list": [{
3-
"date": "1997-11-11",
4-
"name": "林丽",
5-
"address": "吉林省 辽源市 龙山区"
6-
}, {
7-
"date": "1987-09-24",
8-
"name": "文敏",
9-
"address": "江西省 萍乡市 芦溪县"
10-
}, {
11-
"date": "1996-08-08",
12-
"name": "杨秀兰",
13-
"address": "黑龙江省 黑河市 五大连池市"
14-
}, {
15-
"date": "1978-06-18",
16-
"name": "魏强",
17-
"address": "广东省 韶关市 始兴县"
18-
}, {
19-
"date": "1977-07-09",
20-
"name": "石秀兰",
21-
"address": "江苏省 宿迁市 宿豫区"
22-
}, {
23-
"date": "1994-09-20",
24-
"name": "朱洋",
25-
"address": "海外 海外 -"
26-
}, {
27-
"date": "1980-01-22",
28-
"name": "傅敏",
3+
"id": 1,
4+
"date": "2013-05-04",
5+
"name": "邱秀兰",
6+
"address": "浙江省 金华市 兰溪市"
7+
}, {
8+
"id": 2,
9+
"date": "1991-05-02",
10+
"name": "蒋军",
11+
"address": "广东省 东莞市 长安镇"
12+
}, {
13+
"id": 3,
14+
"date": "2009-12-01",
15+
"name": "廖艳",
16+
"address": "陕西省 渭南市 富平县"
17+
}, {
18+
"id": 4,
19+
"date": "1999-07-15",
20+
"name": "秦强",
21+
"address": "内蒙古自治区 巴彦淖尔市 磴口县"
22+
}, {
23+
"id": 5,
24+
"date": "1997-02-09",
25+
"name": "唐洋",
26+
"address": "湖南省 长沙市 "
27+
}, {
28+
"id": 6,
29+
"date": "2018-05-23",
30+
"name": "易勇",
31+
"address": "重庆 重庆市 双桥区"
32+
}, {
33+
"id": 7,
34+
"date": "2014-04-09",
35+
"name": "袁丽",
36+
"address": "陕西省 铜川市 耀州区"
37+
}, {
38+
"id": 8,
39+
"date": "2013-08-05",
40+
"name": "常刚",
2941
"address": "海外 海外 -"
3042
}, {
31-
"date": "1985-10-10",
32-
"name": "毛明",
33-
"address": "内蒙古自治区 包头市 九原区"
43+
"id": 9,
44+
"date": "1995-12-13",
45+
"name": "龚勇",
46+
"address": "黑龙江省 牡丹江市 西安区"
3447
}, {
35-
"date": "1975-09-08",
36-
"name": "何静",
37-
"address": "西藏自治区 阿里地区 普兰县"
38-
}, {
39-
"date": "1970-06-07",
40-
"name": "郭秀英",
41-
"address": "四川省 巴中市 恩阳区"
48+
"id": 10,
49+
"date": "2017-06-06",
50+
"name": "魏强",
51+
"address": "安徽省 六安市 裕安区"
4252
}]
4353
}

src/components/page/BaseTable.vue

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@
8787
date: '',
8888
address: ''
8989
},
90-
idx: -1
90+
idx: -1,
91+
id: -1
9192
}
9293
},
9394
created() {
@@ -143,16 +144,18 @@
143144
},
144145
handleEdit(index, row) {
145146
this.idx = index;
146-
const item = this.tableData[index];
147+
this.id = row.id;
147148
this.form = {
148-
name: item.name,
149-
date: item.date,
150-
address: item.address
149+
id: row.id,
150+
name: row.name,
151+
date: row.date,
152+
address: row.address
151153
}
152154
this.editVisible = true;
153155
},
154156
handleDelete(index, row) {
155157
this.idx = index;
158+
this.id = row.id;
156159
this.delVisible = true;
157160
},
158161
delAll() {
@@ -170,15 +173,33 @@
170173
},
171174
// 保存编辑
172175
saveEdit() {
173-
this.$set(this.tableData, this.idx, this.form);
174176
this.editVisible = false;
175177
this.$message.success(`修改第 ${this.idx+1} 行成功`);
178+
if(this.tableData[this.idx].id === this.id){
179+
this.$set(this.tableData, this.idx, this.form);
180+
}else{
181+
for(let i = 0; i < this.tableData.length; i++){
182+
if(this.tableData[i].id === this.id){
183+
this.$set(this.tableData, i, this.form);
184+
return ;
185+
}
186+
}
187+
}
176188
},
177189
// 确定删除
178190
deleteRow(){
179-
this.tableData.splice(this.idx, 1);
180191
this.$message.success('删除成功');
181192
this.delVisible = false;
193+
if(this.tableData[this.idx].id === this.id){
194+
this.tableData.splice(this.idx, 1);
195+
}else{
196+
for(let i = 0; i < this.tableData.length; i++){
197+
if(this.tableData[i].id === this.id){
198+
this.tableData.splice(i, 1);
199+
return ;
200+
}
201+
}
202+
}
182203
}
183204
}
184205
}

0 commit comments

Comments
 (0)