Sum total of filled rows in child table

Hi! I have a Doctype call “Viaticos” with a chil table call “Rendiciones”. I have a field in the Doctype “gastos_con_tarjeta” where I want to sum the column “total_pagado” of the child table. But, I want to sum just the rows of the child table that in another column call “tipo_de_gasto” have the value “Combustible”. The Custom Script I have sum all the rows, how can I make it?

frappe.ui.form.on(“Rendicion”, {
entrada_de_pago:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.tarjeta.forEach(function(d) { total += d.total_pagado; });
frm.set_value(“gastos_con_tarjeta”, total);
refresh_field(“gastos_con_tarjeta”);
},
tarjeta_remove:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.tarjeta.forEach(function(d) { total += d.total_pagado; });
frm.set_value(“gastos_con_tarjeta”, total);
refresh_field(“gastos_con_tarjeta”);
}
});

Instead of:

Check for the value of tipo_de_gasto property of d (the row object), before adding to total:

frm.doc.tarjeta.forEach(function(d) {
    if(d.tipo_de_gasto == "Combustible") {
         total += d.total_pagado;
    }
});
1 Like

Thank you very much!!! It works!!