Remove Empty Row in Table

Dear Community,

How to remove empty row in mandatory child table.

i had child doctype name SR Items

Table name : Select Services and field as table_lepd

whenever select doctype one empty row comes as default how to remove the empty row but set as mandatory.

I think, if the table is mandatory then this type of issue happens. you can remove it using the client script so search it in the forum.

frappe.ui.form.on('Service Request', {
    // Trigger this function when you need to remove empty rows
    delete_btn: function(frm, cdt, cdn) {
        let row = locals[cdt][cdn];

        // Check if the row is empty or contains invalid data
        if (!row.item_code || row.qty === 0 || row.rate === 0) {
            // Remove the row from the child table
            frm.get_field(cdt).grid.grid_rows[cdn].remove();
            
            // Optionally, refresh the form to reflect changes
            frm.refresh_field(cdt);
        }
    }
});

Even i tried that it not works

@Rizwaan Please try this code for remove empty row in new doctype

Example :

frappe.ui.form.on('Service Request', {
    setup: function(frm) {
        if (frm.is_new()) {
            frm.clear_table('your_table_fieldname');
        }
    }
});
2 Likes

Thanks a lot

It will be removed every time the page is first loaded.
Issue is, if the table has data, then it also removes the data. This logic is not right for the first row.

update your code:

frappe.ui.form.on('Service Request', {
    setup: function(frm) {
        if (frm.is_new()) {
            frm.clear_table('your_table_fieldname');
        }
    }
});

Thanks for response even i add as solution but one doubt if i save the document but refresh it show as Not saved

There is an issue occurring due to a refresh event running. so check the client script and update it according to the scenario.

Thank you