gpt4 book ai didi

javascript - 通过文本搜索和下拉列表进行 Angular 过滤

转载 作者:行者123 更新时间:2023-11-30 00:23:45 26 4
gpt4 key购买 nike

我遇到了麻烦,我似乎找不到答案。我正在尝试使用文本输入框和下拉菜单进行过滤。这是一个梦幻足球应用程序,可以为您提供一个想法。下面是一些代码

 <form class="form-inline search-width">
<input class="search form-control" type="text" ng-model="nameSearch.name">
<select class="form-control" ng-model="nameSearch.position">
<option value="All">All</option>
<option value="QB">QB</option>
<option value="RB">RB</option>
<option value="WR">WR</option>
<option value="TE">TE</option>
<option value="DEF">DEF</option>
<option value="K">K</option>
</select>
{{nameSearch.position}}
</form>
<ul class="list">

<li ng-repeat="list in playerlist"
ng-click="PlayerSelected($event, this)">Rank: {{list.Rank}} Pos: {{list.Position}} {{list.Team}}</br>
{{list.Last}}, {{list.First}} Bye: {{list.Bye}}</li>

</ul>

我对如何使用这两种输入进行搜索一无所知。下拉菜单应该只搜索位置值。输入框真的可以搜索任何东西。

最佳答案

自定义过滤器在您的情况下可能很有用。那我将如何解决这个问题。

过滤器:

angular.module('DraftBoard').filter('playersFilter', function () {
return function (input, filterObject) {
if (filterObject == undefined) { return input; }

var searchName = filterObject.name.toLowerCase();
var searchPosition = filterObject.position.toLowerCase();
var out = [];
if (input != undefined) {
for (var i = 0; i < input.length; i++) {

var firstName = input[i].First != undefined ? input[i].First.toString().toLowerCase() : '';
var lastName = input[i].Last != undefined ? input[i].Last.toString().toLowerCase() : '';
var team = input[i].Team != undefined ? input[i].Team.toString().toLowerCase() : '';
var position = input[i].Position != undefined ? input[i].Position.toString().toLowerCase() : '';

var filterCondition = ((searchPosition === 'all') || (position.indexOf(searchPosition) > -1))
&& ((searchName == '') || (firstName.indexOf(searchName) > -1) || (lastName.indexOf(searchName) > -1) || (team.indexOf(searchName) > -1));

if (filterCondition) {
out.push(input[i]);
}
}
}
return out;
};
});

在你的 Controller 中添加:

$scope.nameSearch = {
name: '',
position: 'All'
};

在 View 中这样使用它:

<div class="selectionlist">
<form class="form-inline search-width">
<input class="search form-control" type="text" ng-model="nameSearch.name">
<select class="form-control" ng-model="nameSearch.position">
<option value="All">All</option>
<option value="QB">QB</option>
<option value="RB">RB</option>
<option value="WR">WR</option>
<option value="TE">TE</option>
<option value="DEF">DEF</option>
<option value="K">K</option>
</select>
{{nameSearch.position}}
</form>
<ul class="list">

<li ng-repeat="list in playerlist | playersFilter:nameSearch "
ng-click="PlayerSelected($event, this)">
Rank: {{list.Rank}} Pos: {{list.Position}} {{list.Team}}</br>
{{list.Last}}, {{list.First}} Bye: {{list.Bye}}
</li>

</ul>

</div>

关于javascript - 通过文本搜索和下拉列表进行 Angular 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32218231/

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