0

I've got an array of values that i show with an ng-repeat. When i click over one of it, i add this value in another array. If already exists i remove it. It works well here. But i have a button that push all array in the second. It's working but i can push the whole array infite times even if a value already exists. Of course, if i check one or two value and then i push "Select all" it must select all values also the values already select with the single selection. By thge way this is the code with a jsfiddle:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.all_titles = [
        "Title 1",
        "Title 2",
        "Title 3",
        "Title 4"
        ];

    $scope.selection=[];

    $scope.getSelectedItem = function getSelectedItems(title) {
            var idx = $scope.selection.indexOf(title);
            // is currently selected
            if (idx > -1) {
                $scope.selection.splice(idx, 1);
            }
            // is newly selected
            else {
                if(Array.isArray(title)) {
                    for(var i=0;i<title.length;i++) {
                        $scope.selection.push(title[i]);
                    }
                } else {
                    $scope.selection.push(title);
                }
            }
        };
}

<div ng-controller="MyCtrl">
    <div>
        <button data-ng-click="getSelectedItem(all_titles)">
            Select all
        </button>
    </div>
    <div ng-repeat="title in all_titles">
        <a ng-click="getSelectedItem(title)">{{title}}</a>
    </div>

    <hr>
    <div>
        {{selection}}
    </div>
</div>

http://jsfiddle.net/HB7LU/20342/

2
  • is this what you want? jsfiddle.net/L1Lf8yau it's not quite clear how the select all button is supposed to work. Commented Nov 26, 2015 at 15:04
  • Not at all.. because in this way if you click in one and then in "Select all", it selects all except the one you selected before. Commented Nov 26, 2015 at 15:12

3 Answers 3

2

You scenario is not quite clear to me.

If you want the select all button to behave like all links are click, then this is your solution:

$scope.getSelectedItem = function getSelectedItems(title) {
            if(Array.isArray(title)) {
                for(var i=0;i<title.length;i++) {
                    $scope.pushIt(title[i]);
                }
            } else {
                $scope.pushIt(title);
            }

    };

$scope.pushIt = function pushIt(title) {
        var idx = $scope.selection.indexOf(title);
        // remove if already in array
        if (idx > -1) {
            $scope.selection.splice(idx, 1);
        } else {
            $scope.selection.push(title);
        }
    };

If you want the select all button to add the remaining items, then this is your solution:

$scope.getSelectedItem = function getSelectedItems(title) {

    if (Array.isArray(title)) {
        for (var i = 0; i < title.length; i++) {
            var idx = $scope.selection.indexOf(title[i]);
            // don't add if already in the array
            if (idx == -1) {
                $scope.selection.push(title[i]);
            }
        }
    } else {
        var idx = $scope.selection.indexOf(title);
        // is currently selected
        if (idx > -1) {
            $scope.selection.splice(idx, 1);
        } else {
            $scope.selection.push(title);
        }
    }

};
Sign up to request clarification or add additional context in comments.

1 Comment

your second solution is what i need!! Perfect thank you!
0

Your code is work, but you need to create a else

for(var i=0;i<title.length;i++) {
 var n_idx = $scope.selection.indexOf(title[i]);
 if( n_idx == -1){
   $scope.selection.push(title[i]);
 }else{
   $scope.selection.splice(n_idx, 1);
 }
}


var myApp = angular.module('myApp',[]);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {
    $scope.all_titles = [
        "Title 1",
        "Title 2",
        "Title 3",
        "Title 4"
        ];

    $scope.selection=[];

    $scope.getSelectedItem = function getSelectedItems(title) {
            var idx = $scope.selection.indexOf(title);
            // is currently selected
            if (idx > -1) {
                $scope.selection.splice(idx, 1);
            }
            // is newly selected
            else {
                if(Array.isArray(title)) {
                    console.log("YES");
                    for(var i=0;i<title.length;i++) {
                        var n_idx = $scope.selection.indexOf(title[i]);
                        if( n_idx == -1){
                            $scope.selection.push(title[i]);
                        }else{
                            $scope.selection.splice(n_idx, 1);
                        }
                    }
                } else {
                    $scope.selection.push(title);
                }
            }
        };
}

Comments

0

Netzach is right concerning the bug.

Also I would optimise your code by using hash instead of looping over the array. Then the code excluding/including the value will look like:

    if($scope.selection[title]){
        delete $scope.selection[title];
    } else {
        $scope.selection[title] = true;
    }

Here is jsfiddle to a working example:

http://jsfiddle.net/HB7LU/20348/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.