Calculated fields in model of Datasource

11 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    57 posts
    Member since:
    Jun 2012

    Posted 05 Jun 2012 Link to this post

    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.

  2. Dave
    Dave avatar
    9 posts
    Member since:
    Jul 2007

    Posted 05 Jun 2012 Link to this post

    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?

  3. Nigel
    Nigel avatar
    2 posts
    Member since:
    Mar 2012

    Posted 07 Jun 2012 Link to this post

    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

  4. Iliana Nikolova
    Admin
    Iliana Nikolova avatar
    1645 posts
    Member since:
    Sep 2012

    Posted 08 Jun 2012 Link to this post

    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!

  5. Jonathan
    Jonathan avatar
    57 posts
    Member since:
    Jun 2012

    Posted 11 Jun 2012 Link to this post

    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?

  6. Iliana Nikolova
    Admin
    Iliana Nikolova avatar
    1645 posts
    Member since:
    Sep 2012

    Posted 14 Jun 2012 Link to this post

    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!

  7. Henning
    Henning avatar
    3 posts
    Member since:
    Jul 2012

    Posted 23 Jun 2012 Link to this post

    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? 

  8. axwack
    axwack avatar
    25 posts
    Member since:
    Aug 2012

    Posted 25 Aug 2012 Link to this post

    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?

  9. Iliana Nikolova
    Admin
    Iliana Nikolova avatar
    1645 posts
    Member since:
    Sep 2012

    Posted 30 Aug 2012 Link to this post

    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!

  10. axwack
    axwack avatar
    25 posts
    Member since:
    Aug 2012

    Posted 30 Aug 2012 Link to this post

    What if its in a viewmodel?

  11. Iliana Nikolova
    Admin
    Iliana Nikolova avatar
    1645 posts
    Member since:
    Sep 2012

    Posted 04 Sep 2012 Link to this post

    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!

Back to Top