这是一个示例代码,用户在文本框中写入一些数据并相应地过滤结果,但是如何通过单击按钮来过滤数据?
<div ng-app="myApp" ng-controller="MyController">
<label>Field:
<select ng-model="selectedFieldName">
<option value="">--Select Account--</option>
<option ng-repeat="(fieldname,fieldvalue) in customer[0]" ng-value="fieldname | uppercase">{{fieldname | uppercase}}</option>
</select>
</label>
<label>data: <input ng-model="searchText"></label>
<table class="table table-striped table-bordered">
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>Salary</td>
<td>Date of Birth</td>
<td>City</td>
<td>Phone</td>
</tr>
<tr ng-repeat="item in customer | filter:SearchList ">
<!-- orderBy:'$index':false -->
<td>{{ item.id }}</td>
<td>{{ item.firstname }}</td>
<td>{{ item.lastname }}</td>
<td>{{ item.salary }}</td>
<td>{{ item.dob }}</td>
<td>{{ item.city }}</td>
<td>{{ item.phone }}</td>
</tr>
</table>
</div>
;var myApp = angular.module('myApp', []);
myApp.controller('MyController', function MyController($scope) {
$scope.selectedFieldName='';
$scope.searchText='';
$scope.SearchList = function(row) {
if ($scope.selectedFieldName && $scope.searchText) {
var propVal = row[$scope.selectedFieldName.toLowerCase()]+ '';
if (propVal) {
return propVal.toUpperCase().indexOf($scope.searchText.toUpperCase()) > -1;
} else {
return false;
}
}
return true;
};
$scope.customer = [
{
'id': 1,
'firstname': 'Tridip',
'lastname': 'Bhattacharjee',
'salary' : 15000,
'dob': '05/09/2013',
'city': 'kolkata',
'phone': '033 2589 7415'
},
{
'id': 2,
'firstname': 'Arijit',
'lastname': 'Banerjee',
'salary' : 25000,
'dob': '01/09/2010',
'city': 'Bihar',
'phone': '033 2589 9999'
},
{
'id': 3,
'firstname': 'Dibyendu',
'lastname': 'Saha',
'salary' : 20000,
'dob': '06/09/2011',
'city': 'Rachi',
'phone': '033 2589 3333'
},
{
'id': 4,
'firstname': 'Bisu',
'lastname': 'Das',
'salary' : 5000,
'dob': '05/01/2009',
'city': 'Silchar',
'phone': '033 2589 2222'
},
{
'id': 5,
'firstname': 'Soumyajit',
'lastname': 'Kar',
'salary' : 12000,
'dob': '09/08/2011',
'city': 'kanpur',
'phone': '033 3333 1894'
}
];
})
请查看我的代码并告诉我是否添加了一个按钮,然后当用户在文本框中写入数据进行搜索后单击按钮时,如何触发自定义过滤器。寻找指引。感谢
答案 0 :(得分:3)
您可以在点击时创建过滤器对象。
<input ng-model="searchText"></label>
<button ng-click="submitFilter()" class="search-button">Submit</button>
然后在你的控制器中:
$scope.submitFilter(){
$scope.SearchList = $scope.searchText;
}