Vue實現在當前表格資料複製後可編輯、儲存功能程式碼
阿新 • • 發佈:2018-12-31
export default {
name: 'tb',
data () {
return {
items: [{
id: 1,
name: 'zhansgan1',
hight: '168cm',
weight: {id: 1, name: '168kg'},
rank: {id: 1, name: '一級1'},
isClone: false
}, {
id: 2,
name: 'zhansgan2' ,
hight: '168cm',
weight: {id: 2, name: '167kg'},
rank: {id: 2, name: '一級2'},
isClone: false
}, {
id: 3,
name: 'zhansgan3',
hight: '168cm',
weight: {id: 3, name: '166kg'},
rank: {id: 3, name: '一級3'},
isClone: false
}],
rank: [],
weight: {
data: [{
id: 1,
name: '168kg'
}, {
id: 2,
name: '167kg'
}, {
id: 3,
name: '166kg'
}],
index: -100,
isShow: false
}
};
},
methods: {
// 複製使用者
useclone (id, index) {
let arr = this.items.slice(index, index + 1)[0];
// 注意:轉成json再反轉回來,不能直接使用賦值,不然會和複製的一層公用同一個記憶體單元地址,會得到一樣的結果
let obj = JSON.stringify(arr);
let newObj = JSON.parse(obj);
console.log(newObj);
/** 這裡可以對複製的資料進行操作 ***/
newObj.name = ''; // 清空名字
newObj.isClone = true; // 顯示操作按鈕
/** 這裡可以對複製的資料進行操作 ***/
this.items.splice(index + 1, 0, newObj);
},
// 選擇體重彈窗
showWeight (id, index) {
this.weight.isShow = true;
this.weight.index = index;
console.log(this.weight);
},
// 體重彈窗選擇對應體重
chooseWeight (index, dbIndex) {
console.log(this.items[dbIndex]);
console.log(this.weight.data[index]);
this.items[dbIndex].weight = this.weight.data[index];
},
// 儲存使用者資訊
save (index) {
alert(JSON.stringify(this.items[index]));
console.log(this.items[index]);
},
// 刪除使用者資訊
del (index, userId) {
this.items.splice(index, 1);
console.log(userId);
},
// 阻止冒泡
stopPropa () {
let e = event || window.event;
e.stopPropagation();
return false;
}
},
created () {
// 初始化rank
this.rank = [{
id: 1,
name: '一級1'
}, {
id: 2,
name: '一級2'
}, {
id: 3,
name: '一級3'
}];
}
};