diff --git a/frontend/src/components/Controls/Grid.vue b/frontend/src/components/Controls/Grid.vue index 1a0c082c..30ce62c6 100644 --- a/frontend/src/components/Controls/Grid.vue +++ b/frontend/src/components/Controls/Grid.vue @@ -327,9 +327,15 @@ const props = defineProps({ type: String, required: true, }, + parentFieldname: { + type: String, + required: true, + }, }) const triggerOnChange = inject('triggerOnChange') +const triggerOnRowAdd = inject('triggerOnRowAdd') +const triggerOnRowRemove = inject('triggerOnRowRemove') const { getGridViewSettings, @@ -419,11 +425,16 @@ const addRow = () => { newRow['__islocal'] = true newRow['idx'] = rows.value.length + 1 newRow['doctype'] = props.doctype + newRow['parentfield'] = props.parentFieldname + newRow['parenttype'] = props.parentDoctype rows.value.push(newRow) + triggerOnRowAdd(newRow) } const deleteRows = () => { rows.value = rows.value.filter((row) => !selectedRows.has(row.name)) + triggerOnRowRemove(selectedRows, rows.value) + showRowList.value.pop() selectedRows.clear() } diff --git a/frontend/src/components/FieldLayout/Field.vue b/frontend/src/components/FieldLayout/Field.vue index 3c0fecbc..7c4e7b34 100644 --- a/frontend/src/components/FieldLayout/Field.vue +++ b/frontend/src/components/FieldLayout/Field.vue @@ -26,6 +26,7 @@ v-model="data[field.fieldname]" :doctype="field.options" :parentDoctype="doctype" + :parentFieldname="field.fieldname" /> diff --git a/frontend/src/data/document.js b/frontend/src/data/document.js index 25fb9d3d..6713cf6f 100644 --- a/frontend/src/data/document.js +++ b/frontend/src/data/document.js @@ -83,6 +83,36 @@ export function useDocument(doctype, docname) { return await c[fieldname]?.() } + async function triggerOnRowAdd(row) { + const dt = row?.doctype ? row.doctype : doctype + const c = getController(dt) + if (!c) return + + c.currentRowIdx = row.idx + c.value = row + + return await c[row.parentfield + '_add']?.() + } + + async function triggerOnRowRemove(selectedRows, rows) { + if (!selectedRows) return + const dt = rows[0]?.doctype ? rows[0].doctype : doctype + const c = getController(dt) + if (!c) return + + if (selectedRows.size === 1) { + const selectedRow = Array.from(selectedRows)[0] + c.currentRowIdx = rows.find((r) => r.name === selectedRow).idx + } else { + delete c.currentRowIdx + } + + c.selectedRows = Array.from(selectedRows) + c.rows = rows + + return await c[rows[0].parentfield + '_remove']?.() + } + function getOldValue(fieldname, row) { if (!documentsCache[doctype][docname]) return '' @@ -103,6 +133,8 @@ export function useDocument(doctype, docname) { actions: computed(() => getActions()), getOldValue, triggerOnChange, + triggerOnRowAdd, + triggerOnRowRemove, triggerOnRefresh, setupFormScript, } diff --git a/frontend/src/data/script.js b/frontend/src/data/script.js index b632b5b2..3790daec 100644 --- a/frontend/src/data/script.js +++ b/frontend/src/data/script.js @@ -163,8 +163,6 @@ export function getScript(doctype, view = 'Form') { } row.parent = row.parent || data.name - row.parentype = row.parenttype || data.doctype - row.parentfield = row.parentfield || parentField return createDocProxy(row, instance) }