update +++

This commit is contained in:
lb 2023-04-03 11:35:14 +08:00
parent 13753f0c6c
commit 2afdb1d23d
2 changed files with 80 additions and 26 deletions

View File

@ -252,19 +252,39 @@ export default {
}; };
}, },
mounted() { mounted() {
/** 临时保存所有发出的请求 */
const promiseHistory = {};
/** 处理 injectTo 选项 */ /** 处理 injectTo 选项 */
this.configs.form.rows.forEach((row) => { this.configs.form.rows.forEach((row) => {
row.forEach((col) => { row.forEach((col) => {
if ("injectTo" in col && Array.isArray(col.injectTo)) { if (col.fetchData && typeof col.fetchData === "function" && !col.hasPrev) {
// -
promiseHistory[col.prop] = col.fetchData().then(({ data: res }) => {
if (res.code === 0) {
if ("list" in res.data) {
// options
this.$set(
col,
"options",
res.data.list.map((i) => ({
label: col.optionLabel ? i[col.optionLabel] : i.name,
value: col.optionValue ? i[col.optionValue] : i.id,
}))
);
//
if ("injectTo" in col) {
//
this.cachedList[col.prop] = res.data.list;
//
const valueProp = "optionValue" in col ? col.optionValue : "id"; const valueProp = "optionValue" in col ? col.optionValue : "id";
const unwatch = this.$watch( const unwatch = this.$watch(
() => this.dataForm[col.prop], () => this.dataForm[col.prop],
(newVal) => { (val) => {
const chosenObject = this.cachedList[col.prop].find((i) => i[valueProp] === newVal); const chosenObject = this.cachedList[col.prop].find((i) => i[valueProp] === val);
if (chosenObject) { if (chosenObject) {
// //
col.injectTo.map((item) => { col.injectTo.map((item) => {
console.log("[setting ---> ]:", item[1], "to", `dataForm[${item[0]}]`);
this.$set(this.dataForm, item[0], chosenObject[item[1]]); this.$set(this.dataForm, item[0], chosenObject[item[1]]);
this.$forceUpdate(); this.$forceUpdate();
}); });
@ -279,7 +299,40 @@ export default {
this.watchList.push(unwatch); this.watchList.push(unwatch);
} }
if ("toggleFetchData" in col) { if ("autoUpdateProp" in col) {
this.$set(savedDatalist, col.prop, res.data.list);
}
// end branch
} else if (Array.isArray(res.data)) {
//
if ("injectTo" in col) {
//
this.cachedList[col.prop] = res.data;
}
this.$set(
col,
"options",
res.data.map((i) => ({
label: col.optionLabel ? i[col.optionLabel] : i.name,
value: col.optionValue ? i[col.optionValue] : i.id,
}))
);
if ("autoUpdateProp" in col) {
this.$set(savedDatalist, col.prop, res.data);
}
}
} else {
col.options.splice(0);
}
});
}
if (col.fetchData && typeof col.fetchData === "function" && col.hasPrev) {
// -
// 1. fullfilled
// 2. fetchData
const unwatch = this.$watch( const unwatch = this.$watch(
() => this.dataForm[col.toggleFetchData], () => this.dataForm[col.toggleFetchData],
(val) => { (val) => {
@ -319,11 +372,9 @@ export default {
this.$set(savedDatalist, col.prop, res.data); this.$set(savedDatalist, col.prop, res.data);
} }
} }
// col.options = res.data.list;
} else { } else {
col.options.splice(0); col.options.splice(0);
} }
// dataForm[col.prop] = col.default ?? null; // not perfect!
}); });
}, },
{ {
@ -332,6 +383,8 @@ export default {
); );
this.watchList.push(unwatch); this.watchList.push(unwatch);
} }
/// old part ====================
}); });
}); });
}, },

View File

@ -150,7 +150,8 @@ export default function () {
prop: "orderId", prop: "orderId",
options: [], options: [],
optionLabel: 'orderCode', optionLabel: 'orderCode',
toggleFetchData: 'carId', // 当 carId 改变的时候,也会 fetchData // toggleFetchData: 'carId', // 当 carId 改变的时候,也会 fetchData
hasPrev: 'carId', // 当 carId 改变的时候,也会 fetchData
// 需要保存当前状态 // 需要保存当前状态
needCache: true, needCache: true,
fetchDataParam: { search: 'carId', get: 'id' }, // 伴随着 toggleFetchData 出现 fetchDataParam: { search: 'carId', get: 'id' }, // 伴随着 toggleFetchData 出现