ng-repeat directive sort the data when using (key, value)

前端 未结 7 2111
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 09:40

I have a code something like this with ng-repeat = \"(key,value) in data\". In Controller:

  $scope.Dates = {\"Today\":\"30\",
                  \"This Wee         


        
相关标签:
7条回答
  • 2020-12-08 09:45

    Well, as explained in Angular's documentation for ngRepeat

    https://docs.angularjs.org/api/ng/directive/ngRepeat#iterating-over-object-properties

    when using it to iterate over the properties of an object, it will not work

    The built-in filters orderBy and filter do not work with objects, and will throw an error if used with one.

    The best solution in this case I think that is using arrays instead of an object and its properties. Therefore, just convert it to an array, iterating the properties of your object and pushing to the new array for example. After all, I think that would be the natural choice to a collection, instead of a single object with many properties.

    0 讨论(0)
  • 2020-12-08 09:47

    This is fixed as of Angular 1.4.

    See details here:

    Previously, the order of items when using ngRepeat to iterate over object properties was guaranteed to be consistent by sorting the keys into alphabetic order.

    Now, the order of the items is browser dependent based on the order returned from iterating over the object using the for key in obj syntax.

    It seems that browsers generally follow the strategy of providing keys in the order in which they were defined...

    0 讨论(0)
  • 2020-12-08 09:51

    I was able to sort an Object of Strings by Key alphabetically like this with ng-repeat!

    In my controller:

    $scope.build_config = {
      build_path :  "/x/eng/build",
      arch : "x86",
      variant : "debug",
      run_method : "boot", 
    };
    
    $scope.get_keys = function (obj) {
      if (obj)
        return Object.keys(obj);
    };
    

    In my HTML:

    <tr ng-repeat="key in get_keys(build_config) | orderBy:'toString()'">
          <td> {{key}} </td>
          <td> {{selected.build_config[key]}} </td>
    </tr>
    
    0 讨论(0)
  • 2020-12-08 09:52

    This is limitation of JavaScript not Angular.

    From ECMAScript Third Edition:

    4.3.3 An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

    From ECMAScript Language Specification

    The [...] order of enumerating the properties [...] is not specified.

    Angular sorts object keys explicitly in order to provide at least some sort of persistent behavior.

    Workaround is to iterate over extracted keys:

    <div ng-repeat="key in keys(Dates)">
      {{key}} ==> {{Dates[key]}}
    </div>
    
    $scope.keys = function(obj){
      return obj? Object.keys(obj) : [];
    }
    
    $scope.Dates = {
      "Today":"30",
      "This Week":"42",
      "This Month": "Oct",
      "This Quarter" : "Bad",
      "This Year" : 2013
    };
    
    0 讨论(0)
  • 2020-12-08 09:58

    There is actually an answer: it's called orderBy and not sort:

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

    {{ orderBy_expression | orderBy : expression : reverse}}

    <tr ng-repeat="friend in friends | orderBy:'-age'">
          <td>{{friend.name}}</td>
          <td>{{friend.phone}}</td>
          <td>{{friend.age}}</td>
        </tr>
    

    Your list just needs to be a list, and you might need an index to know set order

    $scope.Dates = [{index:1, "Today":"30"},
                      {index:2,"This Week":"42"},
                      {index:3,"This Month": "Oct"},
                      {index:4,"This Quarter" : "Bad"},
                      {index:5,"This Year" : 2013}]
    

    and then

    <tr ng-repeat="(key, value) in Dates | orderBy:'index'">
              <td>{{key}}</td>
              <td>{{value}}</td>
             </tr>
    
    0 讨论(0)
  • 2020-12-08 10:01

    Object.keys doesn't reserve the order - FYI https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

    // array like object with random key ordering

    var an_obj = { 100: 'a', 2: 'b', 7: 'c' };
    console.log(Object.keys(an_obj)); // console: ['2', '7', '100']
    

    I convert it into an array of object

    $scope.Dates = [
            { id: "Today",      value:"30" },
            { id: "This Week",  value:"42" },
            { id: "This Month", value: "Oct" },
            { id: "This Quarter", value : "Bad" },
            { id: "This Year",  value : 2013 }
        ];
    
    <li ng-repeat="date in Dates">{{date.id}} -> {{date.value}}</li>
    

    http://jsfiddle.net/2hqew68k/1/

    0 讨论(0)
提交回复
热议问题