1

I have displayed products in divs and users can click on them, to select them. The problem i have is that only one product can be selected. I wan't to change this functionality to be multi selected and function should collect id's of products.

This is my HTML for products

<div class="product_item hit w_xs_full" ng-repeat="p in products track by $index" style="float: left; background-color: blue;">
    <figure class="r_corners photoframe type_2 t_align_c tr_all_hover shadow relative"
            ng-click="selectUserItems($index)" ng-class="{sel: $index == selected}">
        <!--product preview-->
        <img ng-src="images/products/{{p.images[0].thumbName}}" alt="{{p.name}}" class="tr_all_hover">
        <!--description and price of product-->
        <figcaption>
            <br>
            <h5 class="m_bottom_10"><b>{{p.name}}</b></h5>
            <p class="scheme_color f_size_large m_bottom_15" style="color:black;">Located in: <b>{{p.country}}</b></p>
            <a href="#/swap/{{p.mainCategorieLink}}/{{p.subCategoryLink}}/{{p.alias}}">
                <button class="button_type_4 bg_scheme_color r_corners tr_all_hover color_light mw_0 m_bottom_15">See item</button>
            </a>
        </figcaption>
    </figure>
</div>

And this is my controller function for selecting items

//select items
$scope.selectUserItems = function(index){
    $scope.selected = index;
};

Once item is selected background of div is colored blue

.sel {
    background-color:#5ACBFF;
}

So how to properly write controller function so you can select multiple divs and $scope.selectd variable collects ids of products.

4
  • So what have you tried? If you give us your code, we can actually help. Commented Feb 23, 2016 at 7:58
  • Maybe you could give us a minimal JSFiddle example ? Commented Feb 23, 2016 at 8:00
  • so far i have tried to push variables in array, but the problem was unselectings items. Once i fixed that issue, coloring divs blue didn't work so that's why i reverted the code to when i can select one Commented Feb 23, 2016 at 8:00
  • @Derlin i'm working on it Commented Feb 23, 2016 at 8:00

2 Answers 2

1

Currently, $scope.selected = index; holds only one index, you either need to make it an array and toggle the current selection:

Variant 1

 $scope.selected[index] = !$scope.selected[index];

and change your ngClass definition:

 ng-class="{sel: $index[selected]}"

Variant 2

or change your ngClick-function:

 ng-click="selectUserItems(p)"

And the selectUserItems function

 $scope.selectUserItems = function(item){
    item.selected = !item.selected;
 };

and change your ngClass definition:

 ng-class="{sel: p.selected}"
Sign up to request clarification or add additional context in comments.

1 Comment

wow, good job. I didn't even had a time to create pluker or fiddle. Thx
1

In the product object, add a field 'selected' to control whether the item is selected. And toggle the value of selected in ng-click:

<figure class="...." ng-init="p.isSelected = false"
        ng-click="p.isSelected = !p.isSelected" ng-class="{sel: p.isSelected}">

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.