如何在下拉菜单中添加搜索栏?

时间:2018-11-26 05:41:53

标签: javascript asp.net-mvc bootstrap-4

有人可以帮我添加搜索栏作为下拉列表的第一个值吗?我使用了ASP.NET MVC。这是我的代码

<div class="col-md-8">
    <div class="dropdown">
         <div class="chzn-dd-width">
               @Html.DropDownListFor(
                   model => model.DriverId, 
                   Model.Drivers, 
                   new { @id = "driverDropDown", @class = " form-control chosen-search" })
          </div>
     </div>
</div>

4 个答案:

答案 0 :(得分:0)

您好Ravindu Saluwadana,

您尝试过这个吗?解决方案add search functionality on DropDownListFor

@Html.DropDownListFor(x =>
    x.StockCode,
    (IEnumerable<SelectListItem>)ViewBag.AllStockList,
    new
    {
        @class = "form-control selectpicker",
        @Value = @Model.Description,
        onchange = "this.form.submit();",
        data_show_subtext="true",
        data_live_search="true"
})

答案 1 :(得分:0)

function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    div = document.getElementById("myDropdown");
    a = div.getElementsByTagName("a");
    for (i = 0; i < a.length; i++) {
        if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
            a[i].style.display = "";
        } else {
            a[i].style.display = "none";
        }
    }
}
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

#myInput {
    border-box: box-sizing;
    background-image: url('searchicon.png');
    background-position: 14px 12px;
    background-repeat: no-repeat;
    font-size: 16px;
    padding: 14px 20px 12px 45px;
    border: none;
    border-bottom: 1px solid #ddd;
}

#myInput:focus {outline: 3px solid #ddd;}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow: auto;
    border: 1px solid #ddd;
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}
    <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <a href="#about">item test</a>
    <a href="#base">soheil</a>
    <a href="#blog">bijavar</a>
    <a href="#contact">php</a>
    <a href="#custom">Hello</a>
    <a href="#support">Support</a>
    <a href="#tools">Tools</a>
  </div>
</div>



</body>
</html>

答案 2 :(得分:0)

您可以使用3rd party插件,该插件允许多个下拉菜单(如multiselect),新样式以及搜索功能

使用此SELECT2下拉插件

https://select2.org/searching

答案 3 :(得分:0)

使用此功能-下拉列表中的 data_live_search =“ true”

要使用该功能,您必须使用以下脚本-

1.bootstrap-select.min.js
2.boostrap-select.min.css

您可以从此处下载该文件-https://cdnjs.com/libraries/bootstrap-select

示例-

<div class="col-md-8">
    <div class="dropdown">
         <div class="chzn-dd-width">
              @Html.DropDownListFor(
                model => model.DriverId, 
               Model.Drivers, 
               new { @id = "driverDropDown", @class = " form-control chosen-search" ,data_live_search="true"})
      </div>
 </div>

希望它能对您有所帮助。