[英]Angularjs orderby objects keys
So, I'm trying to create a sorting list in angularjs. 所以,我试图在angularjs中创建一个排序列表。 Once a person clicks on the name of the value it updates the dom and show the data accordingly.
一旦用户单击值的名称,它将更新dom并相应地显示数据。 Trying to orderby the value of Bracelet, Charms, and so on... I know I could do something like this:
尝试按Bracelet,Charms等的值排序...我知道我可以做这样的事情:
$scope.sortOptions = ['Bracelets','Charms', 'Earring', ...]; //giving sorting options
$scope.sort = 'Bracelets'; //setting a default sortby item
$scope.active = function(x) {
return x === $scope.sort ? 'active' : '';
};
$scope.setSort = function(type){
$scope.sort = type.toLowerCase();
};
But this is just one object. 但这只是一个对象。 I will have multiple objects.
我将有多个对象。 Coming from the server.
来自服务器。
Here is my Category object: 这是我的类别对象:
{
"Pandora": {
"id": "1",
"s1": {
"d1": "Bracelets",
"d2": "Charms",
"d3": "Earrings",
"d4": "Jewelry",
"d5": "Necklaces",
"d6": "Pendants",
"d7": "Rings"
}
}
}
I've been reading that you can't use angularjs orderby without have an array of objects. 我一直在阅读,如果没有一组对象,就无法使用angularjs orderby。 Answered on StackOverFlow .
在StackOverFlow上回答。 So in my controller I have this code:
因此,在我的控制器中,我有以下代码:
$scope.catData = [];
Then I have a factory that goes to the server grabs the json. 然后我有一个工厂去服务器获取json。
dataFactory.getCat().then(function(res){
$scope.catData = res.data;
});
Here is what my HTML looks like 这是我的HTML外观
<li ng-repeat="(key, value) in catData">
<a href="#" data-id='{{value.id}}' class="anchor">{{key}}</a>
<ul class="sub" ng-class="{true: 'activeSlideF'}[value.id == 1]" >
<span ng-repeat="hi in value.s1 | orderBy:'hi':true">
<li>
<a href="#"><i class="fa fa-arrow-right"></i>{{hi}}</a>
</li>
</span>
</ul>
</li>
I'm thinking that when I set the $scope.catData to an array. 我在想将$ scope.catData设置为数组时。 Then set
$scope.catData = res.data
it's getting overridden. 然后设置
$scope.catData = res.data
它将被覆盖。 I could set $scope.catData = [res.data]
but I don't think that's the right way of doing it (or could be?). 我可以设置
$scope.catData = [res.data]
但我认为这样做不是正确的方法(或者可能是?)。
Thanks for the help! 谢谢您的帮助!
You can achieve this by using ng-click and ng-model to update a scope variable that is displayed in the DOM. 您可以通过使用ng-click和ng-model更新DOM中显示的范围变量来实现。
Below is an example and a JSFiddle: 下面是一个示例和一个JSFiddle:
HTML HTML
<div id="pandora" ng-app="myApp">
<div ng-controller="pandoraController">
<!--Some display text that updates via the $scope.hi var-->
<h2 ng-model="hi">{{hi}}</h2>
<ul ng-repeat="obj in data">
<!--Repeats the data and includes a function call for the value-->
<li ng-repeat="entry in obj.s1" ng-click="update(entry)"><button ng-value="{{ entry }}">{{ entry }}</button></li>
</ul>
</div>
</div>
JS JS
var app = angular.module('myApp', []);
// Your data object from before
var myData = "..Some JSON data...";
app.controller('pandoraController', ['$scope',
function($scope) {
$scope.hi = "Select a Pandora Product:";
$scope.data = myData;
$scope.update = function(val) {
$scope.hi = val;
}
}
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.