[英]Sorting number field in Angular
我有一個由Angular填充的數據表,但是'rate'字段正在排序,就好像它是一個字符串而不是一個數字。 我知道它需要轉換,但不確定如何執行,尤其是通過AJAX填充列表時。
角度的
app.controller('itemsCrtl', function ($scope, $http, $timeout) {
function returnItems(f){
$http.get('angular-ajax/vcodes.php?filter=' + f).success(function(data){
$scope.list = data;
$scope.currentPage = 1; //current page
$scope.entryLimit = 50; //max no of items to display in a page
$scope.filteredItems = $scope.list.length; //Initially for no filter
$scope.totalItems = $scope.list.length;
});
}
$scope.changeFilter = returnItems; // provide function for button click
returnItems('all'); // initialize default filter (all contacts)
$scope.sort_by = function(predicate) {
$scope.predicate = predicate;
$scope.reverse = !$scope.reverse;
};
});
具有列排序標題的表
<table>
<thead>
<th>Code <a ng-click="sort_by('code');"><i class="glyphicon glyphicon-sort"></i></a></th>
<th>Description <a ng-click="sort_by('description');"><i class="glyphicon glyphicon-sort"></i></a></th>
<th>Rate % <a ng-click="sort_by('rate');"><i class="glyphicon glyphicon-sort"></i></a></th>
</thead>
<tbody>
<tr ng-repeat="data in filtered = (list | orderBy : predicate :reverse)">
<td>{{data.code | decodeURI}}</td>
<td>{{data.description | decodeURI}}</td>
<td>{{data.rate | number:2}}</td>
</tr>
</tbody>
</table>
orderBy 文檔說:
注意:如果您發現數字未按預期進行排序,請確保將其實際保存為數字而不是字符串。
因此,我無法訪問您的數據集,我認為這就是問題所在。 如果您無法控制API中的數據,則可以在http get返回函數中對其進行處理:
$http.get('angular-ajax/vcodes.php?filter=' + f).success(function(data){
$scope.list = data;
$scope.list.forEach(function(item, index){
$scope.list[index].rate = Number(item.rate);
});
. . .
});
取決於您對碼率字段只有多少數字或數字字符串的信心,您可以在循環中引入錯誤處理
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.