treelist使用url地址做为数据源时,实现全部展开与全部折叠的方法之一

从网上找的方法,备注:这个方法不好使

var treelist =   $("#treelist").data('kendoTreeList');
treelist.options.dataSource.schema.model.expanded = !treelist.options.dataSource.schema.model.expanded
treelist.setDataSource( treelist.options.dataSource)

 

自己实现的方法:

新建一个数据源的函数,然后把是否展开传过去,如下:

function getDataSource(isExpland) {
        var dataSource = new kendo.data.TreeListDataSource({
            transport: {
                read: {
                    url: "@Html.UrlHref("LocalSite", "/BaseInfoManagement/CaseManagement/AllCaseFolder")" + "?CaseId=" + $.getUrlParam('CaseId')
                },
                update: {
                    url: "@Html.UrlHref("LocalSite", "/BaseInfoManagement/CaseManagement/UpdateCaseFolder")",
                    dataType: "json",
                    type: "POST"
                },
                destroy: {
                    url: "@Html.UrlHref("LocalSite", "/BaseInfoManagement/CaseManagement/DestroyCaseFolder")"
                },
                create: {
                    url: "@Html.UrlHref("LocalSite", "/BaseInfoManagement/CaseManagement/CreateCaseFolder")" + "?CaseId=" + $.getUrlParam('CaseId'),
                    dataType: "json",
                    type: "POST"
                },

                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return { models: kendo.stringify(options.models) };
                    }
                }
            },
            requestStart: function (e) {
                parent.ShowLoading();
            },
            requestEnd: function (e) {
                parent.HiddeLoading();
                var response = e.response;
                var type = e.type;
                if (type == "update" || type == "create") {
                    if (response && response.id) {
                        ShowNotify("目录" + (type == "create" ? "添加" : "修改") + "成功!");
                    }
                }

            },
            batch: true,
            schema: {
                model: {
                    id: "ID",
                    fields: {
                        parentId: { field: "PARENT_ID", nullable: true },
                        NAME: { type: "string", editable: true, nullable: false, validation: { required: true } },
                        CASE_FOLDER_KIDE_DISPLAY: { type: "string", nullable: true, validation: { required: false } },
                        CASE_FOLDER_KIDE: { type: "string", editable: true, nullable: true, validation: { required: false } },
                        IS_EMAIL_FOLDER_DISPLAY: { type: "string", nullable: true, validation: { required: false } },
                        IS_EMAIL_FOLDER: { type: "string", editable: true, nullable: true, validation: { required: false } },

                        CASE_FOLDER_KIDEDropdownlistValue: { defaultValue: { ParaValue: 0, DisplayCode: "[文件夹类]" } },
                        IS_EMAIL_FOLDERDropdownlistValue: { defaultValue: { ParaValue: 0, DisplayCode: "" } }
                    },
                    expanded: isExpland
                },
                data: function (response) {
                    if (response.Data && response.Data.error) {
                        ShowNotify(response.Data.msg, true);
                        return response.Data.model;
                    }
                    return response;
                }
            }
        });
        return dataSource;
    }

初始化时,默认展开:

$(document).ready(function () {
        $("#treelist").kendoTreeList({
            dataSource: getDataSource(true),
            toolbar: [{ name: "create", text: "添加根信息", class: "sss" }, { name: "unExpandAll", text: "全部折叠", click: unExpandAll }, { name: "expandAll", text: "全部展开", click: expandAll }],
            save: function (e) {
                //ShowLoading();
            },
            dataBound: function (e) {
                //HiddeLoading();
            },
            //height: 540,
            columns: [
                {
                    field: "NAME", expandable: true, title: "名称"
                },
                {
                    field: "CASE_FOLDER_KIDE", title: "类型", editor: CASE_FOLDER_KIDE_Editor,  template: "#:CASE_FOLDER_KIDE_DISPLAY#", width: 155, headerAttributes: { style: "text-align: center;" }, attributes: { style: "text-align: center;" }
                },
                {
                    field: "IS_EMAIL_FOLDER", title: "包含", editor: IS_EMAIL_FOLDER_Editor,  template: "#:IS_EMAIL_FOLDER_DISPLAY#",width: 85, headerAttributes: { style: "text-align: center;" }, attributes: { style: "text-align: center;" }
                },
                {
                    field: "", title: " ", width: 45, template: $("#templateToTop").html(), headerAttributes: { style: "text-align: center;" }, attributes: { style: "text-align: center;" }
                },
                {
                    field: "", title: " ", width: 35, template: $("#templateToUp").html(), headerAttributes: { style: "text-align: center;" }, attributes: { style: "text-align: center;" }
                },
                {
                    field: "", title: " ", width: 35, template: $("#templateToDown").html(), headerAttributes: { style: "text-align: center;" }, attributes: { style: "text-align: center;" }
                },
                {
                    field: "", title: " ", width: 115, template: $("#templateFromTemplate").html(), headerAttributes: { style: "text-align: center;" }, attributes: { style: "text-align: center;" }
                },
                {
                    command: [{ name: "createChild", text: "添加子目录", attributes: { title: "添加子目录" } }, { name: "edit", text: "编辑" }, { name: "delete", text: "删除", click: FnDelete }], width: 225
                }
            ],
            editable: true
        });
    });

用按钮控件:

    function expandAll() {
        var treelist = $("#treelist").data('kendoTreeList');
        treelist.setDataSource(getDataSource(true));
        treelist.dataSource.read();
    }
    function unExpandAll() {
        var treelist = $("#treelist").data('kendoTreeList');
        treelist.setDataSource(getDataSource(false));
        treelist.dataSource.read();
    }

 

posted @ 2021-03-12 14:47  星星c#  阅读(306)  评论(0编辑  收藏  举报