When working with child tables in the Frappe Framework or ERPNext, some functionalities that work out of the box with other DocTypes that aren’t child tables may prove to be a pain. In this tutorial, I will quickly brush through the following:
- How to listen to the add-row event in a child table
- How to assign a value to a field in a child table on adding a new row
- How to assign a value to a field in a child table on changing another field in the same child table
LISTENING TO THE ADD-ROW EVENT
frappe.ui.form.on('Article Details', { form_render: function(frm,cdt,cdn) { let item = locals[cdt][cdn]; let articleId = Math.round(+new Date()/1000); item.article_id = articleId; frm.refresh_field('my_article'); }, });
LISTENING TO A CHANGE IN ANOTHER FIELD IN THE SAME CHILD TABLE
frappe.ui.form.on("Article Details", "article_name", function(frm, cdt, cdn) { let item = locals[cdt][cdn]; let articleId = Math.round(+new Date()/1000); item.article_id = articleId; frm.refresh_field('my_article'); }); Notice the presence of cdt and cdn in both functions . That means that child doctype and child docname are passed to function and hence you can know what row was modified and triggered. locals is a global array that contains all the local documents opened by the user item is the row that the user is working with