简体   繁体   English

如何过滤下拉列表中的元素?

[英]How to filter the elements in dropdown?

I have a scenario, in that I have to filter the elements in dropdown based on input given in text box. 我有一个场景,因为我必须根据文本框中给出的输入过滤下拉列表中的元素。 Is it possible, so how to do that? 有可能吗,那该怎么做? I tried with ng-change but it is not getting. 我试过ng-change但是没有得到。

Html code: Html代码:

<input type="text" ng-model="somvar" ng-change="someFunc(somvar)" />

<div id="dropdown" class="dropdownClass">
    <ul class="predtsct">
        <li class="prfdwn" ng-repeat="list in countries| filter: somevar" ng-click="countryIdFunc(countriesLst.countryId)">{{list.countryName}}</li>
    </ul>
</div>

From the fiddle I posted in the comments: A dropdown works no differently that an ordinary list, except that the styling is different. 从我在评论中发布的小提琴:一个普通列表的下拉列表没有什么不同,除了样式不同。 This example uses Bootstrap to do the dropdown, but you can find many other examples if you google them. 此示例使用Bootstrap执行下拉列表,但如果您使用它们,则可以找到许多其他示例。

Working version: https://jsfiddle.net/jorgthuijls/6gvp2z9h/ 工作版本: https//jsfiddle.net/jorgthuijls/6gvp2z9h/

This is the entire view: 这是整个视图:

<div ng-controller="MyCtrl">
  city: <input type="text" ng-model="search.city">

  <div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
      Dropdown
      <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
      <!-- the 'search' bit in the filter maps to the 'search' variable on $scope -->
      <li ng-repeat="user in users | filter:search:strict">{{user.name}}, {{user.city}}</li>
    </ul>
  </div>
</div>

The controller is also pretty small: 控制器也很小:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
  $scope.search = {};
  $scope.search.city = 'new york';
  $scope.users = [{
    name: 'bob',
    city: 'hong kong'
  }, {
    name: 'jack',
    city: 'new york'
  }, {
    name: 'john',
    city: 'new hampshire'
  }]
}

您可以使用Typeahead.js,它可以非常灵活地控制下拉列表并对其进行过滤。

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

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