How to define dependent method / calculated field in model schema

6 posts, 0 answers
  1. Remco
    Remco avatar
    31 posts
    Member since:
    Sep 2012

    Posted 27 Sep 2012 Link to this post

    I have read how you can add a dependent method / calculated field to an ObservableObject (http://docs.kendoui.com/getting-started/framework/mvvm/observableobject), but I cannot find how you can define a dependent method / calculated field in the schema of a Model (http://docs.kendoui.com/api/framework/model). The instances of my Model are read from a remote kendo DataSource, so I need to define a dependent method / calculated field in the schema of the Model so that every instance of my Model automatically has this dependent method / calculated field. I've tried defining it as a field with the defaultValue set to the function and set nullable: true, but all the instances of my Model end up with null values in this field. Here's an example:

    var Category = kendo.data.Model.define({
        id: "Id",
        fields: {
            Id: { type: "number", editable: false, nullable: true},
            Title: { type: "string" },
            SortOrder: { type: "number" }
        }
    }
     
    var Product = kendo.data.Model.define({
        id: "Id",
        fields: {
            Id: { type: "number", editable: false, nullable: true},
            Title: { type: "string" },
            CategoryId: { type: "number" },
            Category: {},
            CategorySortOrder: { defaultValue: function() { return this.get("Category.SortOrder"); }, nullable: true
        }
    }

    so when I get an instance of my Product from my kendo DataSource its CategorySortOrder property is null. So, how to define dependent method / calculated field in model schema?
  2. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 02 Oct 2012 Link to this post

    Hello Remco,

    I am afraid that what you would like to achieve is not currently supported by the DataSource component. The DataSource is designed to work with flat data arrays and does not provide way for defining calculated fields.

    As an alternative approach, in order to display the Category.SortOrder you could use column template. For example:
    //columns
    { field: "Category", title: "something", template: "#= Category.SortOrder #" },

    Kendo templates can also execute JavaScript code or call JavaScript functions, which will allow you to perform calculations with the current row values.

    Regards,
    Alexander Valchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. Nathan
    Nathan avatar
    39 posts
    Member since:
    Sep 2011

    Posted 04 Jun 2013 Link to this post

    Is it possible to provide an example of the solution that relates to the question... that is, a modified version of the original code?
  5. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 05 Jun 2013 Link to this post

    Hello Nathan,

     We cannot provide a modified version of the original code because it is not supported. Could you please let us know what exactly are you trying to achieve? Isn't a simple column template sufficient?

    Regards,
    Atanas Korchev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. EB
    EB avatar
    3 posts
    Member since:
    Mar 2014

    Posted 08 May 2014 Link to this post

    I am also trying to archieve this.

    My use case is following: 
    Have data source which brigs a task list which has a timestaöp field. I would like to create a calculated field which will be short string format of this timestamp. İt will be used to group data source, and sort it. 

    İs this possible?
  7. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 13 May 2014 Link to this post

    Hello EB,

    In order to achieve this you should set the type of the field to "date" in the dataSource.schema.fields configuration. Please check this code sample:
    Once the field is set to "date" the dataSource will automatically parse timestamp string into JavaScript date object and you will be able to use kendo.toString and columns.template to format it according to your requirements. Similar approach is used in this demo page (check the Order Date column):

    Regards,
    Alexander Valchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top
Kendo UI is VS 2017 Ready