0

I know there are several similar topics already but I found none that really matches my problem.

When opening my AngularJS app/website I am loading two arrays of objects (both are of the same type). One list contains all possible values (called sources here) and the other list is a selection of elements from the first.

My goal is display all sources as checkboxes. The ones from the second list have to be preselected, the rest not. Now the user can select/deselect checkboxes. If he does so I need to inform the server (with the source.id).

What I got so far:

exampleApp.controller('testController', [ '$scope', '$http', function($scope, $http) { $scope.sources = []; $scope.selectedSources = []; $scope.changeSource = function(source) {...}; }) 

and

<div ng-repeat="source in sources"> <input type="checkbox" name="source.name" value="{{source.id}}" ng-model="??" ng-change="changeSource(source.id)" > {{source.name}} </div> 

What I can't figure out is how I can get ng-model to preselect the right checkboxes and how to get the new (and old) values to changeSource(). Is there an elegant way of doing that?

Example (Pseudo code only):

Sources = [{id=1, name=test1},{id=2, name=test2}, ...] SelectedSources = [{id=2, name=test2}] 

Now what I need are checkboxes like this:

[ ] test1 [x] test2 

where all elements from sources are checkboxes and the ones from selectedsources are preselected. Changes of the selection can be stored in selected sources (as objects) and have to trigger my changeSource() function so that I can inform my server.

1
  • The easiest way to deal with this is to use a single array with an extra field for each item that corresponds to it's selected status. is that something that would work in your situation?
    – Claies
    CommentedJun 22, 2017 at 21:28

3 Answers 3

1

Set the selected/unselected state in a property inside each of the objects in Sources array(initialize it based on whats present in selectedArray)

$scope.sources.forEach(function(source) { source.selected = isSelected(source); }) function isSelected(selectedSource) { return !!$scope.selectedSources.find(function(s) { return s === selectedSource || s.id == selectedSource.id; }) } 

Here's a working plunker link

1
  • In the end this seemed to be the cleanest answer. I implemented it pretty much exactly that way. Thank you, and thanks for all the other answers.
    – CodeO
    CommentedJun 23, 2017 at 20:32
0

I didn't understood your question very well, but if i'm not mistaken, you want to fill the second collection only with the selected items from the first one, right? If it's the case, you could turn your second collection into a returning function with a filter of the first inside, as follows:

In your controller:

exampleApp.controller('testController', [ '$scope', '$http', function ($scope, $http) { $scope.sources = []; /* ... */ $scope.getSelectedSources = function () { return $scope.sources.filter(function (val) { return val.selected; }); }; }) 

In your template:

<div ng-repeat="source in sources"> <input type="checkbox" name="source.name" value="{{source.id}}" ng-model="source.selected" ng-change="changeSource(source.id)" > {{source.name}} </div> <div ng-repeat="source in getSelectedSources()"> <input type="checkbox" name="source.name" value="{{source.id}}" ng-model="source.selected" ng-change="changeSource(source.id)" > {{source.name}} </div> 
3
  • Thank you. What I want to do is display a checkbox for every object in the first array and check (select) every checkbox of which the item is also in the second array.
    – CodeO
    CommentedJun 22, 2017 at 20:53
  • Sorry @CodeO. Can you provide an example of what you are trying to achieve (an image, template or something like that)?CommentedJun 22, 2017 at 20:58
  • No problem, still thanks for the help. I added an example to my question above
    – CodeO
    CommentedJun 22, 2017 at 21:02
0

Hi this may be help you to get new & old value.

$scope.$watch('sources', function (oldval, newval) { console.log(oldval + newval); }); 
1
  • Thank you, but the values in the sources array should not really change. I only want to display checkboxes for each item.
    – CodeO
    CommentedJun 22, 2017 at 20:54

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.