Child tables and *_on_form_rendered trigger

I have a field on a child table whose readonly state I control with a checkbox. I did this to provide molly-guard like functionality; the field value is set by the application elsewhere and readonly by default, if the “I know what I’m doing!” box is checked it becomes editable via an event listener on the checkbox from the parent doc script. Since the child table form is created/destroyed often, I set the initial appearance of the field via the *_on_form_rendered trigger I found in grid.js

example parent_doc.js

override_the_field: function(doc, cdt, cdn) {
        var me = this;    
        var item = frappe.get_doc(cdt, cdn);
        this.frm.fields_dict[me.fname].grid.grid_rows_by_docname[cdn].fields_dict["the_field"].df.read_only = !Boolean(item.override_the_field);

        me.frm.fields_dict[me.fname].grid.refresh_row(cdn);
    },

fname_on_form_rendered: function(doc, cdt, cdn) {
        var me = this;
        item = me.frm.fields_dict[me.fname].grid.open_grid_row.doc;
        this.override_the_field(item, item.doctype, item.name);
    },

So, reason for this topic:

  1. To share this idea, if anyone was trying something similar, since working with child tables seems to be trickier :smile:
  2. To ask: am I going about this the right away? I don’t like that deep reference I had to write to get at the form field. Is there some function / jQuery selector I missed that let’s me get right to the field I’m looking for? Am I abusing what should be considered internal data on grid.js?
4 Likes

The solution looks okay.

-Anand

@jvermette,
Is this control by check box?. when check box (enable/tick), the read only field become editable right?.

I want similarly to yours idea. My idea is:

i have child doctype :Plan Goal and
parent doctype is Work Plan.

in Child doctype ‘Plan Goal’ have field called accomplished and checkbox. So i want to control accomplished field by checkbox. When enable(tick) checkbox, accomplished field should become read only and when checkbox disable(untick) accomplished field should editable.