Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS checkbox checking when in ng-repeat

I know the solution is not very pretty, but it's needed for the template I'm working in.

Now it works, and it shows me the "Added" span-element when the products was in the list.

<section ng-repeat="product in products">
    <label>
        <input type="checkbox" 
            checklist-model="foobar.products" 
            checklist-value="product.id">
        {{ product.name }}
    </label>

    <div ng-repeat="current_item in my.profile.items">
        <span ng-show="product.id==current_item.id">Added</span>
    </div>
</section>

What I want, it to check the checkbox to checked, when the "Added" text also appeared behind the checkbox.

I tried doing it with:

<ng-checked="{{my.profile.items.array.thing}}">

But that's not working. Because the products in an Array like:

[{       
    id: 1, name:"Trinity",
    id: 2, name:"Van Gogh",
    id: 3, name:"Bonaqua",
}]

And the my.profile.items is an array with more info then above. Because it's a many-to-many relation where I stored it.

Is there even a way to do this? I don't mind a dirty solution :P

I tried this:

// NG-check function
$scope.checkStoredValues = function(my) {

    // Loop trought my current stored items
    angular.forEach(my.profile.items, function(value, key) {

        // Loop trough the array    
        angular.forEach(value, function(value, key) {
            console.error(key);

            // If key == 'id'
            if(key == 'id'){
                // Push the value to the Array of the checkboxes,
                // so it's checked 
                // # Docs: http://vitalets.github.io/checklist-model/
                console.info(value); // Return's: integer 
                foobar.products.push(value); // Needs more then an integer I guess..

            }
        });
    });

};

This returns: TypeError: Cannot read property 'push' of undefined

like image 299
user1867254 Avatar asked Aug 20 '15 19:08

user1867254


People also ask

How to find a checkbox is checked in AngularJS?

In AngularJS with the model, it is easier to maintain values across elements and to define the model ng-model directive is been used. To find a checkbox is been checked or not check the defined model. If it is TRUE means checkbox is been checked. In this tutorial, I will show how you can retrieve single or multiple checked checkboxes.

How to bind array list data to checkboxes in AngularJS?

Following is the result of using checkboxes in angularjs applications. By using ng-repeat directive in angularjs we can bind array list values to checkboxes. Following is the example of binding array list data to checkboxes in angularjs applications.

How to populate checkboxlist from a JSON array using ng-repeat?

The ng-repeat directive has been applied to the HTML DIV in order to populate CheckBoxList i.e. List of multiple CheckBoxes from a JSON array. The ng-model attribute of the CheckBox has been assigned the Selected property. Thus whenever a CheckBox is selected, the Selected property for the corresponding object in the JSON array will be set to TRUE.

What is ng-true-value in Angular 2?

The directive “ng-true-value” is assigned to each checkbox (just like ng-model) and has an item each. This item description is sent to the expression when selected.


3 Answers

Add this function to your controller:

$scope.isChecked = function(product) {
  var items = $scope.my.profile.items;
  for (var i=0; i < items.length; i++) {
    if (product.id == items[i].id)
      return true;
  }

  return false;
};

And for your html use

<section ng-repeat="product in products">
    <label>
        <input type="checkbox" 
               ng-checked="isChecked(product)">
        {{ product.name }}
    </label>

    <div ng-repeat="current_item in my.profile.items">
        <span ng-show="product.id==current_item.id">Added</span>
    </div>
</section>
like image 172
bumpy Avatar answered Oct 11 '22 04:10

bumpy


Another trick that you can do is create a view model specific for your need.

So for example you have the array of products and items within the profile.

In the controller, you could do something like:

 $scope.products.forEach(function(p){
       var items = $scope.my.profile.items;
       var wasAdded = false;
       for (var i=0; i < items.length; i++) {
           if (product.id == items[i].id)
                 wasAdded =  true;
                 i = items.length;
        }
       $scope.productsViewModels.push({product:p, added:wasAdded});
 });

Now that you have created your data as needed in the view, then you can simple do:

 <section ng-repeat="p in productsViewModels">
     <label>
         <input type="checkbox" 
             ng-checked = "p.added"
             checklist-model="foobar.products" 
             checklist-value="p.product.id">
              {{ p.product.name }}
     </label>

     <span ng-show="p.added">Added</span>
 </section>

When I have combined data from multiple sources I prefer to process it before in the controller and create a viewModel that will makes my life easy in the view.

like image 37
Andrés Soto Avatar answered Oct 11 '22 04:10

Andrés Soto


you need to implement something like this AngularJs ng-checked using a function

you just need to implement your own logic based on your many to many product array and return true or false accordingly.

like image 2
Syed Ekram Uddin Avatar answered Oct 11 '22 04:10

Syed Ekram Uddin