简体   繁体   English

AngularJS orderBy在选择框上无法正常工作。

[英]AngularJS orderBy on select boxes doesn't work as expected.

I am trying to convert an old PHP/JS project over to straight AngularJS. 我正在尝试将旧的PHP / JS项目转换为直接的AngularJS。

The issue I'm running into is that the orderBy:"name" on my select box is only sort of working. 我遇到的问题是我的选择框上的orderBy:"name"只能正常工作。

For example, in the plunker (below) if you open the building drop down, it appears to be in the correct alphabetical order. 例如,如果您打开建筑物的下拉菜单,则该建筑物的下拉菜单似乎处于正确的字母顺序(在下面)。 However, there's a few that are just randomly misplaced throughout the drop down. 但是,在整个下拉列表中,有一些只是随机放错了位置。

The other odd thing is that the JSON file is already in alphabetical order, so I'm not sure what the deal is. 另一个奇怪的是,JSON文件已经按字母顺序排列,所以我不确定这笔交易是什么。

I am new to AngularJS, so I know I'm missing something that's small. 我是AngularJS的新手,所以我知道我缺少一些小东西。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

Plunker: http://plnkr.co/edit/wHSERdfKLaYaaSMBph73 Plunker: http ://plnkr.co/edit/wHSERdfKLaYaaSMBph73

JSON Example: JSON示例:

{
    "id": 110,
    "name": "Administration Center",
    "address": null,
    "latitude": "41.256326",
    "longitude": "-95.973784",
    "content": "",
    "overlay": "g|xzFnywhQ?^^??j@M??^r@??_@K??kA",
    "url": "",
    "type_id": 3,
    "show_marker": 0,
    "show_label": 1,
    "custom_marker": "",
    "created_at": "2013-07-10 15:40:09",
    "updated_at": "2013-07-10 15:42:50",
    "color": "#08c"
}

Your plunker had some errors in the order of your dependancies (ie: jquery after angular, etc. There is an error message in the dev console). 随便的人有一些错误,具体取决于您的依存关系(即:jangle之后的jquery等)。在开发控制台中有一条错误消息。 When using angular and jquery, you have to include jquery first. 使用angular和jquery时,必须首先包含jquery。

<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="http://code.angularjs.org/1.2.0-rc.3/angular.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//cdn.jsdelivr.net/underscorejs/1.5.1/underscore-min.js"></script>

Here is your demo with the fixes: http://plnkr.co/edit/92zvkjP1cx1wBlGqwB4D?p=preview 这是您的修复示例: http : //plnkr.co/edit/92zvkjP1cx1wBlGqwB4D?p=preview

You do not need to order your data in the controller if you are using the orderBy filter. 如果使用orderBy过滤器,则无需在控制器中对数据进行排序。

html HTML

<div ng-repeat="(key,menu) in menus">
    <h1>{{typeIds[key]}}</h1>
    <select ng-model="selectedlocation" 
            ng-options="item.id as item.name for item in menu | orderBy:'name'">
    </select>
</div>

js JS

function menuController($scope, $http) {
    // probably not the best way to do this
    $scope.typeIds = {3: "Buildings", 6: "Parking (Fac/Staff)", 7: "Parking (Public)", 8: "Parking (Student)", 11: "Landmarks"};

    $http.get('locations.json').then(function(res) {
        $scope.locations = res.data;
        $scope.menus = _($scope.locations).groupBy('type_id');

        /*
        // This is not needed anymore
        //Convert to array so it sorts correctly on the view side. 
        $scope.orderedMenus = [];
        _($scope.menus).each(function(data, key) {
            $scope.orderedMenus.push({"key" : key, "data" : data})
        });
        */
    });
}

http://plnkr.co/edit/yRR34nwCBIh6cAiTXIBP?p=preview http://plnkr.co/edit/yRR34nwCBIh6cAiTXIBP?p=preview

It looks to me like ng-select just doesn't like to order on anything but the value (edit: it looks like that's not right exactly). 在我看来, ng-select只是不喜欢按值订购任何东西(编辑:看起来好像不太正确)。 So I did a bit of sorting in the controller with underscore and made a more explicit select: 因此,我在控制器上使用了下划线进行了一些排序,并做出了更明确的选择:

 <div ng-repeat="menu in orderedMenus | orderBy:'name'"><!-- orderBy might not be necessary here --> 
  <h1>{{typeIds[menu.key]}}</h1>
  <select ng-model="selectedlocation">
   <option ng-repeat="item in  menu" value="item.id">
    {{item.name}}
   </option>
  </select>     
 </div>

Which does the trick. 绝招。 Screenshot here . 屏幕截图在这里

Looks like there's a better solution posted here now. 看起来现在这里发布了一个更好的解决方案。 But, just so you know your options in these situations, you can write you own sort as orderBy can take a sort function, for example: 但是,只要您知道在这些情况下的选项,就可以编写自己的排序,因为orderBy可以采用排序功能,例如:

  <select ng-model="selectedlocation" ng-options="value.id as value.name for (key,value) in menu.data | orderBy:sortFunction"></select>  

If you add something like the following in your controller: 如果在控制器中添加如下内容:

$scope.sortFunction = function(item){
   console.log("Parameter: ", item.name);
}

You'll see you get the item name. 您会看到您得到的商品名称。 If you go this route, your job is to return a value from sortFunction() that when evaluated against the <, =, > operator produces whatever sort you'd like. 如果走这条路线,您的工作是从sortFunction()返回一个值,该值在针对<,=,>运算符进行评估时会产生所需的任何排序。

I'm not recommending this route- just mentioning it for completeness sake. 我不建议您使用此路线-出于完整性考虑而仅提及它。

http://docs.angularjs.org/api/ng.filter:orderBy http://docs.angularjs.org/api/ng.filter:orderBy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM