fix relation

This commit is contained in:
Rizky 2024-06-26 21:21:41 -07:00
parent 37cfd190d1
commit c742e27887
4 changed files with 34 additions and 15 deletions

View File

@ -110,6 +110,7 @@ export const newField = async (
table: arg.relation.to.table,
select: res.select,
pks: {},
type: arg.type === "has-many" ? "typeahead" : "dropdown",
});
if (["has-one"].includes(arg.type)) {
const rel__gen_fields = JSON.stringify(
@ -171,7 +172,10 @@ export const newField = async (
result.on_load = `() => { return []; }`;
}
let child: any = { childs: [] };
const relation = arg.relation?.fields.filter((e) => get(e, "name") !== opt.parent_table) || [];
const relation =
arg.relation?.fields.filter(
(e) => get(e, "name") !== opt.parent_table
) || [];
let rel__gen_fields: any = JSON.stringify(
relation.map((e) => {
const v = (e as any).value;

View File

@ -17,11 +17,13 @@ export const genRelMany = (prop: {
if (master) {
const select = generateSelect(master.relation.fields);
console.log(master);
result.on_load = on_load_rel({
pk: select.pk,
table: master.name,
select: select.select,
pks: {},
type: "typeahead",
});
const pk_master = master.relation.fields.find((e: any) => get(e, "is_pk"));
const get_value = `\

View File

@ -5,7 +5,7 @@ export const on_load_rel = ({
table,
select,
pks,
type
type,
}: {
pk: string;
table: string;
@ -25,6 +25,11 @@ export const on_load_rel = ({
}
}
const skip_select =
!isEmptyString(type) &&
["checkbox", "typeahead", "button"].includes(type as any);
console.log(skip_select, type);
return `\
async (arg: {
reload: () => Promise<void>;
@ -38,10 +43,13 @@ async (arg: {
}
return new Promise(async (done) => {
${!isEmptyString(type) && ["checkbox", "typeahead", "button"].includes(type as any) ? `` : `const fields = parseGenField(rel__gen_fields);
const res = generateSelect(fields);`}
${
skip_select
? ``
: `
const fields = parseGenField(rel__gen_fields);
const res = generateSelect(fields);
const is_tree =
typeof rel__feature !== "undefined" &&
Array.isArray(rel__feature) &&
@ -49,21 +57,28 @@ async (arg: {
if (is_tree && typeof rel__id_parent === "string" && rel__id_parent) {
res.select[rel__id_parent] = true;
}`
}
const items = await db.${table}.findMany({
${!isEmptyString(type) && ["checkbox", "typeahead", "button"].includes(type as any) ? `` : `select: {
...${JSON.stringify(select)},
...(res?.select || {})
},`}
let items = await db.${table}.findMany({
select: {
...${JSON.stringify(select)}
${skip_select ? `` : `,...(res?.select || {})`}
},
orderBy: arg.orderBy || {
${pk}: "desc"
},
...arg.paging,
});
${
skip_select
? ""
: `\
if (is_tree && typeof rel__id_parent === "string" && rel__id_parent) {
items = sortTree(items, rel__id_parent, "${pk}");
}`
}
if(items.length){
@ -79,9 +94,10 @@ async (arg: {
}
let blank: any = undefined;
if (ext__required !== "y") {
if ((ext__required as any) !== "y" && (sub_type as any) === 'dropdown') {
blank = { value: undefined, label: "", data: {} };
}
done(
[
blank,

View File

@ -1,3 +0,0 @@
.rdg-dark {
--rdg-color-scheme: light !important;
}