大家好我正在使用angularjs来显示表格中的记录列表。我面临的困难是按升序显示表格,我使用了我需要显示的字母记录(X),表格中有第一条记录。
让我给你一个html页面。
<table class="table table-bordered">
<thead>
<tr>
<th ng-click="sort('bucket')" th-sort by="order">Bucket<span class="sortorder descending" ng-hide="(sortKey=='bucket' && reverse==true)"></span>
<span class="sortorder" ng-hide="(sortKey=='bucket' && reverse==false)"></span>
</th>
<th ng-click="sort('productCode')" th-sort by="order">Product Code<span class="sortorder descending" ng-hide="(sortKey=='productCode' && reverse==true)"></span>
<span class="sortorder" ng-hide="(sortKey=='productCode' && reverse==false)"></span>
</th>
<th ng-click="sort('countOfAllocatedAccount')" th-sort by="order">Allocated
<span class="sortorder descending" ng-hide="(sortKey=='countOfAllocatedAccount' && reverse==true)"></span>
<span class="sortorder" ng-hide="(sortKey=='countOfAllocatedAccount' && reverse==false)"></span>
</th>
<th ng-click="sort('countOfCollectedAccount')" th-sort by="order">Collected
<span class="sortorder descending" ng-hide="(sortKey=='countOfCollectedAccount' && reverse==true)"></span>
<span class="sortorder" ng-hide="(sortKey=='countOfCollectedAccount' && reverse==false)"></span></th>
<th ng-click="sort('sumOfArrearsOfAllocatedAmount')" th-sort by="order">Total Allocated Amount
<span class="sortorder descending" ng-hide="(sortKey=='sumOfArrearsOfAllocatedAmount' && reverse==true)"></span>
<span class="sortorder" ng-hide="(sortKey=='sumOfArrearsOfAllocatedAmount' && reverse==false)"></span>
</th>
<th ng-click="sort('sumOfCollectedAmount')" th-sort by="order">Total Collected Amount
<span class="sortorder descending" ng-hide="(sortKey=='sumOfCollectedAmount' && reverse==true)"></span>
<span class="sortorder" ng-hide="(sortKey=='sumOfCollectedAmount' && reverse==false)"></span></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in view_data">
<td><span ng-hide="view_data[$index-1].bucket == item.bucket">{{item.bucket}}</span></td>
<td>{{item.productCode}}</td>
<td>{{item.countOfAllocatedAccount}}</td>
<td>{{item.countOfCollectedAccount}}</td>
<td><span>{{item.sumOfArrearsOfAllocatedAmount | currency:"₹"}}</span></td>
<td>{{item.sumOfCollectedAmount | currency:"₹"}}</td>
</tr>
</tbody>
</table>
让我告诉你输出:
这是我的jsfiddle:http://jsfiddle.net/CvKNc/152/
如图所示,存储桶值X位于表中的最后一个值中。但是,我需要在表格的第一行显示它,其余的数字应按升序设置。我被困在这里,请有人帮我解决这个问题。
答案 0 :(得分:1)
//creating an application module
var myAppModule = angular.module("myApp", []);
myAppModule.controller("MyCtrl", function($scope, $http,$filter){
var jsonData = [
{
"bucket": ">120",
"productCode": "SBML",
"countOfAllocatedAccount": 640
},
{
"bucket": ">120",
"productCode": "SBHL",
"countOfAllocatedAccount": 1391
},
{
"bucket": "1-30",
"productCode": "SBHL",
"countOfAllocatedAccount": 1081
},
{
"bucket": "1-30",
"productCode": "SBML",
"countOfAllocatedAccount": 408
},
{
"bucket": "1-30",
"productCode": "SBML",
"countOfAllocatedAccount": 998
},
{
"bucket": "X",
"productCode": "SBML+",
"countOfAllocatedAccount": 93
}
];
$scope.products = $filter('orderByValue')(jsonData);
});//end controller
myAppModule.filter('orderByValue', function() {
return function(items, field) {
var filtered = [],filteredX = [];
angular.forEach(items, function(item) {
if(item.bucket=="X")
{
filteredX.splice(0, 0, item);
}else if(item.bucket.indexOf(">") !== -1) {
filtered.push(item);
}else
{
filteredX.push(item);
}
});
angular.forEach(filtered, function(item) {
filteredX.push(item);
});
return filteredX;
};
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
<body ng-app="myApp">
<div ng-controller="MyCtrl">
<table border="1">
<tr>
<th>Bucket</th>
<th>PRODUCT_CODE</th>
<th>Allocated #</th>
</tr>
<tr ng-repeat="p in products ">
<td ><span ng-show="products[$index-1].bucket != p.bucket">{{p.bucket}}</span></td>
<td><span>{{p.productCode}}</span></td>
<td><span>{{p.countOfAllocatedAccount}}</span></td>
</tr>
</table>
</div>
</body>
</html>
&#13;