kendo自定义TreeList手动添加childnode意外结果

时间:2015-01-26 19:11:58

标签: kendo-ui custom-controls add crud treelist

  • 列表项

我有一个页面,通过按下按钮加载一个kendo TreeList。数据暂时在变量中静态定义,它作为Kendo TreeList数据源的基础。

我有一个数据源定义,我主要从Telerik网站复制。 我有一个在CRUD方面有一些要求的树形图。

  • level1 - 没有
  • level2 - 仅添加新的子节点
  • level3 - 编辑和删除

编辑应该在level3项目上双击

CRUD命令按钮必须是仅图标(按钮中没有文字)

遗憾的是,我无法使用buildin CRUD控件实现这一点,因此我使用了一个模板列,其中的按钮根据其“类型”字段放置。

现在这已经奏效了但经过一些我无法以某种方式撤消的更改后,添加功能不再起作用了。它可以工作,但只有在编辑或删除另一个节点后才能看到新的子节点。 (好像在添加期间未触发更改事件)。 treeList中的Add按钮调用函数addProduct,最后我尝试将pushCreate直接调用到数据源。但是从不调用Transport.create。只有在另一个Crud动作触发后才会调用它

任何人都可以看到什么是错的,并且不能用更简单的方法实现这一切吗?

这是页面:

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Kendo UI Grid - CRUD operations with local data</title>

    <style>
        html {
            font-size: 12px;
            font-family: Arial, Helvetica, sans-serif;
        }
    </style>
    <link href="styles/kendo.common.min.css" rel="stylesheet" />
    <link href="styles/kendo.default.min.css" rel="stylesheet" />

    <script src="Scripts/jquery-2.1.3.min.js"></script>
    <!--<script src="Scripts/kendo.all.min.js"></script>-->
    <script src="Scripts/kendo.all.js"></script>
</head>
<body>
    <style>
        .hidden {
            display: none;
        }

        .k-grid tbody .k-button, .k-ie8 .k-grid tbody button.k-button {
            min-width: 0px;
            padding-left: 10px;
            padding-right: 0px;
            padding-bottom: 0px;
            padding-top: 0px;
            margin: 0px;
        }
    </style>

    <div id="buttons">
        <br />
        <p>
            <button name="clear" id="clear" onclick="myclear()">Clear grid</button>
            <button name="load" id="load" onclick="loadLocal()">Load from local DB</button>

        </p>
        <br />
        version 1.01<br />
        <br />
    </div>
    <div id="treelist"></div>

    <script id="btn-template" type="text/x-kendo-template">
        # if (Code == "Product") { #
        <a id="btnupdate" class="k-button k-button-icontext k-grid-update column hidden" title="Update product" onclick="update(this)" href="\#"><span class="k-icon k-update"></span></a>
        <a id="btndelete" class="k-button k-button-icontext k-grid-delete column" title="Delete product" data-command="destroy" href="\#"><span class="k-icon k-delete"></span></a>
        # } else if (Code == "Requirement") { #
        <a class="k-button k-button-icontext k-grid-add column" title="Add a product to this requirement" onclick="addProduct(this)" href="\#"><span class="k-icon k-add"></span></a>
        # } #
    </script>

    <script>

        var EPDdata  // For holding the data of the TreeList


        function loadLocal() {
            EPDdata = [
                          { Id: 1, Description: "Item1", Code: "Category", parentId: null },
                          { Id: 2, Description: "Item2", Code: "Requirement", parentId: 1 },
                          { Id: 3, Description: "Item3", Code: "Product", parentId: 2 },
                          { Id: 4, Description: "Item4", Code: "Requirement", parentId: 1 },
                          { Id: 5, Description: "Item5", Code: "Product", parentId: 4 },
                          { Id: 6, Description: "Item6", Code: "Product", parentId: 4 },
                          { Id: 7, Description: "Item7", Code: "Requirement", parentId: 1 },
                          { Id: 8, Description: "Item8", Code: "Requirement", parentId: 1 },
                          { Id: 9, Description: "Item9", Code: "Product", parentId: 8 },
                          { Id: 10, Description: "Item10", Code: "Product", parentId: 8 }
            ]
            LoadTree();
        };

        function LoadTree() {
            var EPDdataNextID = EPDdata.length + 1;

            var LocaldataSource = new kendo.data.TreeListDataSource({
                transport: {
                    read: function (e) {
                        // on success
                        e.success(EPDdata);
                    },
                    create: function (e) {
                        // assign an ID to the new item
                        e.data.Id = EPDdataNextID++;
                        // save data item to the original datasource
                        EPDdata.push(e.data);
                        // on success
                        e.success(e.data);
                    },
                    update: function (e) {
                        // locate item in original datasource and update it
                        EPDdata[getIndexById(e.data.Id)] = e.data;
                        // on success
                        e.success();
                    },
                    destroy: function (e) {
                        // locate item in original datasource and remove it
                        EPDdata.splice(getIndexById(e.data.Id), 1);
                        // on success
                        e.success();
                    }
                },
                error: function (e) {
                    // handle data operation error
                    alert("Status: " + e.status + "; Error message: " + e.errorThrown);
                },
                pageSize: 10,
                expanded: true,
                batch: false,
                schema: {
                    model: {
                        id: "Id",
                        expanded: true,
                        fields: {
                            Id: { type: "number", editable: false, nullable: true },
                            Description: { type: "string", validation: { required: true } },
                            Code: { type: "string" },
                            parentId: { type: "number", editable: true, nullable: true }
                        }
                    }
                }
            });


            $("#treelist").empty(); // only 1 treelist on the page please

            $("#treelist").kendoTreeList({
                dataSource: LocaldataSource,
                pageable: false,
                edit: onEdit,
                columns: [
                    { field: "Description", title: "Description", width: "400px" },
                    { field: "Code", width: "120px" },
                    { field: "Id", title: "ID", width: "30px" },
                    { field: "parentId", title: "PID", width: "30px" },
                    { width: "35px", template: $("#btn-template").html() },
                    { command: ["create", "edit", "destroy"] }

                ],
                editable: "inline"
            });

            var treeList = $("#treelist").on("dblclick", function (e) {
                var treeList = $("#treelist").data("kendoTreeList");
                var rowindex = e.target.parentNode.rowIndex; // get rowindex
                var tr = $(e.target).closest("tr"); // get the current table row (tr)
                var dataItem = $("#treelist").data("kendoTreeList").dataItem(tr);

                if (dataItem.Code == "Product") {

                    $("#treelist").find(".edit").addClass("hidden");
                    $("#treelist").find(".edit").removeClass("edit");
                    $("#treelist").find(".delete").removeClass("hidden");
                    $("#treelist").find(".delete").removeClass("delete");

                    treeList.saveRow(); // first save all other rows
                    treeList.editRow(tr[0]);
                };
            }); // double click function
        }; // Function CreatetreeList

        function onEdit(arg) {
            var tr = $(arg.container);//.closest("tr"); // get the current table row (tr)
            tr.find("#btndelete").addClass("hidden"); //remove btndelete from commandcolumn
            tr.find("#btndelete").addClass("delete"); //put class to select the btn later on
            tr.find("#btnupdate").removeClass("hidden"); //make btnupdate visible in commandcolumn
            tr.find("#btnupdate").addClass("edit"); //put class to select the btn later on
        };

        function update(e) { // update the edited row
            var tr = $(e).closest("tr"); // get the current table row (tr)
            var treeList = $("#treelist").data("kendoTreeList");
            treeList.saveRow();
            tr.find("#btndelete").removeClass("hidden");
            tr.find("#btndelete").removeClass("delete");
            tr.find("#btnupdate").addClass("hidden");
            tr.find("#btnupdate").removeClass("edit");
        };

        function addProduct(e) {
            var treeList = $("#treelist").data("kendoTreeList");
            var dataSource = treeList.dataSource;
            var data = dataSource.data;
            var tr = $(e).closest("tr"); // get the current table row (tr)
            var dataItem = treeList.dataItem(tr);

            dataSource.pushCreate({ Id: 15, Description: "New", Code: "Product", parentId: dataItem.Id });
            alert("Done");
        };

        function getIndexById(id) {
            var idx,
                l = EPDdata.length;

            for (var j; j < l; j++) {
                if (EPDdata[j].Id == id) {
                    return j;
                }
            }
            return null;
        }
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我找到了答案!!!

datasource pagesize设置为10,TreeList设置为paging:false。在我的所有测试中,我从10个节点的示例数据开始。我一直在添加第11和第12条记录,直到我删除另一个节点才显示...

这些事情只发生在我身上吗?