From 01511cd15a695eba82f47100400bc3f3c76debce Mon Sep 17 00:00:00 2001 From: EnvBsh Date: Mon, 4 Aug 2025 16:40:38 +0200 Subject: [PATCH] import from CSV Signed-off-by: Alex Piatakov --- .../schema-form-model/field-form.ts | 51 ++++++++- .../schema-form/schema-form.component.html | 102 +++++++++++------- .../schema-form/schema-form.component.scss | 20 +++- .../schema-form/schema-form.component.ts | 52 +++++++++ 4 files changed, 184 insertions(+), 41 deletions(-) diff --git a/frontend/src/app/modules/schema-engine/schema-form-model/field-form.ts b/frontend/src/app/modules/schema-engine/schema-form-model/field-form.ts index e1d4fc86b3..e3b726aa1b 100644 --- a/frontend/src/app/modules/schema-engine/schema-form-model/field-form.ts +++ b/frontend/src/app/modules/schema-engine/schema-form-model/field-form.ts @@ -26,6 +26,7 @@ export interface IFieldControl; visibility: boolean; + csvEnabled: boolean; } export interface IFieldIndexControl { @@ -643,7 +644,8 @@ export class FieldForm { open: this.lvl === 0, subject: new Subject(), visibility: true, - model: null + model: null, + csvEnabled: false }; item.visibility = !item.hide && !item.hidden && !item.autocalculate; item.preset = field.default; @@ -686,6 +688,7 @@ export class FieldForm { if (field.isArray && !field.isRef) { item.control = this.createArrayControl(); item.list = []; + item.csvEnabled = true; if (field.remoteLink) { item.fileUploading = true; } @@ -715,6 +718,15 @@ export class FieldForm { item.control = this.createArrayControl(); item.list = []; item.fields = field.fields; + item.csvEnabled = true; + if(item.fields) { + for (const fld of item.fields) { + if(fld.isRef) { + item.csvEnabled = false; + } + } + } + if (item.preset && item.preset.length) { for (let index = 0; index < item.preset.length; index++) { const preset = item.preset[index]; @@ -887,6 +899,43 @@ export class FieldForm { } } + public setFromCsv(control: IFieldControl, rows: Record[]) { + const cFieldName = control.field.name; + + const values = rows.reduce((acc, row) => { + const raw = row[cFieldName]; + const split = raw?.includes(';') + ? raw.split(';').map(v => v.trim().replace(/^\(/, '').replace(/\)$/, '')) + : [raw?.trim()]; + return acc.concat(split); + }, []); + + if(control.isArray) { + //remove items + control.list?.forEach(controlListItem => this.removeItem(control, controlListItem)); + //add items + values.forEach(() => this.addItem(control)); + } + + //fill + if(!control.isRef && control.isArray) { + for(let i = 0; i< values.length; i++) { + control.list && control.list[i].control.setValue(values[i]); + } + } else if(control.isRef && control.isArray) { + if(control.list) { + for(let i = 0; i < control.list.length; i++) { + const listItem = control.list[i]; + listItem.model.controls.forEach((ctr: any) => { + listItem.model.setFromCsv(ctr, [rows[i]]); + }) + } + } + } else if(!control.isArray && !control.isRef) { + control.control.setValue(values[0]); + } + } + public removeGroup(item: IFieldControl) { item.control = null; item.model?.destroy(); diff --git a/frontend/src/app/modules/schema-engine/schema-form/schema-form.component.html b/frontend/src/app/modules/schema-engine/schema-form/schema-form.component.html index dafff43d0b..a6eb4dd035 100644 --- a/frontend/src/app/modules/schema-engine/schema-form/schema-form.component.html +++ b/frontend/src/app/modules/schema-engine/schema-form/schema-form.component.html @@ -499,45 +499,59 @@
-
-
- {{ i }}/{{ formModel.controls.length - 1 }} -
-
- * +
+
- {{ item.description }} + {{ i }}/{{ formModel.controls.length - 1 }} +
+
+ * + {{ item.description }} +
+
+
+ {{ i + 1 }} +
-
-
- {{ i + 1 }} +
+ + Quickly prefill the data using a CSV file + +
@@ -866,7 +880,7 @@ > -
+
+ + Quickly prefill the data using a CSV file + + +
- +