Try to make field in child table read only based on condition using js

`frappe.ui.form.on(‘Sales Invoice Item’, {
qty: function(frm, cdt, cdn) {
frappe.msgprint(‘qty changed’);
var item = locals[cdt][cdn];

    if (item.qty === 5) {
        console.log(frm.fields_dict);
        frm.set_df_property('custom_nosquantity', 'read_only', 1);
        console.log('Setting custom_nosquantity to read-only for item:', item.item_code);
    }
}

}); ` why this code not working

Please check the reference:

2 Likes

Hi @MhmedRjb:

Note that you have to check each row at form refresh too. Otherwise, custom_nosquantity will be writable despite qty==5

Something like this

frappe.ui.form.on('Sales Invoice', {
    onload_post_render (frm){
        frm.fields_dict.items.grid.wrapper.on("focus", "div.frappe-control.form-group", function(e) {
            $.each(frm.doc.items,  function(i,  d) {
                if (d.qty === 5) {
                    frm.fields_dict['items'].grid.grid_rows_by_docname[d.name].toggle_editable('custom_nosquantity', false);
                }
            })
        })
    },

Happy saturday.

2 Likes

thx you guys that was really helpful ,for reference to others this the full code after extra help from Chat gpt

  1. make a field non editable /read only in child table based on value from other field in the same table
frappe.ui.form.on('Sales Invoice', {
    // Customer field change event handler
    onload_post_render: function(frm) {
        if (!frm.has_item_focus_handler) {
            frm.has_item_focus_handler = true;
            frm.fields_dict.items.grid.wrapper.on("focus", "div.frappe-control.form-group", function(e) {
                toggle_nosquantity_editability(frm);
            });
        }
        frm.trigger('refresh_items');
    },
    refresh: function(frm) {
        frm.trigger('refresh_items');
    },
    refresh_items: function(frm) {
        toggle_nosquantity_editability(frm);
    },
    items_add: function(frm) {
        toggle_nosquantity_editability(frm);
    },
    items_remove: function(frm) {
        toggle_nosquantity_editability(frm);
    }
});

function toggle_nosquantity_editability(frm) {
    $.each(frm.doc.items, function(i, d) {
        let grid_row = frm.fields_dict['items'].grid.grid_rows_by_docname[d.name];
        if (d.qty === 5) {
            grid_row.toggle_editable('custom_nosquantity', false);
        } else {
            grid_row.toggle_editable('custom_nosquantity', true);
        }
    });
}
  1. make a field non editable /read only in child table based on value from other docktype
var customDualQuantities = {};
frappe.ui.form.on('Sales Invoice', {
    onload_post_render: function(frm) {
        if (!frm.has_item_focus_handler) {
            frm.has_item_focus_handler = true;
            frm.fields_dict.items.grid.wrapper.on("focus", "div.frappe-control.form-group", function(e) {
                toggle_nosquantity_editability(frm);
            });
        }
    frm.trigger('refresh_items');
    },
    refresh: function(frm) {
        frm.trigger('refresh_items');
    },
    refresh_items: function(frm) {
        toggle_nosquantity_editability(frm);
    },
    items_add: function(frm) {
        toggle_nosquantity_editability(frm);
    },
    items_remove: function(frm) {
        toggle_nosquantity_editability(frm);
    }
});

frappe.ui.form.on("Sales Invoice Item", "item_code", function(frm, cdt, cdn) {
    var d = locals[cdt][cdn];
    console.log("Item: " + d.item_code);
    frappe.db.get_value("Item", {"name": d.item_code}, "custom_dual_quantity", function(value) {
        console.log("Item: " + d.item_code + ", dual quantity: " + value.custom_dual_quantity);
        customDualQuantities[d.name] = value.custom_dual_quantity;
        toggle_nosquantity_editability(frm);
    });
});

function toggle_nosquantity_editability(frm) {
    $.each(frm.doc.items, function(i, d) {
        let grid_row = frm.fields_dict['items'].grid.grid_rows_by_docname[d.name];
        if (customDualQuantities[d.name] === 0) {
            grid_row.toggle_editable('custom_nosquantity', false);
        } else {
            d.custom_nosquantity = customDualQuantities[d.name];
            grid_row.toggle_editable('custom_nosquantity', true);
        }
    });
}

custom_nosquantity is a field in the child table
custom_dual_quantity is a field in item docktype