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

field default value from anonymous function

4 Answers 703 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Cagatay
Top achievements
Rank 1
Cagatay asked on 26 Feb 2012, 09:34 PM
Hello,

I've tried

.
.
.
            schema: {
                type: "json",
                model: {
                    id: "id",
                    fields: {
                        c_pmchart: {type: "string", validation: { required: true }},
                        defi: { type: "string", validation: { required: false }, defaultValue: function () {return 'test value';} },
.
.
.

to set a field's defaultValue via an anonymous function but it di not work. Any ideas how to set deafultValues by a function  ?

TIA

4 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 27 Feb 2012, 09:10 AM
Hello Cagatay,

I am afraid that defaultValue should be value, not a function. It is not supported to have the default field value as a function.

Regards,
Nikolay Rusev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Robert
Top achievements
Rank 1
answered on 16 Jun 2014, 10:02 PM
A Telerik employee posted this solution elsewhere:

http://trykendoui.telerik.com/atOj/3

The gist is here, where the code sets one of the values equal to the currently-specified filter in that grid column:
defaultValue: function(e) {
  if(typeof this.CategoryID === "function") {
    var grid = $("#grid").data("kendoGrid");
    var ds = grid.dataSource;
    var filter = ds.filter();
 
    if(filter && filter.filters[0].field === "CategoryID") {
        this.set("CategoryID", filter.filters[0].value);
    } else {
        this.CategoryID = 1;
    }
  }
}

Hope this helps!
0
Tim
Top achievements
Rank 1
answered on 08 Aug 2016, 02:47 PM
Are you defining the datasource in the scope of the grid options or linking it?

I ask because when I try this, e is undefined.  I'm working with a dataSource that's defined outside of the grid.
0
Nikolay Rusev
Telerik team
answered on 11 Aug 2016, 06:50 AM

Hello Tim,

It doesn't matter how DataSource is defined the defaultValue function is called without arguments, thus e in this context will always be undefined. As you see in the code snippet it is not used anywhere in the function body.

Regards,
Nikolay Rusev
Telerik by Progress
 
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
 
Tags
Data Source
Asked by
Cagatay
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Robert
Top achievements
Rank 1
Tim
Top achievements
Rank 1
Share this question
or