Setting default values when adding new row

2 posts, 0 answers
  1. Neil
    Neil avatar
    28 posts
    Member since:
    Aug 2016

    Posted 02 Jun Link to this post

    I have a 3 part question.

    1) Can you set the value of a text in the same row, based on a selection from a dropdownlist. When a user chooses a Field, I would like to update the DisplayName column to a value based on the Field they chose.

    2) How do you set the default value of a ClientTemplate checkbox to true

    3) How do you set the default value of a textbox, I want to set the Ordering textbox to count + 1.

    Following his my grid definition and relevant OnEdit function.

    @(Html.Kendo().Grid<SOCKETWorx.Core.Data.GridViewProjection>()
                                                .Name("grid_ViewProjections")
                                                .Columns(columns =>
                                                {
                                                    columns.ForeignKey(m => m.FieldId, (System.Collections.IEnumerable)ViewData["AvailableFields"], "Id", "Value")
                                                        .Title("Field").EditorTemplateName("FieldsEditor")
                                                        .HeaderHtmlAttributes(new { style = "text-align:center" })
                                                        .Width(175);
                                                    columns.Bound(m => m.Display)
                                                        .Title("Display")
                                                        .ClientTemplate("<input type='checkbox' #=Display ? checked='checked' : '' # />")
                                                        .HeaderHtmlAttributes(new { style = "text-align:center" })
                                                        .Width(65).HtmlAttributes(new { style = "text-align: center" });
                                                    columns.Bound(m => m.DisplayName)
                                                        .Title("Display Name")
                                                        .HeaderHtmlAttributes(new { style = "text-align:center" })
                                                        .Width(250);
                                                    columns.Bound(m => m.Ordering).Title("Order")
                                                        .HeaderHtmlAttributes(new { style = "text-align:center" })
                                                        .Width(50)
                                                        .HtmlAttributes(new { style = "text-align: center" });
                                                    columns.Command(command => { command.Destroy(); }).Width(100);
                                                })
                                                .ToolBar(toolbar => {
                                                    toolbar.Create().Text("Add");
                                                    toolbar.Save().SaveText("Save").CancelText("Cancel");
                                                    toolbar.Custom().Text("Custom View").Action("List", "Data", new { moduleId = Model.ModuleId, viewId = Model.Id });
                                                })
                                                .Editable(editable => editable.Mode(GridEditMode.InCell).CreateAt(GridInsertRowPosition.Bottom))
                                                .Filterable()
                                                .Navigatable()
                                                .Sortable(sortable =>
                                                {
                                                    sortable.SortMode(GridSortMode.SingleColumn);
                                                })
                                                .Scrollable()
                                                .Events(e => e.Edit("OnEdit"))
                                                .HtmlAttributes(new { style = "height:575px" })
                                                .DataSource(dataSource => dataSource
                                                    .Ajax()
                                                    .Batch(true)
                                                    .Model(model => model.Id(p => p.Id))
                                                    .ServerOperation(false)
                                                    .Events(events => events.Error("OnError"))
                                                    .Read(read => read.Action("ViewProjectons_Read", "ViewManager").Data("OnData"))
                                                    .Create(create => create.Action("ViewProjectons_Create", "ViewManager").Data("OnData"))
                                                    .Update(update => update.Action("ViewProjectons_Update", "ViewManager").Data("OnData"))
                                                    .Destroy(destroy => destroy.Action("ViewProjectons_Destroy", "ViewManager"))
                                                )
                                            )
    function OnEdit(e) {
            if (e.model.isNew()) {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("GetGridViewColumnCount", "ViewManager")',
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify({ viewId: @Model.Id }),
                    dataType: "json",
                    success: function (result) {
                        if (result.status != "Success") {
                            var order = e.container.find("input[name=Ordering]");
                            if (order) {
                                order.val(result.message);
                            }                       
                        }
                    },
                    error: function () {
                        $(".validation-summary-valid").text(result.message);
                    }
                });
                e.container.find("input[name=Display]").prop('checked', true);
            }
        }
  2. Stefan
    Admin
    Stefan avatar
    912 posts

    Posted 06 Jun Link to this post

    Hello Neil,

    Regarding the questions:

    1) This will depend if the DropDown will be part of the column or not.

    - If the DropDown will be used as custom editor this can be achieved as shown in our demo which is provided below. Also, additional information can be found at:

    http://docs.telerik.com/aspnet-mvc/helpers/grid/templating/editor-templates#configuration-Create

    - If the DropDown will be outside of the Grid, this will require custom logic, to programmatically set a new value in the Grid cell on the change event of the DropDown:

    http://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist#events-change

    2 and 3) The default value is used when adding a new item and should be added in the model as shown in our example:

    http://demos.telerik.com/aspnet-mvc/grid/editing-custom

    DefaultValue(ViewData["defaultCategory"] as Kendo.Mvc.Examples.Models.CategoryViewModel);


    Regards,
    Stefan
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top