【发布时间】:2017-10-17 17:25:51
【问题描述】:
我有一个从搜索栏中搜索项目的代码,但它在搜索的选择下拉列表中:
[]
但是我最近决定我不想要选择并且只想要查询过滤器,我将如何分隔代码?从选择到独立搜索。这是我使用的代码:
Javascript:
// AngularJS
var phonecatApp = angular.module('app', []);
phonecatApp.controller('ListCtrl', function ($scope) {
$scope.items = [{
'name': 'Item 1'
}, {
'name': 'Item 2'
}, {
'name': 'Account 3'
}, {
'name': 'Account 4'
}, {
'name': 'Item 5'
}, {
'name': 'Item 6'
}, {
'name': 'User 7'
}, {
'name': 'User 8'
}, {
'name': 'Item 9'
}, {
'name': 'Item 10'
}, {
'name': 'Item 11'
}, {
'name': 'Item 12'
}, {
'name': 'Item 13'
}, {
'name': 'Item 14'
}, {
'name': 'User 15'
}, {
'name': 'User 16'
}, {
'name': 'Person 17'
}, {
'name': 'Person 18'
}, {
'name': 'Person 19'
}, {
'name': 'Item 20'
}, ];
});
// jQuery
$('.dropdown-menu').find('input').click(function (e) {
e.stopPropagation();
});
HTML:
<div class="dropdown dropdown-scroll" ng-app="app">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">Select <span class="caret"> </span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1" ng-controller="ListCtrl">
<li role="presentation">
<div class="input-group input-group-sm search-control"> <span class="input-group-addon">
<span class="glyphicon glyphicon-search"></span>
</span>
<input type="text" class="form-control" placeholder="Query" ng-model="query"></input>
</div>
</li>
<li role="presentation" ng-repeat='item in items | filter:query'> <a href="#"> {{item.name}} </a>
</li>
</ul>
</div>
CSS:
.dropdown.dropdown-scroll .dropdown-menu {
max-height: 200px;
width: 60px;
overflow: auto;
}
.search-control {
padding: 5px 10px;
}
JSFiddle
【问题讨论】:
-
您愿意使用添加库吗?因为你需要的是 Typehead,而 Angular UI Bootstrap 有完美的指令。 angular-ui.github.io/bootstrap/#!#typeahead
-
只有当一切都失败时我才是
-
@Shantanu 如果你想回答,请接受并投票
标签: javascript jquery html css angularjs