Dynamic default values

6 posts, 0 answers
  1. Staffan
    Staffan avatar
    1 posts
    Member since:
    Dec 2012

    Posted 29 Jan 2013 Link to this post

    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

  2. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 31 Jan 2013 Link to this post

    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!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Richard van Diggelen
    Richard van Diggelen avatar
    8 posts
    Member since:
    Jul 2012

    Posted 21 Nov 2013 Link to this post

    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?
  5. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 25 Nov 2013 Link to this post

    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!
  6. Patrick
    Patrick avatar
    2 posts
    Member since:
    Apr 2014

    Posted 06 Mar in reply to Vladimir Iliev Link to this post

    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.
  7. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 09 Mar Link to this post

    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
Back to Top
UI for ASP.NET MVC is VS 2017 Ready