Angular - Filtering with checkboxes - angularjs

I am new to Angular and i just cant wrap my head around that problem.
I have several checkboxes which will have a unique value each, like so:
<label ng-repeat="c in classes">
<input type="checkbox" ng-model="query" ng-true-value='{{c.name}}'>
<span>{{c.name}}</span>
</label>
Next i have this set of divs that should be filtered on a specific value like card.name , like so:
<div ng-repeat="card in cards | filter:query">
<h2>{{card.name}}</h2>
<p><em>{{card.text}}</em></p>
</div>
When checkbox with value 'Peter' is checked, all cards with card.name=Peter are shown. When checkbox 'Peter' and 'John' are shown all cards with card.name=Peter and =John are shown. How can i realize this?
I guess its more of a general concept/markup question...

You don't need to have two different variables, in fact that is the problem. Angular doesn't know how to tie them together.
The filter can look deep into the object for a specific key set to a specific value. In my example the filter looks for this particular selected attribute, only showing ones that are set to true Keeping everything in the same object keeps everything tied together.
<label ng-repeat="c in classes">
<input type="checkbox" ng-model="c.selected" />
<span>{{c.name}}</span>
</label>
<div ng-repeat="card in classes | filter:{selected:true}">
<h2>{{card.name}}</h2>
<p><em>{{card.text}}</em></p>
</div>
Here is the data:
$scope.classes = [{
name: "John",
text: "Something about John"
},{
name: "Peter",
text: "Something about Peter"
}];
Note: that a selected attribute is added to each object when it is selected. This is the key we are filtering on.
Working example: http://jsfiddle.net/TheSharpieOne/y2UW7/
UPDATE:
After re-reading the question, it appeared to me that there would be multiple cards for the same name. A different approach would be needed.
In this case, multiple variables are needed, a list of checkboxes and a list of "cards". We also need a var to track which boxes are selected.
In this case we use a function to filter the list as 1 checkbox could change many "cards"
Example: http://jsfiddle.net/TheSharpieOne/y2UW7/1/

Related

Get value of dynamic nested checkbox in angular

I have created the dynamic nested checkboxes in Angular, I want to get the value of checkboxes if selected. I created a demo project in angular and facing the problem in json structure to achieve functionality.
sourcecode
There might be a more effective solution, but I would make an object with nested objects that are connected to your html.
For example:
countriesSport: CountrySports[] | undefined;
country: { id: 1, name: England, active: false, sports: [{ id: 1, name: soccer, active: false, selection: ENUM girl|boy|all}]}
You can save these values in a variable in your .ts file. In your html you can map over these values, for example like this:
<ul>
<li *ngFor="let country of countries>
<input
type="checkbox"
name="checkbox"
[checked]="country.active"
(change)="functionToDoExtraTHingsLikeToggleSubItems()"
/>
<ul>
<li *ngFor="let sport of country.sports>
<input
type="checkbox"
name="checkbox"
[checked]="sport.active"
(change)="functionToChangeSportsStuff()"
/>
</li>
</ul>
</li>
How you want to structure and style you input fields and which onces can be chosen or not, depends on your demands. The selection ENUM with girls|boys|all can also be an array of objects that has an active state.
When you initialize and destroy you component, you can save or map your object to other values.

AngularJS - Check checkboxes dynamically

I am new to AngularJS. I am working on checkboxlist using WebApi and angularJS and need help.
Well, there is a checkboxlist where user can select multiple options. I am able to write successful code for this. The selected options are saved into the database. But, on edit, I want those options selected already. How can I achieve this?
Thanks.
Here is my checkboxlist:
<div ng-repeat="value in getCheckboxlist">
<input ichecklist type="checkbox" id="{{value.Id}}" value="{{value.Id}}">
<span>{{value.Name}}</span>
</div>
Declaration of array:
$scope.selection: [];
and this is how I am getting selected IDs from database on edit:
$scope.selection: selectedValues;
where 'selectedValues' contains json of selected IDs.
Your are looking for the ngChecked directive from AngularJS
Sets the checked attribute on the element, if the expression inside ngChecked is truthy.
Use it like this
<input id="checkSlave" type="checkbox" ng-checked="expression">
You can replace expression by a call to a function that will verify if this checkbox should be checked or not. The function should return true or false
By using ng-checked you can write it as follow
//Angular Controller codes
$scope.Checkboxlist = [{id:1, value: true, Name: "A"}, {id:2, value: false, Name: "B"}];
//View codes
<div ng-repeat="value in Checkboxlist">
<input ichecklist type="checkbox" id="{{value.Id}}" ng-checked="Checkboxlist[$index].value">
<span>{{value.Name}}</span>
</div>

AngularJS dynamically add key and value to an object

I would like to ask you how I can add a key
and a value to an object. I want to get values from textareas, which
are generated with the help of ng-repeat, and to push them to an object.
courses: {}
With postman it works fine
courses :{
"C#" : "C# 101",
"C++": "C++ Advanced"
}
I have a collection called courses. Then with ng-repeat I create a textarea for each course and from this form I would like to put a key and a value to the object courses. key: course_code, value: course_desc. The problem is that I do not know how to store in the in the DB.
<div class="form-group" ng-repeat="course in courses">
<label for="{{course.name}}">{{course.name}}: </label>
<textarea rows="3" ng-model="course.value" class="form-control"></textarea>
</div>
The form

Why is angular ng-repeat adding properties to my model?

I'm working on an angular 1.2.x project and I have a list of radio button generated with ng-repeat and an array of objects.
markup
<div ng-repeat="answer in question.answers track by $index">
<label>
<input type="radio" name="answers" ng-value="answer" ng-model="myDataModel">{{answer.text}}
</label>
</div>
array
[
{
"id":"0",
"parentId":"0a4540dfec6549b4a3bd1b8fb6169d77",
"text":"peanuts"
},
{
"id":"1",
"parentId":"deka9fkac6549b4a3bd1b8fb6169d77",
"text":"cashews"
},
{
"id":"2",
"parentId":"0a4540dfec6asdf4a3bd1b8fb6169d77",
"text":"brazil nuts"
}
]
If I use pre tags to view my model as I select through the radios like this...
<pre>{{myDataModel | json}}</pre>
I see random properties climbing onto my data like this
{
"id":"0",
"parentId":"0a4540dfec6549b4a3bd1b8fb6169d77",
"text":"peanuts",
"spc_mXSzO":0,
"idx_mXSzO":0
}
This is causing issues when I try to pre-select a radio button after loading data from my server. When my controller sets my model equal to one of the answers it doesn't have those properties so it doesn't select the radio. Additionally those property names change every time that I refresh the page so I'm not able to mock them. Where do these come from and what might I try to get around them when preselecting answers?
Alright, I found the culprit. It was this library https://github.com/isteven/angular-multi-select
It attaches spc and idx properties for it's purposes.
I can't reproduce what you're seeing either - here's a plunker with what you have above working:
http://plnkr.co/edit/1td3XtqQjMDk1XYBbjEn?p=preview
One issue which what you have is the ng-model directive in your input tag. You shouldn't bind to primitives directly on the $scope. Here's a good description of why:
https://github.com/angular/angular.js/wiki/Understanding-Scopes
And an update to your code:
<div ng-repeat="answer in question.answers track by $index">
<label>
<input type="radio" name="answers" ng-value="answer" ng-model="myDataModel.myAnswer" />{{answer.text}}
</label>
</div>

Assigning ng-model to checkboxes generated by ng-repeat

I have set up a json containing a list of countries with an ID and Country code attached:
It looks like this:
$scope.countries = [
{"name":"Afghanistan","id":"AFG","country-code":"004"},
{"name":"Ă…land Islands","id":"ALA","country-code":"248"},
{"name":"Albania","id":"ALB","country-code":"008"},
{"name":"Algeria","id":"DZA","country-code":"012"}
]
I then use the ng-repeat directive to create checkbox inputs for every country.
<div ng-repeat="country in countries">
<label><input type="checkbox" ng-model="{{country.id}}" ng-true-value="'{{country.name}}'" ng-false-value="''">{{country.name}}</label>
</div>
However when I run the code I only get the following to display:
Location
checkbox here {{country.name}}
If I remove the ng-model part of the repeat my checkboxes generate fine but I need a unique ng-model to be attached to every checkbox
ng-model="{{country.id}}"
How would I go about attaching a unique ng-model value?
This answer (Generate ng-model inside ng-repeat) does not provide a unique ng-model value
I will suggest you, use:
<div ng-repeat="country in countries">
<label><input type="checkbox" ng-model="myCountry.selected[country.id]" ng-true-value="'{{country.name}}'" ng-false-value="''">{{country.name}}</label>
</div>
{{myCountry.selected}}
JS:
$scope.myCountry = {
selected:{}
};

Resources