lundi 19 octobre 2015

How to register order of checkboxes in AngularJS?

I currently have a list of checkboxes in my webapp. I want to show the order in which the checkboxes have been checked. So I wrote the code below.

$scope.updateNumbers = function(id, checked, inputs) {
    if (checked === true) {
        $scope.count += 1;
        var span = angular.element('#'+id+'-'+id);
        span.html($scope.count);
    } else {
        if ($scope.count != 0) {
            $scope.count -= 1;
        }

        for (index = 0; index < inputs.length; ++index) {
            var input = inputs[index];

            var span = angular.element('#'+test.id+'-'+test.id);
            if (span.html() > $scope.count || span.html() == $scope.count) {
                span.html(span.html()-1);
            }
        }

        var span = angular.element('#'+id+'-'+id);
        span.html($scope.count);
    }
}

And this HTML

<div class="list-view__body__row" ng-repeat="restaurant in restaurants">
    <div class="list-view__body__cell">
        <input type="checkbox" id="<% restaurant.id %>" value="" 
          class="input-field__checkbox--input" 
          ng-model="restaurant.checked" 
          ng-change="updateNumbers(restaurant.id, restaurant.checked, restaurants)">
        <label for="<% restaurant.id %>" 
          class="input-field__checkbox--label" 
          ng-bind-html="restaurant.name|html"></label>
    </div>
    <div class="list-view__body__cell">
        <div class="order__wrapper" ng-show="restaurant.checked">
            <span id="<% restaurant.id %>-<% restaurant.id %>">0</span>
        </div>
    </div>
</div>

In the current implementation, though, sometimes the number will go down to 0 or numbers will appear twice. It's not working correctly, so how can I improve on this code?




Aucun commentaire:

Enregistrer un commentaire