gpt4 book ai didi

javascript - kendo ui 列表框删除项目

转载 作者:行者123 更新时间:2023-12-03 22:51:10 24 4
gpt4 key购买 nike

大家好,我需要一些帮助来获取所选项目并将其从 kendoListBox 中删除。
kendoTreeView 中检查项目时,我目前已经成功地将选中的项目添加到 kendoListBox 中,但是当我从 kendoTreeView 中取消选中它时,我无法找到如何删除项目.

let apiData = {
"object_list": [{
"Name": "facebook",
"Description": null,
"Id": "eb8cb0c8c32d1201bff30cf54S4f30889abb23f92b1f7",
"DocumentType": null,
"ProviderId": 2,
"Cat": "Social Networks"
}, {
"Name": "twitter",
"Description": null,
"Id": "732fe66cce4365bc5074384f09b34e787f3f3efe8b55",
"DocumentType": null,
"ProviderId": 2,
"Cat": "Social Networks"
}, {
"Name": "Google",
"Description": null,
"Id": "8b11d6f7b74bf71a7ddbc62dcfead27087e0f3e047e",
"DocumentType": null,
"ProviderId": 2,
"Cat": "Search Engines"
}]
};

$("#treeview").kendoTreeView({
checkboxes: {
checkChildren: true
},
check: onCheck,
dataSource: {
data: apiData,
schema: {
model: {
children: 'items'
},
parse: (data) => {
let newData = [];

data.object_list.forEach(item => {
let parent = newData.find(parentItem => parentItem.text === item.Cat);

if (!parent) {
parent = {
id: 2,
text: item.Cat,
expanded: true,
items: [],
spriteCssClass: "folder"
};

newData.push(parent);
}

parent.items.push({
id: 3,
text: item.Name,
spriteCssClass: "image"
});
});

return [{
id: 1,
text: 'Categories',
expanded: true,
spriteCssClass: "rootfolder",
items: newData
}];
}
}
}
});

$("#Sources").kendoListBox();

function onCheck(e) {
var checkedNodes = [];
var checkedNode = e.node.innerText;

console.log(e.node.innerText);

if (e.node.ariaChecked == "false") {
console.log("Its already selected *REMOVE*");
var listBox = $("#Sources").data("kendoListBox");

console.log(listBox);

} else {
console.log("Its just now been selected *ADD*");
var listBox = $("#Sources").data("kendoListBox");

listBox.add({
text: checkedNode,
value: checkedNode
});
}
}

function onExpand(e) {
if ($("#tbSourcesFilter").val() == "") {
$(e.node).find("li").show();
}
}

$("#tbSourcesFilter").keyup(function(e) {
var filterText = $(this).val().toLowerCase();

//console.log("filterText: ", filterText);

if (filterText !== "") {
$("#treeview .k-group .k-group .k-in").closest("li").hide();
$("#treeview .k-group").closest("li").hide();

$("#treeview .k-in:contains(" + filterText + ")").each(function() {
$(this).parents("ul, li").each(function() {
var treeView = $("#treeview").data("kendoTreeView");
treeView.expand($(this).parents("li"));
$(this).show();
});
});

$("#treeview .k-group .k-in:contains(" + filterText + ")").each(function() {
$(this).parents("ul, li").each(function() {
$(this).show();
});
});
} else {
//console.log("k-group: ", $("#treeview .k-group").find("li").show());
$("#treeview .k-group").find("li").show();
var nodes = $("#treeview > .k-group > li");
//console.log("nodes: ", nodes);

$.each(nodes, function(i, val) {
//console.log(nodes[i].getAttribute("data-expanded"));
if (nodes[i].getAttribute("data-expanded") == null) {
$(nodes[i]).find("li").hide();
}
});
}
});
        #treeview .k-sprite {
background-image: url("../content/web/treeview/coloricons-sprite.png");
}

.rootfolder {
background-position: 0 0;
}

.folder {
background-position: 0 -16px;
}

.pdf {
background-position: 0 -32px;
}

.html {
background-position: 0 -48px;
}

.image {
background-position: 0 -64px;
}

#filterText {
width: 100%;
box-sizing: border-box;
padding: 6px;
border-radius: 3px;
border: 1px solid #d9d9d9;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.common.min.css">
<link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.rtl.min.css">
<link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.default.min.css">
<link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.mobile.all.min.css">
<script type="text/javascript" src="https://kendo.cdn.telerik.com/2021.1.330/js/angular.min.js"></script>
<script type="text/javascript" src="https://kendo.cdn.telerik.com/2021.1.330/js/jszip.min.js"></script>
<script type="text/javascript" src="http://cdn.kendostatic.com/2021.1.330/js/kendo.all.min.js"></script>
<input id="filterText" type="text" placeholder="Search categories" />
<div id="treeview"></div>
<select id="Sources"></select>

我看过一些例子,但它们似乎都没有做任何事情。
下面的代码确实删除了(隐藏的)选择框中的选定项目,但不会从 kendoListBox 中删除。
$("select option[value='" + checkedNode + "']").remove();
似乎 Kendo UI 在添加项目时将其放入 kendoListBox 中。
<li class="k-item" role="option" aria-selected="false">facebook</li>
并且代码 here 仅显示如何删除 kendoListBox 中的第一项而不是选定的项。
var listBox = $("#listBox").data("kendoListBox");
listBox.remove(listBox.items().first());
我相信这会很容易让我忽略,但如果有人能帮助我找到一种通过 kendoUI api/命令删除它的方法,那就太好了。

最佳答案

您可以通过两种方式做到这一点:

  • 使用ListBox的 remove() 方法和jQuery选择的目标节点:

  • let apiData = {
    "object_list": [{
    "Name": "facebook",
    "Description": null,
    "Id": "eb8cb0c8c32d1201bff30cf54S4f30889abb23f92b1f7",
    "DocumentType": null,
    "ProviderId": 2,
    "Cat": "Social Networks"
    }, {
    "Name": "twitter",
    "Description": null,
    "Id": "732fe66cce4365bc5074384f09b34e787f3f3efe8b55",
    "DocumentType": null,
    "ProviderId": 2,
    "Cat": "Social Networks"
    }, {
    "Name": "Google",
    "Description": null,
    "Id": "8b11d6f7b74bf71a7ddbc62dcfead27087e0f3e047e",
    "DocumentType": null,
    "ProviderId": 2,
    "Cat": "Search Engines"
    }]
    };

    $("#treeview").kendoTreeView({
    checkboxes: {
    checkChildren: true
    },
    check: onCheck,
    dataSource: {
    data: apiData,
    schema: {
    model: {
    children: 'items'
    },
    parse: (data) => {
    let newData = [];

    data.object_list.forEach(item => {
    let parent = newData.find(parentItem => parentItem.text === item.Cat);

    if (!parent) {
    parent = {
    id: 2,
    text: item.Cat,
    expanded: true,
    items: [],
    spriteCssClass: "folder"
    };

    newData.push(parent);
    }

    parent.items.push({
    id: 3,
    text: item.Name,
    spriteCssClass: "image"
    });
    });

    return [{
    id: 1,
    text: 'Categories',
    expanded: true,
    spriteCssClass: "rootfolder",
    items: newData
    }];
    }
    }
    }
    });

    $("#Sources").kendoListBox();

    function onCheck(e) {
    let checkedNodes = [],
    checkedNode = e.node.innerText,
    listBox = $("#Sources").data("kendoListBox");

    console.log(e.node.innerText);

    if (e.node.ariaChecked == "false") {
    let $listBoxItem = listBox.wrapper.find(`li:contains("${e.node.innerText}")`);
    listBox.remove($listBoxItem);
    } else {
    listBox.add({
    text: checkedNode,
    value: checkedNode
    });
    }
    }

    function onExpand(e) {
    if ($("#tbSourcesFilter").val() == "") {
    $(e.node).find("li").show();
    }
    }

    $("#tbSourcesFilter").keyup(function(e) {
    var filterText = $(this).val().toLowerCase();

    //console.log("filterText: ", filterText);

    if (filterText !== "") {
    $("#treeview .k-group .k-group .k-in").closest("li").hide();
    $("#treeview .k-group").closest("li").hide();

    $("#treeview .k-in:contains(" + filterText + ")").each(function() {
    $(this).parents("ul, li").each(function() {
    var treeView = $("#treeview").data("kendoTreeView");
    treeView.expand($(this).parents("li"));
    $(this).show();
    });
    });

    $("#treeview .k-group .k-in:contains(" + filterText + ")").each(function() {
    $(this).parents("ul, li").each(function() {
    $(this).show();
    });
    });
    } else {
    //console.log("k-group: ", $("#treeview .k-group").find("li").show());
    $("#treeview .k-group").find("li").show();
    var nodes = $("#treeview > .k-group > li");
    //console.log("nodes: ", nodes);

    $.each(nodes, function(i, val) {
    //console.log(nodes[i].getAttribute("data-expanded"));
    if (nodes[i].getAttribute("data-expanded") == null) {
    $(nodes[i]).find("li").hide();
    }
    });
    }
    });
    #treeview .k-sprite {
    background-image: url("../content/web/treeview/coloricons-sprite.png");
    }

    .rootfolder {
    background-position: 0 0;
    }

    .folder {
    background-position: 0 -16px;
    }

    .pdf {
    background-position: 0 -32px;
    }

    .html {
    background-position: 0 -48px;
    }

    .image {
    background-position: 0 -64px;
    }

    #filterText {
    width: 100%;
    box-sizing: border-box;
    padding: 6px;
    border-radius: 3px;
    border: 1px solid #d9d9d9;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.common.min.css">
    <link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.rtl.min.css">
    <link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.default.min.css">
    <link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.mobile.all.min.css">
    <script type="text/javascript" src="https://kendo.cdn.telerik.com/2021.1.330/js/angular.min.js"></script>
    <script type="text/javascript" src="https://kendo.cdn.telerik.com/2021.1.330/js/jszip.min.js"></script>
    <script type="text/javascript" src="http://cdn.kendostatic.com/2021.1.330/js/kendo.all.min.js"></script>
    <input id="filterText" type="text" placeholder="Search categories" />
    <div id="treeview"></div>
    <select id="Sources"></select>

  • 使用 DataSource 的 remove() 方法,目标 Model 选自 dataItems() :

  • let apiData = {
    "object_list": [{
    "Name": "facebook",
    "Description": null,
    "Id": "eb8cb0c8c32d1201bff30cf54S4f30889abb23f92b1f7",
    "DocumentType": null,
    "ProviderId": 2,
    "Cat": "Social Networks"
    }, {
    "Name": "twitter",
    "Description": null,
    "Id": "732fe66cce4365bc5074384f09b34e787f3f3efe8b55",
    "DocumentType": null,
    "ProviderId": 2,
    "Cat": "Social Networks"
    }, {
    "Name": "Google",
    "Description": null,
    "Id": "8b11d6f7b74bf71a7ddbc62dcfead27087e0f3e047e",
    "DocumentType": null,
    "ProviderId": 2,
    "Cat": "Search Engines"
    }]
    };

    $("#treeview").kendoTreeView({
    checkboxes: {
    checkChildren: true
    },
    check: onCheck,
    dataSource: {
    data: apiData,
    schema: {
    model: {
    children: 'items'
    },
    parse: (data) => {
    let newData = [];

    data.object_list.forEach(item => {
    let parent = newData.find(parentItem => parentItem.text === item.Cat);

    if (!parent) {
    parent = {
    id: 2,
    text: item.Cat,
    expanded: true,
    items: [],
    spriteCssClass: "folder"
    };

    newData.push(parent);
    }

    parent.items.push({
    id: 3,
    text: item.Name,
    spriteCssClass: "image"
    });
    });

    return [{
    id: 1,
    text: 'Categories',
    expanded: true,
    spriteCssClass: "rootfolder",
    items: newData
    }];
    }
    }
    }
    });

    $("#Sources").kendoListBox();

    function onCheck(e) {
    let checkedNodes = [],
    checkedNode = e.node.innerText,
    listBox = $("#Sources").data("kendoListBox");

    console.log(e.node.innerText);

    if (e.node.ariaChecked == "false") {
    let listBoxItem = listBox.dataItems().find(item => item.text === e.node.innerText);

    if (listBoxItem) {
    listBox.dataSource.remove(listBoxItem);
    }

    } else {
    listBox.add({
    text: checkedNode,
    value: checkedNode
    });
    }
    }

    function onExpand(e) {
    if ($("#tbSourcesFilter").val() == "") {
    $(e.node).find("li").show();
    }
    }

    $("#tbSourcesFilter").keyup(function(e) {
    var filterText = $(this).val().toLowerCase();

    //console.log("filterText: ", filterText);

    if (filterText !== "") {
    $("#treeview .k-group .k-group .k-in").closest("li").hide();
    $("#treeview .k-group").closest("li").hide();

    $("#treeview .k-in:contains(" + filterText + ")").each(function() {
    $(this).parents("ul, li").each(function() {
    var treeView = $("#treeview").data("kendoTreeView");
    treeView.expand($(this).parents("li"));
    $(this).show();
    });
    });

    $("#treeview .k-group .k-in:contains(" + filterText + ")").each(function() {
    $(this).parents("ul, li").each(function() {
    $(this).show();
    });
    });
    } else {
    //console.log("k-group: ", $("#treeview .k-group").find("li").show());
    $("#treeview .k-group").find("li").show();
    var nodes = $("#treeview > .k-group > li");
    //console.log("nodes: ", nodes);

    $.each(nodes, function(i, val) {
    //console.log(nodes[i].getAttribute("data-expanded"));
    if (nodes[i].getAttribute("data-expanded") == null) {
    $(nodes[i]).find("li").hide();
    }
    });
    }
    });
    #treeview .k-sprite {
    background-image: url("../content/web/treeview/coloricons-sprite.png");
    }

    .rootfolder {
    background-position: 0 0;
    }

    .folder {
    background-position: 0 -16px;
    }

    .pdf {
    background-position: 0 -32px;
    }

    .html {
    background-position: 0 -48px;
    }

    .image {
    background-position: 0 -64px;
    }

    #filterText {
    width: 100%;
    box-sizing: border-box;
    padding: 6px;
    border-radius: 3px;
    border: 1px solid #d9d9d9;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.common.min.css">
    <link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.rtl.min.css">
    <link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.default.min.css">
    <link rel="stylesheet" type="text/css" href="https://kendo.cdn.telerik.com/2021.1.330/styles/kendo.mobile.all.min.css">
    <script type="text/javascript" src="https://kendo.cdn.telerik.com/2021.1.330/js/angular.min.js"></script>
    <script type="text/javascript" src="https://kendo.cdn.telerik.com/2021.1.330/js/jszip.min.js"></script>
    <script type="text/javascript" src="http://cdn.kendostatic.com/2021.1.330/js/kendo.all.min.js"></script>
    <input id="filterText" type="text" placeholder="Search categories" />
    <div id="treeview"></div>
    <select id="Sources"></select>

    关于javascript - kendo ui 列表框删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67269613/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com