How to delete a particular row from a child table?

Hi Everyone,

Is there any way that I can delete a particular row which matches with a particular condition?

Regards
Ruchin Sharma

1 Like

Hi @ruchin78 please refer this code snippet from item.py

to_remove = []
        for d in self.get("uoms"):
            if d.conversion_factor == 1 and d.uom != self.stock_uom:
                to_remove.append(d)
        [self.remove(d) for d in to_remove]

Regards,
Ravindra Lakal
New Indictrans Technologies PVT LTD

2 Likes

@ravindra_l
Thanks will try this.

1 Like

@ravindra_l
I have tried this code by doing some changes as per requirement but it is not working at all. I appreciate if you can help me what is wrong in this code?

@frappe.whitelist()
def del_items_from_product_pvvideo(item_code, qty, par="PV/VIDEO - BMT",par_qty=2):
    frappe.msgprint("Test")
    to_remove= []
    for d in frappe.db.sql("""SELECT D.item_code as item_code,D.item_name as item_name,D.description as description,sum(D.qty)*%s as qty FROM `tabSales Order Product Mapping Master` M,`tabSales Order Product Mapping Detail` D WHERE D.parent=M.name AND M.product_type=%s GROUP BY D.item_code,D.item_code,D.description""",(par_qty,par),as_dict=1):
    	if d.item_code == item_code and d.qty == qty:
		to_remove.append(d)

    [self.remove(d) for d in to_remove]

Regards
Ruchin Sharma

Hey @ruchin78,

You can also try this.

var tbl = doc.child_table || [];
for(var i = 0; i < tbl.length; i++)
{
if(tbl[i].field_name == ‘’)
{
cur_frm.get_field(“child_table”).grid.grid_rows[i].remove()
}
}
cur_frm.refresh()

4 Likes

Hello,

@ruchin78, @ravindra_l: I think that the last line in the script should have been:

self.remove(d) for d in to_remove

instead of:

[self.remove(d) for d in to_remove]

If the square brackets are still there, they will generate a list whose items will be None, like this: [None, None, None, None, None] and it will not be assigned to any variable.

Anyway, if you leave the brackets they will do no harm.
Note (@ravindra_l): this is not why the script is not working for you. It’s just to point out a minor improvement in the code.

2 Likes

@Nishant_Jariwala
The code you shared is working perfect except one issue.

Say for example I have 5 rows in my child table and above 3 records match with my condition.
Now, what happens when it deletes the first record (at index 0), then the second record becomes the first records which is again at index 0, whereas value of the variable has increased.

@sorin.negulescu thanks for your valuable input but it didn’t work.

Regards
Ruchin Sharma

Here is my shot at it. I strongly believe that the solution is to parse the array from the last to the first element. In this way, the array elements that get re-indexed will not affect the behavior:

var tbl = doc.child_table || [];
var i = tbl.length;
while (i--)
{
    if(tbl[i].field_name == '')
    {
        cur_frm.get_field("child_table").grid.grid_rows[i].remove();
    }
}
cur_frm.refresh();

Give it a try :wink:

14 Likes

@sorin.negulescu
It worked thanks a lot.

You’re welcome! But I just adapted the code a bit. @Nishant_Jariwala is the one who gave the hard part of solution. :slight_smile: