angularjs ng-repeat中的顺序错误

时间:2014-09-05 13:35:31

标签: javascript json angularjs

在我的小播放列表中:http://www.monde-du-rat.fr/zombieReport/popup.html#/radio

IDS订购不正确:1,10,11,2,3 ......

我的json在这里http://www.monde-du-rat.fr/zombieReport/resources/data/radio.json,我的html是:

            <div ng-repeat="song in songs.radio track by song.id | orderBy:'song.id*1'" class="list-group-item" id="{{song.id}}" ng-class="{ 'active' : songPlayed_name == song.name }">
            </div>

出了什么问题?

3 个答案:

答案 0 :(得分:2)

您可以使用https://github.com/petebacondarwin/angular-toArrayFilter

请在此处查看演示http://plnkr.co/edit/PrALxOJfmtcXUntbPmSz?p=preview

<div ng:controller="Main">
      <div ng:repeat="song in songs.radio | toArray | orderBy: 'id'">
        {{song.id}} {{song.title}}

      </div>

<强> JS:

 angular.module('app', ['angular-toArrayFilter'])

    .controller('Main', function Main($scope) {

      $scope.songs = {
        "radio": {
          "1": {
            "id": 1,
            "name": "coucou",
            "title": "\"Coucou, tu veux voir ma bite ?\" l'hymne officiel du MDR !",
            "vote": "7,5"
          },
          "2": {
            "id": 2,
            "name": "kissed",
            "title": "\"I kissed a rat\" - KitschNSniff",
            "vote": "9,5"
          },
          "3": {
            "id": 3,
            "name": "rats",
            "title": "\"It's Rats, It's Rats, It's Rat\" - KitschNSniff",
            "vote": "9,5"
          },
          "4": {
            "id": 4,
            "name": "merguez",
            "title": "\"Merguez-Partie\" - le tube culte",
            "vote": "8,5"
          },
          "5": {
            "id": 5,
            "name": "etchebest",
            "title": "\"Etchebest 1664\" - la pub culte",
            "vote": "8,5"
          },
          "6": {
            "id": 6,
            "name": "gitan",
            "title": "\"Gitan énervé\" - LE SANG DE VOS MORTS !",
            "vote": "7,5"
          },
          "7": {
            "id": 7,
            "name": "coral",
            "title": "\"Karl, Koraaal, Koarl, Karol\"",
            "vote": "5,5"
          },
          "8": {
            "id": 8,
            "name": "sax",
            "title": "\"Saxo Guy\" - musique idéale pour se détendre",
            "vote": "6,5"
          },
          "9": {
            "id": 9,
            "name": "longtime",
            "title": "\"Tomorow is a long time\" - Bob Dylan",
            "vote": "5,5"
          },
          "10": {
            "id": 10,
            "name": "blackbird",
            "title": "\"Blackbird Song\" - Lee DeWyze",
            "vote": "7,5"
          },
          "11": {
            "id": 11,
            "name": "crazy",
            "title": "\"Crazy\" - Ninet Tayeb & the Rose Band",
            "vote": "6,5"
          }
        }
      }

    })

答案 1 :(得分:1)

问题是你的id实际上是字符串,而不是数字。您可以正确格式化JSON(不用引号括起数字),也可以只遍历列表并将这些字符串解析为整数值。

angular.forEach($scope.songs.radio, function (song) {
  song.id = parseFloat(song.id, 10);
});

答案 2 :(得分:1)

您的代码存在两个问题:

  • 您不希望按song.idid
  • 对项目进行排序
  • 对您的项目进行排序,它们应该包含在数组中而不是对象中

我改变了两点,这是一个有效的傻瓜:http://plnkr.co/edit/CaU5TbFQTm9hVls5S3RG?p=preview