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

Dynamic default values

6 Answers 2977 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Staffan
Top achievements
Rank 1
Staffan asked on 29 Jan 2013, 11:48 AM
Hi!

Is there any way to add dynamic default values? When I push the "Add row" button a new row with a new default value (max  value + 1) for count column should be added.

Example:

.ToolBar(toolbar => {
        toolbar.Create().Text("Add row");
    })
...
 .Model(model => {
            model.Id(n => n.ID);
            model.Field(c => c.count).DefaultValue(<this value needs to be max value + 1>);
            model.Field(c => c.count).Editable(false);
        })

I have tried to adding an event on .Events( e => e.Change("MyFunction"))  but the row adding is done after MyFunction event is fired.

/Malte

6 Answers, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 31 Jan 2013, 06:47 AM
Hi Staffan,

 

Current approach to use the edit event is valid and you can use it to change the default value for given field in the following way:

function onEdit(e) {
         
    //execute your custom logic to get the max value
    var maxValue = 5;
 
    //check if record is new
    if (e.model.isNew()) {
        //set the required field value
        e.model.set("OrderCount", maxValue + 1);
    }
}
Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Richard van Diggelen
Top achievements
Rank 1
answered on 22 Nov 2013, 03:32 AM
Hi Vladimir,

Thanks for the solution. I used the same code and it worked. but in my case, I want that field to be readonly, so the default value is calculated from the max +1 and set as new record inserted, displayed in grid, but user shouldn't be able to change that.

I set the 'Editable(false)' to the model, but it actually breaks that code and new value doesn't applied. However the "DefaultValue" applied in Model wil continue working

ex:
                model.Field(p => p.SequenceNumber).Editable(false);  //There is a js function which filles that field but it fails to update
                model.Field(p => p.ApprovalStateID).Editable(false).DefaultValue(1); // This works!

Any thoughts?
0
Vladimir Iliev
Telerik team
answered on 25 Nov 2013, 01:06 PM
Hi Steffan,


You should leave the field editable and use for example the Edit event of the Grid to find the needed input in the container and disable / remove it. Please check the example below:

function onEdit(e) {
    e.container.find("[name=fieldName]").hide();
}

Kind Regards,
Vladimir Iliev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Patrick
Top achievements
Rank 1
answered on 06 Mar 2016, 06:57 PM
Are there limitations to using this technical? It does not work for me trying to set the foreign key column using the value from the grid above.
0
Vladimir Iliev
Telerik team
answered on 09 Mar 2016, 08:31 AM
Hello Patrick,

No there are no limitations of using this approach - that why in current case I would suggest to inspect the editor template using your browser dev tools and adjust the jQuery selector in order to hide the correct element.

Regards,
Vladimir Iliev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Randy
Top achievements
Rank 1
answered on 24 Mar 2017, 11:42 AM
Excellent. That works, thank you!
Tags
Grid
Asked by
Staffan
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Richard van Diggelen
Top achievements
Rank 1
Patrick
Top achievements
Rank 1
Randy
Top achievements
Rank 1
Share this question
or