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

Custom Editor For Template Column

1 Answer 204 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard van Diggelen
Top achievements
Rank 1
Richard van Diggelen asked on 11 Oct 2013, 01:06 AM
Hi,

I am creating a property grid control in MVC and so I need to pass a collection of model attributes to the grid. because each model property has different type and so different editor, e.g. the int property may use a dropdown for edit or simple text. here is the basic structure.

public enum PropertyGridEditorTemplate
    {
        Default,
        DateTime,
        DropDown,
        ....
    }
   public class PropertyMetaDataValue
    {
        public object CurrentValue { get; set; }
        public object DefaultValue { get; set; }
        public PropertyGridEditorTemplate Editor { get; set; }
    }

    public class PropertyMetaData
    {
        public string GroupName { get; set; }
        public string PropertyName { get; set; }
        [ReadOnly(true)]
        public string DisplayName { get; set; }
        
        public PropertyMetaDataValue Value { get; set; }
}

In the view, I have a grid like this:

@(Html.Kendo().Grid(Model.MetaDatas)
        .Name("propertyPanel")
        .Columns(columns =>
            {
                ...
                columns.Bound(p => p.DisplayName);
                columns.Bound(p => p.Value.CurrentValue).EditorTemplateName("PropertyGridEditor");
            })
        
        .DataSource(dataSource => dataSource
            .Ajax()
            .Batch(true)
            .Model(model => model.Id(p => p.PropertyName))
            .Read(read => read.Action("Fee_Read", "Home", new { feeSheetId = Model.FeeSheetID, feeTypeId = Model.FeeID }))
            .Group(g => g.Add( grp => grp.GroupName))
        )
        .Editable(editable => editable.Mode(GridEditMode.InCell))
        .Navigatable()
        .TableHtmlAttributes(new { @class = "property-grid"})
        )

Now the issue is,  in custom editor I want to check the "Editor" value and based on that show different control. but there is no way to pass the "Editor" to the custom editor. I tried ViewData which didn't work. also tried Template column but the issue is I can't set custom editor for template. 

Any thoughts?

1 Answer, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 14 Oct 2013, 09:27 AM
Hello Richard,

The described behavior is currently not supported out of the box, however it could be achieved using an approach that allows you to override the columns.editor option. For example:  
  1. Add a handler to the Grid's edit event:  
    @(Html.Kendo().Grid(Model.MetaDatas)   
        .Name("propertyPanel")   
        .Events(e=>e.Edit("onEdit"))
        ...
    )
  2. Once the event is triggered check what editor the current cell should use:  
    <script type="text/javascript">
        function onEdit(e) {
             var editorType =  e.model.EditorType; 
        }
    </script>
  3. Use the event.container to initialize the corresponding widget:  
    if (editorType == "numericTextBox")
    {
        var editor = e.container.find("input").kendoNumericTextBox({});
    }



Regards,
Alexander Popov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Richard van Diggelen
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Share this question
or