Using a Kendo grid that is in edit mode, how do you reactively compute the value of column B based on what is typed in column A?
E.g. column B = column A x 2
I enter 1 in column A; column B immediately changes to 2.
I enter 16 in column A; column B immediately changes to 32.
2 Answers, 1 is accepted
Hi Scott,
Reactively computing the value of one field, based on the changes of another, would require some custom logic.
Please take a look at this demo I've prepared:
https://stackblitz.com/edit/angular-43qwtz-jycubz?file=app%2Fapp.component.ts
The developer would need to subscribe to the valueChanges observable for the form control, whose changes trigger the update of another dataItem field.
In the example above I am executing the custom logic in the cellClose event handler. Inside, I am listening for UnitPrice changes and then automatically update the corresponding Total Value based on that. In terms of performance optimisation we also need to unsubscribe from the valueChanges observable too.
I hope this helps.
Regards,
Silviya
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
Thanks a lot, Silviya.
I had to tweak a couple of things because there are multiple input columns involved, but it works perfectly.
Sincerely,
Scott Pearson
Silviya,
Per your solution in the link above, I gather that the computed column MUST be read-only and also NOT part of the FormGroup. Is this true?
What if I wanted to make another column computation based on a column that was previously computed?
See code below.
Hi, Scott,
Thank you for the code snippet.
Indeed the StackBlitz example was with 'Total Value' column that is not editable but this is not mandatory. It is up to the developer to decide if this would be necessary or not in the specific implementation.
Usually it is readonly because otherwise there might be some cases where the end user enters as Total Value 100 - but the values for price and units stay at 10 and 5 (100 != 50). This looks inconsistent. Even if a validation is added that the Total Value has to be 50 - there is this other scenario to consider: If the TotalValue is 20, is it because the price is 4 and units 5 or is the price 2 and units 10.
For your convenience, I modified the example so now the 'Total Value' column is editable. Please take a look:
https://stackblitz.com/edit/angular-43qwtz-jycubz?file=app%2Fapp.component.ts
What is the difference: The column used to have an editable option set to false. Besides that, the TotalValue was not part of the FormGroup, as you already noted. Both those things were leading to the column being in readonly state.
As for computing a cell's value based on another computed cell - that can be done by reusing the same approach with subscribing to valueChanges - just as you have done. I only added setValue to the control because when we click the cells the form is recreated (this.createFormGroup(dataItem) is called) and in this case with 2 dynamically calculated cells - I had to modify the code to accommodate that.
I hope this proves helpful.
Regards,
Silviya
Progress Telerik