简体   繁体   English

如何隐藏隐藏的下拉列表项?

[英]How to hide hidden dropdownlist item?

I have a Kendo DropdownList as follows:我有一个剑道下拉列表如下:

 $("#txtTag").kendoDropDownList({
    dataTextField: "TagDesc",
    dataValueField: "TagId",
    optionLabel: " ",
    dataSource: {
        transport: {
            read: {
                dataType: "json",
                url: "/Data/GetTag"
            }
        }
    },
    change: onChange,
    filter: "contains"
});

I have hidden one of the values by using $("#txtTag_listbox li")[4].hidden = true;我使用$("#txtTag_listbox li")[4].hidden = true;隐藏了其中一个值

However, when I do a filter/search on the dropdown List, the hidden item also appears in that list.但是,当我对下拉列表进行过滤/搜索时,隐藏的项目也会出现在该列表中。 How do I prevent it to not appear it in the list?如何防止它不出现在列表中?

The best way to do this is to set a default filter on your underlying dataSource:最好的方法是在底层数据源上设置一个默认过滤器:

$(document).ready(function () {
  $("#dropdownlist").kendoDropDownList({
    dataSource: {
      data: [
        {
          id: 1,
          text: 'show'
        },
        {
          id: 2,
          text: 'hide'
        },
        {
          id: 3,
          text: 'show2'
        }
      ],
      filter: { field: 'text', operator: 'neq', value: 'hide' }
    },
    dataTextField: 'text',
    dataValueField: 'id',
    filter: 'contains',
    optionLabel: {
      id: null,
      text: '-'
    }
  });
});

Fiddle: https://dojo.telerik.com/uZAcIxil小提琴: https://dojo.telerik.com/uZAcIxil

By using the dataBound property of Kendo DropdownList, I was able to hide the required element from the UI as well as from it appearing in the search.通过使用 Kendo DropdownList 的 dataBound 属性,我能够从 UI 以及搜索中隐藏所需的元素。 Only thing is instead of using an index, I am directly searching for the element to hide as the index changes everytime we do a search.唯一的事情不是使用索引,而是直接搜索要隐藏的元素,因为每次我们进行搜索时索引都会发生变化。

$("#txtTag").kendoDropDownList({
    dataTextField: "TagDesc",
    dataValueField: "TagId",
    optionLabel: " ",
    dataSource: {
        transport: {
            read: {
                dataType: "json",
                url: "/Data/GetTag"
            }
        }
    },
    dataBound: function (e) {
        var items = $("#txtTag_listbox li")
        for (var i = 0; i < items.length; i++) {
            if (items[i].innerHTML === 'Text To Hide') {
                $(items[i]).hide();
            }
        }
    },
    change: onChange,
    filter: "contains"
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM