怎么修改addtable和cancle方法使之能够在新增数据后按下取消能去掉这一行
时间: 2024-12-15 15:25:21 浏览: 13
为了使 `AddTable` 和 `cancel` 方法能够实现新增一行并在取消时删除该行的功能,需要对这两个方法进行一些调整。以下是一个具体的解决方案:
### 调整后的代码
#### `AddTable` 方法
保持现有逻辑不变,但在每次调用 `AddTable` 时记录新行的键。
```javascript
methods: {
AddTable() {
const newRecord = {
key: this.uniqueKeyGenerator(),
meterNum: '',
gmType2: '',
vendorCode: '',
meterNum1: '',
batch: '',
productionDate: null, // 确保日期字段为空
validityYears: '',
iccid: '',
imei: '',
statu: '',
depart: '',
departDate: null, // 确保日期字段为空
intakeDirection: '',
ware1house: '',
gauge: '',
minFlow: '',
maxFlow: '',
maxWorkPressure: '',
workVoltage: '',
editable: true // 启用编辑模式
};
this.data = [...this.data, newRecord];
this.cacheData = [...this.cacheData, { ...newRecord }];
this.editingKey = newRecord.key;
// 记录新行的键
this.newRowKeys = [...this.newRowKeys, newRecord.key];
},
// 其他方法...
}
```
#### `cancel` 方法
在 `cancel` 方法中检查当前编辑的行是否是新行,如果是则删除它。
```javascript
methods: {
cancel(key) {
this.editingKey = '';
if (this.newRowKeys.includes(key)) {
// 如果是新行,则直接删除
this.data = this.data.filter(item => item.key !== key);
this.cacheData = this.cacheData.filter(item => item.key !== key);
this.newRowKeys = this.newRowKeys.filter(rowKey => rowKey !== key);
} else {
// 否则,恢复原始数据
const newData = [...this.data];
const targetIndex = newData.findIndex(item => key === item.key);
if (targetIndex !== -1) {
const target = newData[targetIndex];
const originalRecord = this.cacheData.find(item => item.key === key);
if (originalRecord) {
Object.assign(target, originalRecord);
target.editable = false; // 禁用编辑模式
this.data = newData;
}
}
}
},
// 其他方法...
}
```
### 完整的方法部分
```javascript
export default {
name: 'BiMeterReplace',
mixins: [JeecgListMixin, mixinDevice, initColumnsMixin],
components: {
StockMeterBaseDrawer,
JSuperQuery,
draggable,
BizBatchImport,
Drawer,
StockMeterAddForm,
ADatePicker: DatePicker,
},
data() {
this.cacheData = data.map(item => ({ ...item }));
return {
data,
editingKey: '',
visible: false,
newRowKeys: [],
defColumns: [
// 列定义...
],
url: {
list: '/stock/stockMeterBase/list',
delete: '/stock/stockMeterBase/delete',
deleteBatch: '/stock/stockMeterBase/deleteBatch',
changeStatusBatch: '/stock/stockMeterBase/changeStatusBatch',
exportXlsxUrl: '/stock/stockMeterBase/exportExcel',
importXlsxUrl: '/stock/stockMeterBase/importExcel',
exportXlsxTemplateUrl: '/stock/stockMeterBase/exportExcelTemplate'
},
dictOptions: {},
superFieldList: []
};
},
created() {
this.getSuperFieldList();
},
computed: {
uniqueKeyGenerator() {
return () => `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}
},
methods: {
AddTable() {
const newRecord = {
key: this.uniqueKeyGenerator(),
meterNum: '',
gmType2: '',
vendorCode: '',
meterNum1: '',
batch: '',
productionDate: null, // 确保日期字段为空
validityYears: '',
iccid: '',
imei: '',
statu: '',
depart: '',
departDate: null, // 确保日期字段为空
intakeDirection: '',
ware1house: '',
gauge: '',
minFlow: '',
maxFlow: '',
maxWorkPressure: '',
workVoltage: '',
editable: true // 启用编辑模式
};
this.data = [...this.data, newRecord];
this.cacheData = [...this.cacheData, { ...newRecord }];
this.editingKey = newRecord.key;
// 记录新行的键
this.newRowKeys = [...this.newRowKeys, newRecord.key];
},
handleChange(value, key, column) {
const newData = [...this.data];
const target = newData.find(item => key === item.key);
if (target) {
target[column] = value;
this.data = newData;
}
},
edit(key) {
const newData = [...this.data];
const target = newData.find(item => key === item.key);
this.editingKey = key;
if (target) {
target.editable = true;
this.data = newData;
}
},
save(key) {
const newData = [...this.data];
const newCacheData = [...this.cacheData];
const target = newData.find(item => key === item.key);
const targetCache = newCacheData.find(item => key === item.key);
if (target && targetCache) {
delete target.editable;
this.data = newData;
Object.assign(targetCache, target);
this.cacheData = newCacheData;
} else {
console.error('Target or Cache not found');
}
this.editingKey = '';
this.$forceUpdate();
},
cancel(key) {
this.editingKey = '';
if (this.newRowKeys.includes(key)) {
// 如果是新行,则直接删除
this.data = this.data.filter(item => item.key !== key);
this.cacheData = this.cacheData.filter(item => item.key !== key);
this.newRowKeys = this.newRowKeys.filter(rowKey => rowKey !== key);
} else {
// 否则,恢复原始数据
const newData = [...this.data];
const targetIndex = newData.findIndex(item => key === item.key);
if (targetIndex !== -1) {
const target = newData[targetIndex];
const originalRecord = this.cacheData.find(item => item.key === key);
if (originalRecord) {
Object.assign(target, originalRecord);
target.editable = false; // 禁用编辑模式
this.data = newData;
}
}
}
},
showDrawer() {
this.visible = true;
},
onClose() {
this.visible = false;
},
getSuperFieldList() {
let fieldList = [];
// 字段列表...
this.superFieldList = fieldList;
}
}
};
```
通过以上调整,您可以在新增数据后按下取消按钮,该行将被正确地删除或恢复到初始状态。
阅读全文