Add row to table dynamically

Hello, I am trying to add row to table in doctype dynamically using add_child method but no success.i am stuck in this task since day.can anyone help on this issue on how to add row to table dynamically based on data from another doctype.any help will be appreciated.

3 Likes

@Hardik_Mehta, can you share what you tried?

@max_morais_dmm I m trying to add first one row after that as per doc data more row should be add
frappe.ui.form.on(“Employee Timesheet”, {
onload: function(frm) {
frappe.call({
“method”: “attendance.attendance.doctype.employee_timesheet.employee_timesheet.get_employees”,
callback:function(r){
console.log(r.message);
frappe.msgprint("Javascript message "+r.message[0]);
var new_row = frm.add_child(“Timesheet child table”);
new_row.timesheet = ‘ABC’;
}
});
refresh_field(“Timesheet child table”)
}
});

@Hardik_Mehta, You need pass in “frm.add_child” the name of the field, and “Timesheet child table” dont sounds like a valid fieldname, usually it’s in lower case and with “_” underscores (eg: timesheet_details)

Check the right name of the field, in “Customize Form” or in the doctype “Timesheet”

@max_morais_dmm can you please explain which parameter required by add_child method.

@Hardik_Mehta, the fieldname is the value that should be passed!

For example, the “Sales Invoice” doctype, It already have 4 sub-tables, (“items” → “Sales Invoice Item”, “taxes” → “Sales Taxes and Charges”, “payments” → “Sales Invoice Payment”, “sales_team” → “Sales Team”)

So, if you need add a new product in the invoice, the sub-table is “Sales Invoice Item”, but in the body of “Sales Invoice” it’s named “items”, so, you will need the given script;

var child = cur_frm.add_child("items");
frappe.model.set_value(child.doctype, child.name, "item_code", "My Item Code")
cur_frm.refresh_field("items")

The child is in the “items” shortcut, but is stored into “Sales Invoice Item”!
Got me?

13 Likes

@max_morais_dmm thank you so much for your help.can u please help me on how to add data to child table automatically when data in some other doctype get saved.

Hey ,
How would you iterate through this list to see if the item is already present?
i searched a few options but cant exactly get how would you reference the child table to search for duplicate entries.
Any help is appreciated …
Thanks.

@Taher_Khalil easy way is

var items_qty = {};
frm.doc.items.forEach(function(row){
    if (!items_qty[row.item_code]) {
      items_qty[row.item_code] = 1
   } else {
      items_qty[row.item_code] += 1
   }
});
7 Likes

@max_morais_dmm
Thanks a lot ,
everything works but add_child function directly adds a child and value from the second row as first row is present by default.
Is there an inbuilt function like remove_child which i can set on_load to give me a empty table to begin with?

Thanks

@Taher_Khalil you can verify if the row exists and call frappe.remove_from_locals(doctype, docname)

But after that you need to reload the table with frm.refresh_field("table")

@max_morais_dmm
when i am doing frappe.remove_from_locals("items", "item_code") then

frm,refresh_field("items") in onload:function in sales_invoice.js …its not working.
what am i doing wrong?

Also is there any documentation or a cheat sheet highlighting all the inbuilt frappe functions like add_fetch, add_child and similar?

Thanks a ton for quick and helpful replies till now - you’re awesome :slight_smile: :+1:

@Taher_Khalil, the issue, is the event, you could not do that on onload event, cause it dont work, as the name of the event says, it is handled “when the form loads”, you need to pick the properly event, based on the user workflow, my sugestiong is make use of “validate”

@max_morais_dmm , its doesn’t seem to be working.
i just want the table to be blank at the beginning as soon as the the entire form loads like so

but where ever i put the code(tried a few places to declare on different workflows ) the table always comes with the first row and that results in the above situation.
kinda stuck :disappointed_relieved:

any help please

try this in onload:

if(frm.doc.__islocal) 
      frappe.model.clear_table(frm.doc, "items");
2 Likes

@JoEz
Its showing error in console :
ReferenceError: frm is not defined

@JoEz
it worked i changed it to cur_frm.doc and it worked like a charm!

Thanks a ton…

Also is there any documentation or a cheat sheet highlighting all the inbuilt frappe functions like these?

1 Like

Hello @max_morais_dmm
I created web form for the subscription plan doctype. I want the user not click on the ADD ROW button under the Plans Table. Also the data I am trying to send to the table is not getting entered. Is this the problem with ADD ROW Button or the the problem with my URL Format.Please look at the following screenshots.
Thank You.


I want that add row button to be auto clicked and only one row should be added.


Please help me. Thanks in Advance

I want the button ADD ROW to be auto clicked when I load my web form. I am not able to enter the data unless the button is clicked. Some One help me