gpt4 book ai didi

javascript - Angular JS : Search with parameters defined by select

转载 作者:行者123 更新时间:2023-11-30 08:40:09 25 4
gpt4 key购买 nike

所以我有一个搜索框,其中包含一些我想用来限制搜索的参数。在我的示例中,这包括名字和姓氏。

What I'm trying to achieve

所以我一直在尝试这样实现它。这是下拉列表的代码。

<select id="usersearchby" ng-model="searchFilter" ng-change="selectAction()">
<option>Search By ...</option>
<option value="lastName">Last Name</option>
<option value="firstName">First Name</option>
</select>

这是输入搜索字段的代码。

<input type="search" ng-if="searchFilter != 'firstName' && searchFilter != 'lastName'" ng-model="searchText">
<input type="search" ng-if="searchFilter == 'firstName'" ngModel="searchText.first_name">
<input type="search" ng-if="searchFilter == 'lastName'" ngModel="searchText.last_name">

这是显示结果的 div 的代码。

<div class="block_line_item userlist_expanded" ng-repeat="user in users | filter: searchText">

但是这段代码不起作用,如果我只有一个输入搜索框来输入 first_name(没有条件输入),它就可以正常工作!我究竟做错了什么?有更好的方法吗?

我正在使用的 json 数据示例:

[{"role":"1","credentials":"","work_phone":"206–298–3059","last_name":"Doe","middle_name":"","suffix":"","title":"Mr.","mobile_phone":"1–877–932–4259","id":1,"fax":"135–678-6357","first_name":"John","email":"user1@gmail.com","username":"user1"},     {"role":"1","credentials":"","work_phone":"206–298–3059","last_name":"Doe","middle_name":"","suffix":"","title":"Mr.","mobile_phone":"1–877–932–4259","id":2,"fax":"135–678–6357","first_name":"Jane","email":"user22@gmail.com","username":"user2"}]

最佳答案

与其拥有三个文本框,不如拥有一个并在您的 Controller 中使用一个过滤器函数,它将被 ng-repeat 中的过滤器使用:

<input type="search" ng-model="searchText"/>

<div class="block_line_item userlist_expanded" ng-repeat="user in users | filter: filterUsers(searchText, searchFilter)">
{{user.first_name + ' ' + user.last_name}}
</div>

$scope.filterUsers = function(searchText, searchFilter) {
if (searchFilter === 'firstName') {
return function (user) { return user.first_name.match(searchText); }
}
else if (searchFilter === 'lastName') {
return function (user) { return user.last_name.match(searchText); }
}
else {
return function (user) { return user.first_name.match(searchText) || user.last_name.match(searchText); }
}
};

http://plnkr.co/edit/vYlBLiF555JLpwlnZIPR?p=preview

关于javascript - Angular JS : Search with parameters defined by select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27352590/

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