简体   繁体   中英

Angularjs order by not working properly

I'm facing a little problem with order by that i could not underdstand.
When i reoder the array i can see that every thing is working perfectly, but the problem is in the ng-click function of the list.
For example if i have this array:

$scope.ticketsEncour = [
  {_id: 1, vendeur: 'John Doe', date_ticket: '18:52', total: 50},
  {_id: 2, vendeur: 'Foo Bar', date_ticket: '12:12', total: 20},
  {_id: 3, vendeur: 'John Smith', date_ticket: '11:02', total: 10},
  {_id: 4, vendeur: 'Test Test', date_ticket: '05:10', total: 6000}
]

And this is ther reoder and click functions:

$scope.reverse = false;
$scope.orderItems = function (item) {
$scope.reverse = !$scope.reverse;
  if (item != undefined) {
    if (item == $scope.orderItem) {
      $scope.orderType = $scope.orderType == '+' ? '-' : '+';
    }
    else {
      $scope.orderItem = item;
      $scope.orderType = '+';
    }
  }

  $('.iconOrder').removeClass('up').removeClass('down');
  if ($scope.orderType == '+') {
    $('.iconOrder[name="' + $scope.orderItem + '"]').addClass('down')
  }
  else {
    $('.iconOrder[name="' + $scope.orderItem + '"]').addClass('up')
  }

  return $scope.orderType + $scope.orderItem;
}
$scope.goToCaissePage = function (id) {
  id = id.replace("app/caisse/", "");
  console.log(id);
};

And my Html Code

<ion-list class="list articlesList swipedList" show-delete="showDelete">
      <ion-item class="item item-button-right stable-bg">
        <span class="row">
          <span class="col col-20" ng-click="orderItems('vendeur')">
            <strong>Vendeur</strong>
            <div class="iconOrder" name="vendeur">
              <i class="icon ion-android-arrow-dropup"></i>
              <i class="icon ion-android-arrow-dropdown"></i>
            </div>
          </span>
          <span class="col col-20" ng-click="orderItems('date_ticket')">
            <strong>Heure</strong>
            <div class="iconOrder" name="date_ticket">
              <i class="icon ion-android-arrow-dropup"></i>
              <i class="icon ion-android-arrow-dropdown"></i>
            </div>
          </span>
          <span class="col col-10" ng-click="orderItems('total')">
            <strong>Total</strong>
            <div class="iconOrder" name="total">
              <i class="icon ion-android-arrow-dropup"></i>
              <i class="icon ion-android-arrow-dropdown"></i>
            </div>
          </span>
        </span>
      </ion-item>


      <ion-item class="item item-button-right pad-item" ng-repeat="ticket in ticketsEncour | orderBy : orderBy : orderItem:reverse  track by $index">
        <span class="row">
          <span class="col col-20 col-pad" ng-click="goToCaissePage('app/caisse/{{ticket._id}}')">
            <div>
              <a href="" style="color: #333; text-decoration: none">{{ ticket.vendeur }}</a>
            </div>
          </span>
          <span class="col col-20 col-pad" ng-click="goToCaissePage('app/caisse/{{ticket._id}}')">
            <div>
              <a href="" style="color: #333; text-decoration: none">{{ ticket.date_ticket | date:'HH:mm' }}</a>
            </div>
          </span>
          <span class="col col-10 col-pad" ng-click="goToCaissePage('app/caisse/{{ticket._id}}')">
            <div>
              <a href="" style="color: #333; text-decoration: none">{{ ticket.total.toFixed(2) }}€</a>
            </div>
          </span>
        </span>
      </ion-item>
    </ion-list>

When i click on any item of the ng-repeat it shows the right id in the console. But when i use the reorder i can is that the HTML has changed but the ng-click function render always the old one:

click on index 0 => console 1 (perfect)
After reoder with any type:
click on index 0 => console 1 (expected 3 for example)

Thanks

Update: changed the array name (copy cut error)
Updated with @Weijian suggestion

I suggest using the orderBy that uses the scope variables directly. This is also the method suggested by the angular docs.

js

$scope.reverse = false;
$scope.orderItems = function (item) {
    if( item == $scope.orderItem ){
        $scope.reverse = !$scope.reverse;
    }
    $scope.orderItem = item;
    $('.iconOrder').removeClass('up').removeClass('down');        
    if( $scope.reverse ){        
        $('.iconOrder[name="' + $scope.orderItem + '"]').addClass('down');
    }
    else {
        $('.iconOrder[name="' + $scope.orderItem + '"]').addClass('up');
    }
}

html

orderBy:orderItem:reverse

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

this way is cleaner since you avoid the mixed use of the orderItems function.

我解决了这个问题,我是在ng-repeat循环中track by $index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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