Angularjs:搜索过滤器在 table 的另一行上不起作用,这是函数
Angularjs: Search filter is not working on the other row of table which is function
我正在写代码。这里我用 ng-repeat 做了一个 table。我使用 PHP 从 MySQL 数据库中获取数据。搜索过滤器不适用于 calcTotal(names)。
当我搜索一些关键字时,结果显示正确,但支付的总费用保持不变。过滤器无法正常工作 row.Anyone 知道如何在不同的行或函数上应用相同的过滤器。提前谢谢
<script>
var app= angular.module("myapp",[]);
app.controller("mycontroller",function ($scope, $http)
//get data from database
{
$http.get("getData.php").then(function (response) {
$scope.names = response.data.records;});
//subtraction between dates
$scope.CurrentDate=new Date();
$scope.calDate = function(date1, date2){
var dateOut1 = new Date(date1);
var dateOut2 = new Date(date2);
var timeDiff = Math.abs(dateOut2.getTime() -
dateOut1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
return diffDays;
};
$scope.getToday = function(){
return new Date();
}
//total fees paid
$scope.calcTotal = function(names){
var sum = 0;
for(var i = 0 ; i<names.length ; i++){
sum = sum + names[i].fpaid;
}
return sum;
};
});
</script>
<body ng-app ="myapp" ng-controller="mycontroller">
<p>
Search Here: <input type="text" placeholder="Search" ng-
model="searchText">
</p>
<table border=1>
<tr>
<th >Name of student </th>
<th >Course Name </th>
<th >Course fees </th>
<th ></th>
<th>Balance Fees</th>
<th>Start Date of Course </th>
<th >End date of course</th>
<th> No of days </th>
<th>No of Days remaining </th>
</tr>
<tr ng-repeat="x in names | filter: searchText | orderBy:sortColumn">
<td>{{x.Bank}}</td>
<td>{{x.cname}}</td>
<td >{{x.cfees}}</td>
<td>{{x.fpaid }}</td>
<td ng-model="bfess">{{x.cfees-x.fpaid}}</td>
<td>{{x.sdate | date:'dd-MM-yyyy'}}</td>
<td>{{x.edate | date:'dd-MM-yyyy'}}</td>
<td>{{calDate(x.edate,x.sdate)}}</td>
<td>{{calDate(x.edate,getToday()| date:'dd-MM-yyyy')}}</td>
</tr>
<tr>
<td>Total fess paid by students: {{ calcTotal(names) }}</td>
</tr>
</table>
这是因为您对 calcTotal 使用的是原始数组,而不是过滤后的数组。您还可以在控制器中使用过滤器来计算过滤后的总数。
将 filterFilter 注入您的控制器
controller('mycontroller', ['$scope', '$http','filterFilter', function FilterController($scope, $http,filterFilter)
并在计算前过滤数组
$scope.calcTotal = function(names){
var filteredArray = filterFilter(names, $scope.searchText);
var sum = 0;
for(var i = 0 ; i<filteredArray.length ; i++){
sum = sum + filteredArray[i].fpaid;
}
return sum;
};
我正在写代码。这里我用 ng-repeat 做了一个 table。我使用 PHP 从 MySQL 数据库中获取数据。搜索过滤器不适用于 calcTotal(names)。 当我搜索一些关键字时,结果显示正确,但支付的总费用保持不变。过滤器无法正常工作 row.Anyone 知道如何在不同的行或函数上应用相同的过滤器。提前谢谢
<script>
var app= angular.module("myapp",[]);
app.controller("mycontroller",function ($scope, $http)
//get data from database
{
$http.get("getData.php").then(function (response) {
$scope.names = response.data.records;});
//subtraction between dates
$scope.CurrentDate=new Date();
$scope.calDate = function(date1, date2){
var dateOut1 = new Date(date1);
var dateOut2 = new Date(date2);
var timeDiff = Math.abs(dateOut2.getTime() -
dateOut1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
return diffDays;
};
$scope.getToday = function(){
return new Date();
}
//total fees paid
$scope.calcTotal = function(names){
var sum = 0;
for(var i = 0 ; i<names.length ; i++){
sum = sum + names[i].fpaid;
}
return sum;
};
});
</script>
<body ng-app ="myapp" ng-controller="mycontroller">
<p>
Search Here: <input type="text" placeholder="Search" ng-
model="searchText">
</p>
<table border=1>
<tr>
<th >Name of student </th>
<th >Course Name </th>
<th >Course fees </th>
<th ></th>
<th>Balance Fees</th>
<th>Start Date of Course </th>
<th >End date of course</th>
<th> No of days </th>
<th>No of Days remaining </th>
</tr>
<tr ng-repeat="x in names | filter: searchText | orderBy:sortColumn">
<td>{{x.Bank}}</td>
<td>{{x.cname}}</td>
<td >{{x.cfees}}</td>
<td>{{x.fpaid }}</td>
<td ng-model="bfess">{{x.cfees-x.fpaid}}</td>
<td>{{x.sdate | date:'dd-MM-yyyy'}}</td>
<td>{{x.edate | date:'dd-MM-yyyy'}}</td>
<td>{{calDate(x.edate,x.sdate)}}</td>
<td>{{calDate(x.edate,getToday()| date:'dd-MM-yyyy')}}</td>
</tr>
<tr>
<td>Total fess paid by students: {{ calcTotal(names) }}</td>
</tr>
</table>
这是因为您对 calcTotal 使用的是原始数组,而不是过滤后的数组。您还可以在控制器中使用过滤器来计算过滤后的总数。
将 filterFilter 注入您的控制器
controller('mycontroller', ['$scope', '$http','filterFilter', function FilterController($scope, $http,filterFilter)
并在计算前过滤数组
$scope.calcTotal = function(names){
var filteredArray = filterFilter(names, $scope.searchText);
var sum = 0;
for(var i = 0 ; i<filteredArray.length ; i++){
sum = sum + filteredArray[i].fpaid;
}
return sum;
};