0
votes

I have a kendo grid that is edited inline. One of the fields should be edited selecting an element from a list, but the list must have a hierarchical structure (would be nice be able of filter that list). I was thinking in use a kendo treeview as editor for that field but I haven't found any way to accomplish this. I tried make a custom editor template (using columns.Bound(s => s.FieldId).EditorTemplateName("_TreeEditorTemplate")) that render the treeview, but the treeview is not an input and is not selectable. I also thinked in make an editor that use a kendo dropdownlist with the tree inside but this is no currently supported by kendo. Any ideas???

1

1 Answers

0
votes

Have you looked at the sample on the Kendo site: https://docs.telerik.com/kendo-ui/controls/data-management/grid/how-to/Editing/use-treeview-as-grid-editor

 <div id="example">
        <div id="grid"></div>

        <script>
            $(document).ready(function () {
                var crudServiceBaseUrl = "//demos.telerik.com/kendo-ui/service",
                    dataSource = new kendo.data.DataSource({
                        transport: {
                            read: {
                                url: crudServiceBaseUrl + "/Products",
                                dataType: "jsonp"
                            },
                            update: {
                                url: crudServiceBaseUrl + "/Products/Update",
                                dataType: "jsonp"
                            },
                            destroy: {
                                url: crudServiceBaseUrl + "/Products/Destroy",
                                dataType: "jsonp"
                            },
                            create: {
                                url: crudServiceBaseUrl + "/Products/Create",
                                dataType: "jsonp"
                            },
                            parameterMap: function (options, operation) {
                                if (operation !== "read" && options.models) {
                                    return { models: kendo.stringify(options.models) };
                                }
                            }
                        },
                        batch: true,
                        pageSize: 20,
                        schema: {
                            model: {
                                id: "ProductID",
                                fields: {
                                    ProductID: { editable: false, nullable: true },
                                    ProductName: { validation: { required: true } },
                                    UnitPrice: { type: "number", validation: { required: true, min: 1 } },
                                    Discontinued: { type: "boolean" },
                                    UnitsInStock: { type: "number", validation: { min: 0, required: true } }
                                },
                            }
                        }
                    });

                $("#grid").kendoGrid({
                    dataSource: dataSource,
                    edit: function (e) {
                        //checking if a cell from the Test column is opened for editing
                        var dummyInput = e.container.find("input[name='test']");
                        if (dummyInput.length > 0) {
                            var treeView = $(e.container).find(".treeViewEditor").data("kendoTreeView");
                            var originalItem = treeView.findByText(dummyInput.val());
                            if (originalItem != null) {
                                // Select the item based on the field value
                                treeView.select(originalItem);
                            }
                        }
                    },
                    navigatable: true,
                    pageable: true,
                    height: 550,
                    toolbar: ["create", "save", "cancel"],
                    columns: [
                        "ProductName",
                        {
                            field: "test", title: "Test", width: 120,
                            editor: function (container, options) {
                                var input = $("<input class='tveInput'/>");
                                input.attr("name", options.field);
                                var tvDiv = $("<div class='treeViewEditor'></div>");
                                $(tvDiv).kendoTreeView({
                                    animation: false,
                                    dataSource: [
                                      {
                                        text: "foo1"
                                      },
                                      {
                                        text: "foo2",
                                        items: [
                                            { text: "bar" },
                                            { text: "bar1" },
                                            { text: "bar2" }
                                        ]
                                      }
                                    ]
                                });
                                var treeView = $(tvDiv).data("kendoTreeView");
                                $(tvDiv).find(".k-in").mousedown(function (e) {
                                    var clickedNode = $(e.toElement).closest("[role=treeitem]");
                                    var dataItem = treeView.dataItem(clickedNode);
                                    var dummyInput = clickedNode.closest("[role=gridcell]").find("input[name='test']");
                                    dummyInput.val(dataItem.text);
                                    dummyInput.trigger("change");
                                });

                                tvDiv.appendTo(container);
                                input.appendTo(container);
                            }
                        },
                        { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: 120 },
                        { field: "UnitsInStock", title: "Units In Stock", width: 120 },
                        { field: "Discontinued", width: 120 },
                        { command: "destroy", title: "&nbsp;", width: 150 }],
                    editable: true
                });
            });
        </script>
    </div>

    <style>
        .tveInput {
            display: none;
        }
  </style>