dataManager 的设计思路与数据库一致,不涉及数据的顺序问题。
可以参考下面的代码,在插入行后,回填唯一主键。
- productTable = dataManager.addTable("product", {
- batch: true,
- remote: {
- read: {
- url: 'http://localhost:3000/getAll'
- },
- batch: function (changes) {
- console.log('修改的数据', changes)
- let newChanges = changes.map(item => {
- if (item.type == 'insert') {
- item.dataItem['采购订单号'] = `PX2301${String(item.sourceIndex + 1).padStart(4, '0')}`;
- }
- return item;
- })
- console.log('修改的数据', newChanges)
- return new Promise((resolve, reject) => {
- fetch("http://localhost:3000/order/batchUpdate", {
- method: "post",
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify(newChanges),
- }).then((response) => response.json())
- .then((data) => resolve(data))
- .then(() => designer.refresh());
- });
- }
- }
- })
复制代码
后端代码:
- order.get("/getAll", (req, res) => {
- res.json(fakeData);
- });
- order.post("/order/batchUpdate", (req, res) => {
- let obj = []
- const resultArray = req.body
- console.log(resultArray)
- resultArray.forEach((item, index) => {
- obj[index] = {
- "succeed": true
- }
- if (item.type === 'update') {
- let _index = item.sourceIndex
- fakeData[_index] = item.dataItem
- } else if (item.type === 'insert') {
- fakeData.push(item.dataItem)
- obj[index].data = item.dataItem
- } else if (item.type === 'delete') {
- let _index = item.sourceIndex
- fakeData.splice(_index, 1)
- }
- })
- console.log("obj", obj)
- res.json(obj);
- });
复制代码
|
|