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

Setting default values when adding new row

1 Answer 615 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Neil
Top achievements
Rank 1
Neil asked on 02 Jun 2017, 06:42 PM

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);
        }
    }

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 06 Jun 2017, 10:33 AM
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 (charts) and form elements.
Tags
Grid
Asked by
Neil
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or