数据请求一个数组,向数组里push新增内容
新增并不覆盖原有数据
假设请求了一个接口得到一个对象数组的形式
{[{},{},{}]}以下代码不会很全,有的需要自己设置初始值。
基本都有了的,如果有更好的见解或方法欢迎来电
我们自己设置一个常量来接收这个返回值,
const data = res.data.items
// items是一个返回的数组,数组中还是对象,其中有个值是数组,假设我们叫itemarr
此时假设表单上有个按钮是新增/编辑
我们拿到这一行的rows
请求新增或编辑接口,一般都会得到查询(val)的值
假设请求回来成功了
我们
handleEdit = async (keys, rows, type) => {
// 获取实时的要修改的数据
const res = await service.getOneUpdate(rows[0]);
if (res.success) {
const dataForm = {
...res.data
}
if (this.dataForm?.length > 0) {// 如果原来返回就有数据
const dataSource = successTableRef.dataSource; // 赋值
const selectedRowKeys = []; // 定义空的数组便于后续传值
const selectedRows = [];
this.dataForm.forEach((itemid) => { // 循环遍历数组的每一项
const row = dataSource.find((it) => it.id == itemid); // find()方法查找是否有一样的值。
if (row) {
selectedRowKeys.push(itemid);
selectedRows.push(row);
}
});
const selectionData = {
selectedRowKeys,
selectedRows,
length: selectedRowKeys.length
};
setTimeout(() => {
this.tableRef?.setSelectionData(selectionData);
}, 0);
}
};
}
}
然后 就是我们的保存事件
// 保存
save = async (selectionData?: any) => {
const item = selectionData.selectedRows.map(
({ sceneCode, id, sceneName }) => ({
sceneCode,
sceneId: id,
sceneName
})
);
let params = {
defineId: this.props?.match?.params?.id,
params: item
};
const res = await service.addScene(params);
if (res && res.success === true) {
this.setState({});
ElNotification({ type: 'success', message: res.msg });
const { store } = this.props
store.MultiTabMobx.closeCurrentToPath('/fin/purcSettlement/Buyer');
} else {
ElNotification({
type: 'error',
message: res.msg
});
}
};
跟上边没关系的
下边是对于超级没有逻辑要求的效果代码。
const dateitem = res.data.itemsdata 、、就是那个数组
let arr = []
val.forEach(item => {
console.log(item.date)
arr.push(item.date)
})
this.multipleSelection = arr
}