角度未排序数据
Angular not ordering data
我一直在尝试让angular js为我的数据排序。但遗憾的是,不起作用
我正在从firebase检索数据(https://boiling-inferno-4886.firebaseio.com/champion)并使用node js将其发送到控制器。
控制器代码
var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
// Pulls list of games
$scope.pullGame = function() {
console.log("Here");
$http.get('/getGameData').success(function(response) {
console.log(response);
$scope.championlist = response;
});
};
}]);
然后使用ng重复显示信息
<!DOCTYPE>
<html ng-app="myApp">
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<title>DevCha</title>
</head>
<body>
<div class="container" ng-controller="AppCtrl">
<input class="form-control" ng-model="game.name">
<button class="btn btn-info" ng-click="pullGame()">Update</button>
<h1>DevCha</h1>
<table class="table">
<thead >
<tr>
<th>Champion Name</th>
<th>Wins</th>
<th>Losses</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="champion in championlist|orderBy: 'name'">
<td>{{champion.name}}</td>
<td>{{champion.wins}}</td>
<td>{{champion.losses}}</td>
</tr>
</tbody>
</table>
</div>
<script src="controllers/controller.js"></script>
</body>
</html>
我尝试过使用firebase对数据进行排序,但也不起作用(这很奇怪)。但我很高兴,如果任何一个能为我分类(角或火球)
节点Js将数据推送到角度的代码
app.get('/getGameData', function(req, res){
var ref = new Firebase("https://boiling-inferno-4886.firebaseio.com/champion");
ref.once("value", function(snapshot) {
// Success
console.log(JSON.stringify(snapshot.val()));
res.json(snapshot.val());
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
//callback("Failure", null);
});
});
Sample_Data-1:{name:"Jim",赢:10,输:5},2:{name:"Fred",胜:8,输:6}(实际数据可在防火基地链接中找到)
tl:dr我需要对我的数据进行排序,我尝试使用firebase的内置方法对其进行排序,并使用angular的|orderBy,但两者都不起作用
我们确实需要查看这些数据,但由于我之前遇到过这个问题,以下是我的答案。
正如您在文档中看到的,orderBy只对数组进行排序。(key,value)语法适用于对象。也许将你的字典转换为数组就可以了。
如果没有,您可以为自己编写一个过滤器。看看这个过滤器:
app.filter('orderObjectBy', function () {
return function (items, field, reverse) {
var filtered = [];
angular.forEach(items, function (item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (a[field] > b[field] ? 1 : -1);
});
if (reverse) filtered.reverse();
return filtered;
};
});
然后你可以这样使用它:
<div class="item" ng-repeat="item in items | orderObjectBy:'position'">
</div>
顺便说一下,你可以这样更改订单:
<ul>
<li ng-repeat="item in items | orderObjectBy:'color':true">{{ item.color }</li>
</ul>
false为升序,true为降序
相关文章:
- 对角度数据表中括号内的数字进行排序
- 对损坏的子行进行排序的数据表
- 数据表排序但对行进行分组
- 角度未排序数据
- 数据表通过分析一列的值对其进行排序
- 排序数据表
- 对d3堆叠条形图的数据进行排序
- 如何对JQuery Handlebar生成的数据进行排序
- 数组数据排序类似mysql查询
- 使用JQuery对子表数据排序
- 数据排序应该在客户端还是服务器上完成
- a不同列列表的未定义或空引用的数据排序错误
- 日历数据排序 json 树
- 如何在ng-repeat中使用嵌套对象将数据排序为angular js中的JSON数据
- 使用knockout.js与json数据排序列表
- 如何突出显示页面上数据排序类型的菜单项
- dojox.grid.EnhancedGrid中的数据排序
- 在循环值时对JSON数据排序
- 帮助将DIV中的属性数据排序捕获为数组?Jquery/Javascript
- 如何从属性创建数组'数据排序'使用显示的所有分区的Jquery