Add rows with values in child table

Hello,

i need to add rows in child table with values using js,

i tried

frappe.ui.form.on('Batch Packing', {
	refresh(frm) {
    var a = frappe.model.add_child(cur_frm.doc, "Batch Packing", "batch_items");
    a.item_code = item_a;
    a.materials = item_name_a;
    a.unit = uom;
    refresh_field("batch_items");

 }
})

but its not saving, when i refresh the page its discard the old row and insert new one.
also giving me error of pymysql.err.InternalError: (1054, "Unknown column 'batch_no' in 'field list'")
when creating new doc.

NOTE : batch_no is the first field of parent doctype. when i change the first field to another it throws the same error with first field name.

3 Likes

Anybody?

@shahid,
To add Child via Code,

cur_frm.add_child("project_item_list"); // childTable Field Name

in my it is project_item_list

and how to fill rows in child table?

@shahid

var childTable = cur_frm.add_child("project_item_list");
childTable.fieldname="Text"

cur_frm.refresh_fields("project_item_list");
3 Likes

Thanks. :+1:

I am doing the same thing above described but getting error.
i.e.
Cannot read property ‘options’ of undefined

please help.

Share your code.

It is resolved now,can you please help me in How to build HTMl table with a loop by using client side custom script??

You may refer to this page How to create a simple HTML table, and load the content from the other doctype? - #2 by max_morais_dmm

frappe.ui.form.on('Target Doctype', 'refresh', function(frm, cdt, cdn){
   frappe.call({
     'method': 'frappe.client.get_list',
     'args': {
       'doctype': 'Source DocType',
       'columns': ['*']
       'filters': [['Source DocType', 'link_reference', '=', frm.doc.name]]
     },
     'callback': function(res){
         var template = "<table><tbody>{% for (var row in rows) { %}<tr>{% for (var col in rows[row]) { %}<td>rows[row][col]</td>{% } %}</tr>{% } %}</tbody></table>",
        frm.set_df_property('html_fieldname', 'options', frappe.render(template, {rows: res.message});
        frm.refresh_field('html_fieldname');
     }
   })
});
1 Like