Enable or Disable a Child Table Field

Hi,
I have a child table where I want to enable/disable a field on the basis of a condition.
I tried to use this:

frappe.meta.get_docfield(this.doctype, fieldname, this.frm.docname).read_only=1

frappe.meta.get_docfield(this.doctype, “rate”, this.frm.docname).read_only=1

rate is the field which I want to disable.
What else I want to do in it?

Hello @ruchin78

You can use Depends On for that filed in doctype.

Based on child table filed you can write condition like this: eval:filedname == 1

Based on parent table filed you can write condition like this: eval:parent.fieldname == 1

I hope depends_upon works for making field hide/un-hide.

@ruchin78, do you need use this crazy sintax:

var field = frappe.utils.filter_dict(cur_frm.fields_dict["table_field_name"].grid.grid_rows_by_docnam[cdn].docfields, {'fieldname': "fieldname_here"})[0];
field.df.reqd = false;
field.refresh()

@rushabh_mehta, @anand a use case for this pull request 3 Silly small fixes for fields by MaxMorais · Pull Request #1217 · frappe/frappe · GitHub

1 Like

Hi @max
There is some error in the code:

var field = frappe.utils.filter_dict(cur_frm.fields_dict["table_field_name"].grid.grid_rows_by_docnam[cdn].docfields, {'fieldname': "fieldname_here")[0];
field.df.reqd = false;
field.refresh()

see

frappe.utils.filter_dict(

you opened a ( here and you closed that too here:

{'fieldname': "fieldname_here")

But the problem is {
You opened it here:

{'fieldname': "fieldname_here")

but you didn’t close it. I appreciate if you can correct the code.

Regards
Ruchin Sharma

@ruchin78, this is only a sample, if you are able of find the error, you are able to fix!

Anyway it is fixed!

I don’t know how to fix it that is why I asked you to do it. If I could do it by my own then I should not tell you about that.

Is fixed

Hi @max_morais_dmm
It didn’t work for me.

frappe.ui.form.on("Expense Claim Detail","km", function(frm, cdt, cdn) { 
var field = frappe.utils.filter_dict(
cur_frm.fields_dict["expense"].grid.grid_rows_by_docnam[cdn].docfields, 
{'claim_amount': "claim_amount"})[0];
field.df.reqd = true;
field.refresh();
});

What I really want to make the field read only based on a condition.

@ruchin78 use {"fieldname", "claim_amount"}

@max_morais_dmm
See my code below, it makes the field mandatory whereas I want the field should become read-only

frappe.ui.form.on("Expense Claim Detail","km", function(frm, cdt, cdn) { 
var field = frappe.utils.filter_dict(
cur_frm.fields_dict["expense"].grid.grid_rows_by_docnam[cdn].docfields, 
{"fieldname": "claim_amount"})[0];
field.df.reqd = true;
field.refresh();
});

@ruchin78 the just change the property, currenlty the example is ensuring that the field is reqd (Property of Required) just change this property for read_only

Hi @max_morais_dmm

See my below code, it didn’t work for me.

frappe.ui.form.on("Expense Claim Detail","km", function(frm, cdt, cdn) { 
var field = frappe.utils.filter_dict
(
cur_frm.fields_dict["expense"].grid.grid_rows_by_docnam[cdn].docfields,{"fieldname": "claim_amount"}
)[0];
field.df.read_only = true;
field.refresh();
});

try this

frappe.ui.form.on("Expense Claim Detail", "km", function(frm, cdt, cdn){
    frappe.utils.filter_dict(cur_frm.fields_dict["expense"].grid.grid_rows_by_docname[cdn].docfields, {"fieldname": "claim_amount"})[0].read_only = true;
    cur_frm.fields_dict["expense"].grid.grid_rows_by_docname[cdn].fields_dict["claim_amount"].refresh();
})
1 Like

@max_morais_dmm
Sorry to say but it also didn’t work.

@ruchin78, check the names and post the javascript error here!

Cause, “It dont work” dont help! It works to me!

@max_morais_dmm
Thanks it worked now, I didn’t checked the child table name it was expenses instead of expense.
But once it makes the field read only in the next row the field itself get hidden.

This is the error I am facing.