Running event whenever any field value changed in the child table

Hello;

What is the js method (which frappe.ui.form.on) that I have to use it to run the script whenever any field value in the child table changed?

For example: whenever the user select any value the fields: From, To, Category then I need to run script that will calculate the Hope Price. So what java method to be used?

One more question: and how I can know the row number of the changed value of the child table? For example, I need to know if the changed value was in row 1 or row 2 of the field “From”?

Regards
Bilal

Check out this thread:

Hello;
Thank you for the kindly reply.
I checked the link and it is talking about triggering the event in case of selecting (changing) certain field in the child table.
My question is: How to trigger the event in case of changing any field in the table?
I did not find answer in the link.
And I did not find a way to know if the changed value was in row # 1 or # 2 or # 3?
Regards
Bilal

Of course it was there. Let me copy and paste from the second post:

frappe.ui.form.on("Sales Order Item", "rate", function(frm, cdt, cdn) { // notice the presence of cdt and cdn
// that means that child doctype and child docname are passed to function and hence you can know what 
   // row was modified and triggered
    var item = locals[cdt][cdn]; // this is where the magic happens
    // locals is a global array that contains all the local documents opened by the user
    // item is the row that the user is working with
    // to what you need to do and update it back
    var result = a + b + c;
    item.result_field = result; // remember to refresh the field to see the changes in the UI
});
2 Likes

Thank you.
Inside this event, how can I use the cur_frm.add_fetch or he equivalent for it?
Regards
Bilal

cur_frm.add_fetch is working with grid, so no problem :slight_smile:

@yefritavarez

item.result_field = result; // remember to refresh the field to see the changes in the UI
});

How I can do refresh for field in the child table?

Why I need it? Because really I used the above script and the value appears in the field of the child table (and at the right position) just after adding the next row or doing save. So I am assuming maybe I have to refresh the field.

I tried the below script and until now it is not refreshing:
frm.refresh_field(“selected_category”); // selected category is the field in the child table that need to be refreshed.

The thing that let me think that the problem is refresh is:
When I do save, the updated value appears. So for sure it is refresh, but I am surprised why I am not able to refresh using frm.refresh

Below is the script that I am running and the snap shot for the child table:

frappe.ui.form.on(‘Taxi Hops’, ‘to’, function(frm,cdt,cdn) {
var item = locals[cdt][cdn];
item.selected_category = ‘C’;
refresh_field(“Taxi Hops”);
})

Thanks in advance.
Regards
Bilal

You need to refresh the childtable using the fieldname give to the same. So, if your childtable is called items then you would refresh it as follows refresh_field("items").

Also, you can leave it to the framework the refresh by using this function to set the value:
frappe.model.set_value(cdt, cdn, "child_fieldname", value); inside the function definition so that is has access to cdt & cdn

Hello;
I am fully thanks for you.

This worked with me. And I would to add something: that this method was able to change the field value in the row that the change happened and it did not change all the rows and this is the required :+1:

Also other method worked which is: cur_form.refresh().
Also refresh_field(''childtable") worked fine, but I have to place the name of the table in the parent document that is directed to the child doctype. For example, if the table name in the parent document is “hops” and the child doctype is “taxi hops” then I have to place the “hops” name.

The one that did not work with me is the refresh_field(“child_fieldname”), this did not work with me.
Regards
Bilal

how can i Trigger child table on before save event . i doesn’t want to trigger event on child table field