This is a migrated thread and some comments may be shown as answers.

Calculated fields in model of Datasource

15 Answers 2051 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 05 Jun 2012, 03:49 PM
Is it possible to have calculated fields in the datasource items? Kind of like the observable calculated fields? 

So for a simple example would be a Orders dataset that returns this in JSON:

- ItemDescription
- Qty
- Amt

But then I want to bind this to a data grid and have a column for total that is Qty * Amt and I want the grid to be editable in batch mode so that if the Qty changes, it updates the Total. What I am looking for is something way more complex as far as calculations and number columns involved but wanted to give a very simple example.

Could you do this basically with a collection of kendo observable objects where the total property looked something like this:

Total: function() { return Qty * Amt}

Or what is the best practice on stuff like this, I am sure I am not the only one running into this. I am trying to minimize defining my model for the Datasource and again for the View model in MVVM. I would like to do it just once to say what columns are coming across and fill the view model/ datasource.

15 Answers, 1 is accepted

Sort by
0
Dave
Top achievements
Rank 1
answered on 05 Jun 2012, 11:19 PM
I'm also very interested in this. I am getting data via XML and given the XPath-like syntax for specifying fields, I had thought I could accomplish this using XPath functions. Alas, it appears as though Kendo is not using a full XPath processor and so field specifications like "count(/Items)" don't work. An alternative (and a more powerful one at that) would be what's suggested here - a way to use functions for specifying field values. Is this possible?
0
Nigel
Top achievements
Rank 1
answered on 07 Jun 2012, 03:08 PM
I am doing something similar by defining additional fields in my model
model: {
fullname: function() {
return this.firstname + ' ' + this.lastname;
}
}
so fullname is my calculated field and firstname and lastname are field names that are automatically included in the model (they are in the jsondata)


you can then use this in a template (which is all that I've done) with something like ${fullname()}


Hope this helps
0
Iliana Dyankova
Telerik team
answered on 08 Jun 2012, 10:33 AM
Hi guys,

@Nigel, thank you for sharing your solution with the community.

Another approach is to update the model total field via the set method in the save event of the grid. For convenience, I created a jsFiddle example which illustrates this in action.

 

Kind regards,
Iliana Nikolova
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Jonathan
Top achievements
Rank 1
answered on 11 Jun 2012, 04:31 PM
Also is there a best practice for sharing Model structures for both the datasource and the MVVM observable object?

We have cases where we may be viewing 2 entities from a DS in different kendo windows, so the MVVM object needs Save methods, or calculated fields that don't interact with the other entity. The only examples I have seen on the demos basically have an observerable object with DS property and a SelectedEntity (I think it is a product in the example) and has the calculated fields on the main viewmodel. 

It is basically like I need a DS of viewmodels. Does that make sense?
0
Iliana Dyankova
Telerik team
answered on 14 Jun 2012, 06:39 AM
Hello Jonathan,

I am not sure if I understand you correctly. Could you please provide some code snippets or better yet a simple jsFiddle example, which illustrates your scenario? Thank you in advance.

On a side note, the dataSource contains kendo.data.ObservableObject instnaces.

Greetings,
Iliana Nikolova
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Henning
Top achievements
Rank 1
answered on 23 Jun 2012, 05:33 PM
Hi!

I tried to use the solution suggested by Nigel (see this adapted fiddle from Iliana). However, I was not able to get the calculated field working by creating a new method in the model. 

What am I missing or did I get the hint by Nigel wrong? 
0
axwack
Top achievements
Rank 1
answered on 25 Aug 2012, 01:15 PM
I'm having a very similar problem. For attributes with vanilla get/set I can send the values to the server but for values with dependent values or calculated fields, the values are null even though they are set. I have a feeling its because there is a function attached and the kendo library doesn;t handle it.

Is this the correct behavior or should I send the values to the server after they have been set by a dependent value?
0
Iliana Dyankova
Telerik team
answered on 30 Aug 2012, 07:07 AM
Hello Vincent,

I am not sure what causes the problem in your application - by design the updated values should be sent to the server. For convenience I updated the jsFiddle example.
 
Regards,
Iliana Nikolova
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
axwack
Top achievements
Rank 1
answered on 30 Aug 2012, 07:14 AM
What if its in a viewmodel?
0
Iliana Dyankova
Telerik team
answered on 04 Sep 2012, 07:42 AM
Hi Vincent,

When a viewModel is used the updated values should be sent to the server. In order to assist you best I would like to ask you to provide more details about your scenario or better yet send a small but runnable project. This way I would be able to investigate the case in details and advice you further. Thank you in advice your cooperation.
 
Regards,
Iliana Nikolova
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Oleksandr
Top achievements
Rank 1
answered on 22 Nov 2016, 02:17 PM

Hi Iliana,

I know this is an old thread, but isn't viewmodel intention to embed model logic and separate it from presentation layer? If so, why do you suggest to mix that up in declaration of grid (on save event manually update viewmodel's property).

I hope something changed in last 4 years and Kendo UI can provide some better solution for MVVM forms.

0
Stefan
Telerik team
answered on 25 Nov 2016, 02:26 PM
Hello Oleksandr,

If the desired result is to use a calculated field in the MVVM forms, I can suggest checking the following article for the Dependent Methods. The examples demonstrate how to use calculated fields in the MVVM form:

http://docs.telerik.com/kendo-ui/framework/mvvm/observableobject#dependent-methods

As for the approach demonstrated in the Grid, as the Grid is a complex widget and its implementation is different than the regular MVVM forms, using a calculated field is not supported.

Regards,
Stefan
Telerik by Progress
Kendo UI is ready for Visual Studio 2017 RC! Learn more.
0
Kevin
Top achievements
Rank 1
answered on 16 Mar 2017, 09:33 PM

I noticed that Iliana's fiddle no longer works (5 years later), so I fixed it. Here's the corrected fiddle with a grid containing a calculated column.

http://jsfiddle.net/SCJBz/1/

0
Kevin
Top achievements
Rank 1
answered on 16 Mar 2017, 09:37 PM
Sorry, wrong link. That should be http://jsfiddle.net/SCJBz/69/
0
Stefan
Telerik team
answered on 17 Mar 2017, 07:29 AM
Hello Kevin,

Thank you for updating the link and sharing it with us.

It is highly appreciated.

Regards,
Stefan
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data (charts) and form elements.
Tags
Data Source
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Dave
Top achievements
Rank 1
Nigel
Top achievements
Rank 1
Iliana Dyankova
Telerik team
Jonathan
Top achievements
Rank 1
Henning
Top achievements
Rank 1
axwack
Top achievements
Rank 1
Oleksandr
Top achievements
Rank 1
Stefan
Telerik team
Kevin
Top achievements
Rank 1
Share this question
or