Radio Buttons not saving correct value in Angular JS - angularjs

After a couple of tries on fixing my code, and searching for any solutions on the net, I finally gave up and would like to ask help on you guys.
I'm working on multiple sets of radio buttons and turn them to star ratings:
imgur link on my ratings section (cause I am not able to post images)
Each star is represented with 1 radio button, and each row has 5 radio buttons. In this image, im creating 3 sets of 5 radio buttons. Using the ng-repeat feature,
<tr ng-repeat="number in called_numbers">
<td>
<div ng-if="number.name != null">
<span class="star-rating" ng-init="temp_called_numbers[$index].rating.value ">
<input type="radio" name="rating" ng-model="temp_called_numbers[$index].rating.value" value="1"><i></i>
<input type="radio" name="rating" ng-model="temp_called_numbers[$index].rating.value" value="2"><i></i>
<input type="radio" name="rating" ng-model="temp_called_numbers[$index].rating.value" value="3"><i></i>
<input type="radio" name="rating" ng-model="temp_called_numbers[$index].rating.value" value="4"><i></i>
</span>
</div>
</td>
Now this specific page keeps on refreshing so when you click/highlight on a specific star, it refreshes and returns 5 blank stars again. What I did was that, I created an array to temporary store the value of each row.
$scope.temp_called_numbers = [];
$scope.rating_stars = {
value:0
};
and setting $scope.temp_called_numbers by:
rate_stars = function(){
for(i=0; i < $scope.called_numbers.length; i++){
$scope.temp_called_numbers.push( {rating: $scope.rating_stars});
}
}
The issue here now is that, when I select a star/radio button in one row, the other rows get the same value. For example, I select 1 star for row 1 which is temp_called_numbers[0].rating.value indexed 0, all the other rows get 1 star as well.
Any code change, reference or any help is much appreciated. Thanks!

$scope.temp_called_numbers = [];
$scope.rating_stars = {
value:0
};
rate_stars = function(){
rating = $scope.rating_stars;
for(i=0; i < $scope.called_numbers.length; i++){
angular.extend(temp_called_numbers, rating);
}
}

<tr ng-repeat="number in called_numbers">
<td>
<div ng-if="number.name != null">
<span class="star-rating" ng-init="temp_called_numbers[$index].rating.value ">
<input type="radio" name="rating" ng-model="number.checked"><i></i>
<input type="radio" name="rating" ng-model="number.checked"><i></i>
<input type="radio" name="rating" ng-model="number.checked"><i></i>
<input type="radio" name="rating" ng-model="number.checked"><i></i>
</span>
</div>
</td>
</tr>
// Access selected items
angular.forEach($scope.called_numbers, function(value, index) {
if (value.checked) {
$scope.called_numbers[index].checked = isSelectAll;
}
});

For those checking on this thread. I've figure out that the issue here is not on the saving-to-array part, however, the issue is on the HTML side of the code.
Also, this was a visual bug on the stars and the array is fine and it stores the data when clicked. The reason that I was not getting the right data when clicking the radio button from 1 set of radio button group is because I was using <span></span> for the wrapper on the radio button.
what happened was that, even though I was getting 2 or more sets of Radio Button through ng-repeat, the page only accepts 1 radio button input.
By changing the wrapper to <form></form>:
<form class="star-rating" ng-init="temp_called_numbers[$index].rating.value ">
<input type="radio" name="rating" ng-model="number.checked"><i></i>
<input type="radio" name="rating" ng-model="number.checked"><i></i>
<input type="radio" name="rating" ng-model="number.checked"><i></i>
<input type="radio" name="rating" ng-model="number.checked"><i></i>
</form>
I can now get 2 or more different inputs from each set of radio button group.

Related

AngularJS: Fill select box based on two radio buttons

I have 2 radio groups with 2 radio buttons each, age group (AG1, AG2) and language ("one language", "multilingual"). Depending on which radio button is checked, I want to fill a select box with different options. So there's 4 different combinations = 4 different sets of options (4 AngularJS scope properties). I got it to work with one radio group using ng-model, but I can't figure out how to do this with two. I tried using jQuery but it won't work, I've been told that I shouldn't mix AngularJS and jQuery.
HTML (ng-value is obviously wrong here, I don't know what to set as value)
<div class="form-check">
<input class="form-check-input" type="radio" name="AG" id="AG1" ng-model="values" ng-value="PD1E">
<label class="form-check-label" for="AG1">4 - 5 years</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="AG" id="AG2" ng-model="values" ng-value="PD2M">
<label class="form-check-label" for="AG2">6 - 7 years</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="language" id="one" ng-model="values" ng-value="PD1E">
<label class="form-check-label" for="one">one language</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="language" id="multi" ng-model="values" ng-value="PD1M">
<label class="form-check-label" for="multi">multilingual</label>
</div>
<br>
<select id="inputPD" class="form-control">
<option ng-repeat="x in values">{{x}}</option>
</select>
AngularJS scope properties
$scope.PD1E = [
"1-one", "0,3", "0,5", "0,8", "1,8", "5,9", "12,8", "32,2", "58,8", "81,6", "100,0"
];
$scope.PD2E = [
"2-one", "0,7", "0,9", "2,0", "7,9", "21,3", "41,5", "100,0"
];
$scope.PD1M = [
"1-multi", "2,1", "3,5", "5,6", "12,5", "19,4", "33,3", "48,6", "61,8", "82,6", "95,8", "100,0"
];
$scope.PD2M = [
"2-multi", "0,5", "2,2", "7,6", "23,8", "49,2", "70,3", "100,0"
];
So if I select AG1 + one, it should display PD1E in the select box.
AG2 + one = display PD2E
AG1 + multi = display PD1M
AG2 + multi = display PD2M
One approach is to use the ng-if directive with the options:
<select id="inputPD" ng-model="selectPD" class="form-control">
<option ng-repeat="x in PD1E" ng-if="radio1=='1' && radio2=='E'">{{x}}</option>
<option ng-repeat="x in PD2E" ng-if="radio1=='1' && radio2=='E'">{{x}}</option>
<option ng-repeat="x in PD1M" ng-if="radio1=='2' && radio2=='M'">{{x}}</option>
<option ng-repeat="x in PD2M" ng-if="radio1=='2' && radio2=='M'">{{x}}</option>
</select>
The <option> elements will only be added when the condition is true.

how to $Index get from Javascript side

how to Checked value from $index base. suppose in textboxStart Index and TextboxEnd . when user enter Start index 5 and End Index 10 , checkbox automatically checked, from 5 To 10 Index. please help me
<tr ng-repeat="item in MyList">
<td>{{$index}}</td>
<td>
<label class="mdl-checkbox mdl-js-checkbox" for="checkbox33">
<input autocomplete="off" ng-model="item.checked"
type="checkbox" id="checkbox33"
class="mdl-checkbox__input" unchecked>
{{CheckItems()}}
</label>
<td>{{item.Name}}</td>
<td>{{item.PilgrimID}}</td>
<td>{{item.GroupName}}</td>
<td>{{item.PassportNo}}</td>
<td>{{item.Gender}}</td>
<td>{{item.SubAgentName}}</td>
Use the ng-change directive to specify an update function:
<div>Input Start
<input ng-model="inputStart" ng-change="updateSelections(inputStart,inputEnd)" />
</div>
<div>Input End
<input ng-model="inputEnd" ng-change="updateSelections(inputStart,inputEnd)" />
</div>
Then update the selections:
$scope.updateSelections = function(iStart, iEnd) {
$scope.MyList.forEach((x,index)=>item.checked = (iStart<=index && index<=iEnd));
};
just push the selected values in an array , selectedItemIndexes like ng-checked="selectedItemIndexes.includes($index)"
https://stackblitz.com/edit/angularjs-snavdn
you can do it by adding one property like ItemIndex in your ItemList model,
and by putting input box with display none you can get index value in ItemList and can do what you want from javascript side,
<td><input type="text" style="display:none;" ng-model="item.ItemIndex"
ng-value="$index"/>{{$index}}
</td>

How to shift + click to select multiple checkboxes using checklist-model directive?

Is there any way to shift + click to select multiple checkboxes using this directive like we have in Gmail?
Example:
Click on 1st row's checkbox.
Holding down SHIFT key.
Click on 10th row's checkbox.
Result: the first 10th rows will be selected.
Here is an example of how to do it, the selectors will need to be changed to something more specific when it is used in something with more that just the check-boxes.
It works by scanning all the inputs and seeing if it is checked, if it is it toggles a boolean that will cause each element to toggle to checked until the this matches the clicked item then breaks out of the loop as it doesnt need to check any more.
$("[type='checkbox']").click(function(evt) {
if (evt.shiftKey){
let item = $(this);
let hitfirstChecked = false;
$("#wrapper-check input").each(function(){
if($(this).is(":checked")){
hitfirstChecked = true;
}
if(hitfirstChecked){
$(this).prop('checked', true);
}
if(item.is($(this))){
return false;
}
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="wrapper-check">
<input type="checkbox" value="1">1
<input type="checkbox" value="2">2
<input type="checkbox" value="3">3
<input type="checkbox" value="4">4
<input type="checkbox" value="5">5
<input type="checkbox" value="6">6
<input type="checkbox" value="7">7
<input type="checkbox" value="8">8
</span>

ng-disabled not working for radio buttons and checkboxes

I am working on an AngularJS application. I am trying to create a page that allows the user to select one of three radio buttons. Two of the three also have checkboxes underneath them to allow the user to select additional options if they've selected the appropriate radio button. To try to prevent improper checkbox selections, I'm trying to set the ng-disabled attribute on the checkboxes. So far, it's not working, and I've tried several different iterations.
This is my HTML:
<div class="panel-body">
<input type="radio" id="notFraudulent" name="actionSelector" ng-model="cleared" /><label for="notFraudulentRadio"> Not Fraudulent</label><br />
<input type="checkbox" id="highVolumeCustomer" ng-model="highVolumeCustomer" ng-disabled="(fraudulent||cleared)" /><label for="highVolumeCustomer"> High Volume Customer</label><br />
<br/>
<input type="radio" id="appearsFraudulent" name="actionSelector" ng-model="fraudulent" /><label for="isFraudulentRadio"> Appears Fraudulent</label><br />
<input type="checkbox" id="reportAccount" ng-model="reportAccount" ng-disabled="(cleared||reviewed)" /><label for="reportAccount"> Report Account</label><br />
<br/>
<input type="radio" id="markReviewed" name="actionSelector" ng-model="reviewed" /><label for="markReviewed"> Mark As Reviewed For Later</label>
</div>
I have tried changing the operator on the ng-disabled expressions to &&, as I've seen some articles where it's suggested that the operators don't mean what one thinks they mean. But that doesn't work, and neither does it work if I put just a single condition in the expression. There isn't anything in the controller (yet) that tries to use or manipulate any of the ng-models in the HTML. I've come to the conclusion that there's something I'm missing with regard to the radio buttons, but I can't for the life of me figure out what.
Can anyone see what my mistake is?
you should use value property to bind special value for radio button, and when radiobutton's status is changed, the value will be kept at ng-model.
refer the code snippet below:
angular.module("app", [])
.controller("myCtrl", function($scope) {
$scope.selectedValue = 'cleared';
$scope.cleared = false;
$scope.fraudulent = false;
$scope.reviewed = false;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="panel-body" ng-app='app' ng-controller="myCtrl">
<input type="radio" id="notFraudulentRadio" name="actionSelector" value="cleared" ng-model="selectedValue" /><label for="notFraudulentRadio"> Not Fraudulent</label><br />
<input type="checkbox" id="highVolumeCustomer" ng-model="highVolumeCustomer" ng-disabled="selectedValue === 'fraudulent' || selectedValue === 'cleared'" /><label for="highVolumeCustomer"> High Volume Customer</label><br />
<br/>
<input type="radio" id="isFraudulentRadio" name="actionSelector" value="fraudulent" ng-model="selectedValue"/><label for="isFraudulentRadio"> Appears Fraudulent</label><br />
<input type="checkbox" id="reportAccount" ng-model="reportAccount" ng-disabled="selectedValue === 'cleared' || selectedValue === 'reviewed'" /><label for="reportAccount"> Report Account</label><br />
<br/>
<input type="radio" id="markReviewed" name="actionSelector" value="reviewed" ng-model="selectedValue"/><label for="markReviewed"> Mark As Reviewed For Later</label>
<br>
cleared:{{cleared}}<br>
fraudulent:{{fraudulent}}<br>
reviewed:{{reviewed}}<br>
selectedValue: {{selectedValue}}
</div>

Handling multiple radio buttons

Here "idDefault" values are not changing. Please help me.
<div class="form-group" >
<label class="control-label">Select Colors : </label>
<div ng-repeat = "color in colorlist">
<input type="radio" name="color" ng-value="true" ng-model="color.idDefault">
{{color.colorName}} </input>
</div>
Colors : {{colorlist}}
and my JSON is
$scope.colorlist = [
{
colorName:'Black',
idDefault:true},
{
colorName:'Red',
idDefault:false} ,
{
colorName:'Bule',
idDefault:false} ,
{
colorName:'Yellow',
idDefault:false} ,
];
Initially black is selected. When selecting other colors, Black isDefault value not changing to false.
First of all as you are using color list for radio buttons , its the way for Checkbox list . But if you wanna use it like this only , you can do it by adding a ng-change in it like -
View Code-
<div class="form-group" >
<label class="control-label">Select Colors : </label>
<div ng-repeat = "color in colorlist">
<input type="radio" name="color" ng-change="changed(color)" ng- value="true" ng-model="color.idDefault"/>
{{color.colorName}}
</div>
Colors : {{colorlist}}
and in controller add a method -
$scope.changed=function(color){
for(var i=0;i<$scope.colorlist.length;i++)
if($scope.colorlist[i].colorName!==color.colorName){
$scope.colorlist[i].idDefault=false;
}
}
Check the fiddle
hope it will work for you.
The method that you are trying to use above is used for checkboxes where you can select multiple value but here you want only one color to be selected at time.
You can this as,
<div class="form-group" >
<label class="control-label">Select Colors : </label>
<div ng-repeat = "color in colorlist">
<input type="radio" name="color" ng-value="color" ng-model="selectedColor">
{{color.colorName}}
</input>
</div>
This way you can get the selected color in your contoller in $scope.selectedColor variable.

Resources