如何从kendoListBox删除所有项目

时间:2019-01-29 17:21:12

标签: javascript kendo-ui

我有两个剑道列表框,它们在它们之间交换项目。基本上是一个可用的项和一个选定的项对。 我有Json服务,该服务可通过Json数组控制哪些项目可用。
当用户选择一个新的筛选器时,我希望两个Kendo列表框都清除掉这些项目,然后再从服务器添加新项目。
当前,它将新列表从服务器追加到当前列表。

$(document).ready(function () {
    $("#filterKeyWord").click(function () {
        getResults($("#keywords"));
    });
    $("#availableReports").kendoListBox({
        dataTextField: "Name",
        dataValueField: "ID",
        connectWith: "selectedReports",
        dropSources: ["availableReports"],
        toolbar: {
            tools: ["transferTo", "transferFrom", "transferAllTo", "transferAllFrom", "remove"]
        }
    });

    $("#selectedReports").kendoListBox({
        dataTextField: "Name",
        dataValueField: "ID",
        dropSources: ["selectedReports"],
        remove: function (e) {
            setSelected(e, false);
        },
        add: function (e) {
            setSelected(e, true);
        }
    });

    var mydata = { ReportName: "", UserId: "" };
    mydata.ReportName = "SomeName";
    mydata.UserId = "SomeUser";

    var crudService = "",
        dataSource = new kendo.data.DataSource({
            serverFiltering: true,
            transport: {
                read: {
                    url: crudService + "GetReportList",
                    dataType: "json",
                    type: "get",
                    contentType: "application/json; charset=utf-8",
                },
                update: {
                    url: crudService + "SaveReportList2",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    type: "post",
                },
                filter: {
                    url: crudService + "GetReportList",
                    dataType: "json",
                    type: "get",
                    contentType: "application/json; charset=utf-8",
                },

                parameterMap: function (options, operation) {
                    console.log(operation);
                    if (operation !== "read" && options.models) {
                        return JSON.stringify({ models: options.models });
                    }
                    if (operation === "read") {
                        return "request=" + JSON.stringify(mydata);
                    }
                }
            },
            batch: true,
            requestStart: function () {
                kendo.ui.progress($(".demo-section"), true);
                console.log("request start");
            },
            requestEnd: function () {
                kendo.ui.progress($(".demo-section"), false);
                console.log("request end");
            },
            error: function (e) {
                console.log("Error" + e);
            },
            change: function (e) {
                console.log("change" + this.data.length);
                setDropDownBoxes(this);
            },
            schema: {
                model: {
                    id: "ID",
                    fields: {
                        ID: { type: "number" },
                        Selected: { type: "boolean" },
                        Name: { type: "string" },
                        Description: { type: "string" },
                        InternalId: { type: "string" }
                    }
                }
            }
        });
    $("#saveReportList").kendoButton({
        click: function (e) {
            dataSource.sync();
        }
    });
    $("#getReportList").kendoButton({
        click: function (e) {
            mydata.ReportName = $("#keywords").val();
            dataSource.read();
        }
    });
    function setDropDownBoxes(obj) {
        var data = obj.data();
        var availableReports = $("#availableReports").data("kendoListBox");
        var selectedReports = $("#selectedReports").data("kendoListBox");
        var items = availableReports.dataItems();
        for (var i = 0; i < data.length; i++) {
                if (data[i].Selected) {
                    selectedReports.add(data[i]);
                }
                else {
                    availableReports.add(data[i]);
                }
            }
    }
    function setSelected(e, flag) {
        var removedItems = e.dataItems;
        for (var i = 0; i < removedItems.length; i++) {
            console.log(flag + " " + removedItems[i].ID + " " + removedItems[i].Name + " " + removedItems[i].Selected);
            var item = dataSource.get(removedItems[i].ID);
            item.Selected = flag;
            item.dirty = !item.dirty;
        }
    }
});

1 个答案:

答案 0 :(得分:1)

不确定应该在哪个位置执行整理,但是可以同时使用remove()item()方法来清除列表框。

remove()方法接受li返回的items()元素列表,因此将从列表中删除整个li集合。

var listBox = $("#listBox").data("kendoListBox");
listBox.remove(listBox.items());

Demo