Angularjs exclusive radio buttons in table row - html

I am using angularjs v1.
I would like to have exclusive radio buttons in each table row. Each row will look something like below;
The radio buttons should be exclusive. In other words, only 1 of the buttons can be selected at any one time. When the send radio button info button is pressed, information regarding which radio button is selected should be sent to the angularjs controller.
Here is my html code;
<div ng-controller="RadioButtonsCtrl">
<table class="table table-hover data-table sort display">
<thead>
<tr>
<th class="Off_">
Off
</th>
<th class="On_">
On
</th>
<th class="Toggle_">
Middle
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in radio_button_items">
<td><input type="radio" ng-model="model.select_off"></td>
<td><input type="radio" ng-model="model.select_on"></td>
<td><input type="radio" ng-model="model.select_middle"></td>
<td>
<button ng-click="SendInfo(item)">Send radio button info</button>
</td>
</tbody>
</table>
Here is the controller code;
.controller('RadioButtonsCtrl', ['$scope',
function ($scope) {
$scope.SendInfo = function (row) {
console.log(row);
}
}])

Radio button groups are defined by giving them the same name attribute:
<td><input type="radio" ng-model="model.select_off" name="foo"></td>
<td><input type="radio" ng-model="model.select_on" name="foo"></td>
<td><input type="radio" ng-model="model.select_middle" name="foo"></td>
Taken from https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-type
radio: A radio button. You must use the value attribute to define the
value submitted by this item. Use the checked attribute to indicate
whether this item is selected by default. Radio buttons that have the
same value for the name attribute are in the same "radio button
group". Only one radio button in a group can be selected at a time.

Related

Radio Button Model returns undefined value when a save button is clicked

I have 2 radio buttons, which I assigned values of '0' and '1' respectively. I need to get the value when I click my Save Button but It returns an Undefined value. Here is my code. What could be my problem?
<tr ng-repeat="item in recruleData">
<td>
{{item.package_name}}
</td>
<td>
<input type="radio" name="radio" ng-model="radio.selectedProductBlock"
value="0" >A<br/>
<input type="radio" name="radio" ng-model="radio.selectedProductBlock"
value="1" > B
</td>
<a class="btn btn-primary pull-right" ng-click="radioValue();">Save</a>
</tr>
my JS Code
$scope.selectedProductBlock;
$scope.radioValue= function(){
alert($scope.selectedProductBlock);
}
You need to define a variable of type boolean inside the object radio,
$scope.radio = {};
$scope.radio.selectedProductBlock = false;
and then
And radioValue function
$scope.radioValue = function(){
console.log($scope.radio.selectedProductBlock);
}
According to your requirement, you need to define radio object in recruleData
each record
Ex :
$scope.recruleData = [
{"package_name" : "package1", "radio":{"selectedProductBlock":""}},
{"package_name" : "package1", "radio":{"selectedProductBlock":""}},
]
And radioValue function
$scope.radioValue= function(item){
alert(item.radio.selectedProductBlock);
}
In addition, you need to give a unique name for each row radio button groups. You can simply achieve that append $index to your radio button name.
HTML Code
<table>
<tr>
<th>Package</th>
<th>Actioins</th>
<th></th>
</tr>
<tr ng-repeat="item in recruleData">
<td>
{{item.package_name}}
</td>
<td>
<input type="radio" name="radio_{{$index}}" ng-model="item.radio.selectedProductBlock"
value="0" >A<br/>
<input type="radio" name="radio_{{$index}}" ng-model="item.radio.selectedProductBlock"
value="1" > B
</td>
<td><a class="btn btn-primary pull-right" ng-click="radioValue(item);">Save</a></td>
</tr>
</table>
Working JSFiddel

Radio button for each entry

<table style="margin-top:10px;font-size:18px;">
<tr>
<th>NAME</th>
<th>CLASS</th>
<th>ROLL NO.</th>
<th>ATTENDENCE</th>
</tr>
<?php
$stream=$_SESSION['stream'];
$year=$_SESSION['year'];
$rs=mysql_query("SELECT * FROM student WHERE stream='$stream' AND year='$year' ORDER BY roll",$connect);
while($row=mysql_fetch_array($rs))
{
?>
<tr>
<td>
<?=$row[0]?>
</td>
<td>
<?=$row[3]?>
<?=$row[4]?>
</td>
<td>
<?=$row[5]?>
</td>
<td>
<input type="radio" name="r1" value="p" class="attendence">
<input type="radio" name="r1" value="a" class="attendence">
</td>
</tr>
<?php
}
?>
</table>
------------------
here is the code I am currently working on. I am making online attendance system where I am making the page where teacher will give the attendance to student. I have successfully retrive the name of the student but I am unable to make the radio button for which teacher will give the attendance.. Here 2 radio button appears but among all of these only 1 is working
my understanding is for different students only one radio value is working and applicable to all.
You need an array which specify per row.
so use brackets in name r1[] as i mention.
If not this is the requirement then explain more

How to know if my radio button is checked? AngularJS

guys I did a code for knowing if my checkbox is checked and works fine:
HTML:
<div ng-app>
<div ng-controller="UserController">
<table id="tblUsers" class="Table">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Last Name</th>
<th>Phone</th>
</tr>
<tr ng-repeat="user in users">
<td><input type="checkbox" ng-model="user.checked"/></td>
<td>{{user.name}}</td>
<td>{{user.lastName}}</td>
<td>{{user.phone}}</td>
</tr>
</table>
</div>
</div>
This is the Angularjs code:
function UserController($scope) {
$scope.users = [
{ checked: false, name: 'Jorge', lastName: 'Martinez', phone: 012345 },
{ checked: false, name: 'Juan', lastName: 'Perez', phone: 78964 }
];
$scope.updateUser = function () {
angular.forEach($scope.users, function (user) {
if (user.checked) {
user.name = $scope.nameUpdate;
user.lastName = $scope.lastNameUpdate;
user.phone = $scope.phoneUpdate;
}
});
};
}
The problem is when I change the checkbox for a radio button:
<td><input type="radio" ng-model="user.checked"/></td>
When I do this, this value "if(user.checked)" appears as "undefined".
Some one that knows how to fix this or how to know if the radio button is checked or not in the AngularJS controller.
see https://docs.angularjs.org/api/ng/input/input%5Bradio%5D
you'll want to have multiple radio buttons, each with it's own value to set some property to (although this is weird for a yes no you are better off with a checkbox, but if you had multiple values this is how radio buttons work)
<input type="radio" ng-model="user.checked" ng-value="true" />Yes
<input type="radio" ng-model="user.checked" ng-value="false" />No
when you have just one radio button, if it is not checked by default or you have no ng-value it will be undefined, but also having only one radio button, you could never unselect it.
You need to give the radio button a value.
<td><input type="radio" ng-model="user.checked" value="true"/></td>
<td><input type="radio" ng-model="user.checked" value="false"/></td>
Working Fiddle

2 HTML forms within 1 table

I have a single table that is generated dynamically by looping through a result set and creating rows. For each row, I need to include 2 checkboxes. Checkbox A on each row needs to correspond to form A, and checkbox B to form B.
I know this isn't valid, but the below pseudocode is essentially what I want. I know that in HTML5 I can specify which form an input element belongs to, but my users will primarily be using IE8, which, as far as I can tell, doesn't support this feature.
<form name="formA">
<form name="formB">
<table>
<tr>
<th>
<th>
<th>
</tr>
LOOP
<tr>
<td><input type="checkbox" name="chkA" value="1"></td>
<td>Something</td>
<td><input type="checkbox" name="chkB" value="1"></td>
</tr>
END LOOP
</table>
<input type="submit" /> //formA
<input type="submit" /> //formB
</form> //formA
</form> //formB
Any ideas on how I can accomplish this? I suppose one way would be to use a single form and change the action depending on which submit button is clicked, but I want to see if anyone else has any ideas before I do that.
Thanks,
Tom
Probably the best way is to just make it all in one form and act differently later according to the options selected.
For example
<form action="doStuff.php" method="post">
<table>
<tr>
<th></th>
<th></th>
</tr>
<!-- LOOP -->
<tr>
<td><input type="checkbox" name="chkA" value="1"></td>
<td><input type="checkbox" name="chkB" value="1"></td>
</tr>
<!-- END LOOP -->
</table>
<div>
<input type="submit" name="submit" />
</div>
</form>
doStuff.php
if (isset( $_POST["chkA"] ) ){
// Checkbox A is checked.
}
if (isset( $_POST["chkB"] ) ){
// Checkbox B is checked.
}

HTML tabindex problem

I am stuck with a tabindex problem here. As you can see in the HTML mock up, I have four buttons namely "Add", "Subtract", "Save" and "Close". When user Hits the Tab key, the flow starts correctly from Add-> Subtract-> Save-> Close, but after Close if user hits the Tab key again, the focus goes to the TD TAG with comment "Jumping Here". What I want it to do is to go to Add button instead. I tried giving all Buttons a tabindex of 1, 2, 3, 4 then I tried giving every button an index of 1 and rest of the places as -1 but that didnt solve the problem either. How do I solve this issue?
http://jsfiddle.net/t3ch/aNxjy/
Pretty sure this is not possible. Just because you have a form with tabindex values, it doesn't mean those are the only elements that will receive focus. Even browser components, like the address bar, will receive focus at some point. The browser will use the tabindex you specify, but after that tabbing will cycle through everything else.
I don't like this idea but a way you could achieve this is registering an onblur event for the Close Button, then set the focus to what ever control you wanted...
This does work....
function resetFocusIndex(){
var obj = document.getElementsByName('addIt');
if(obj[0])
obj[0].focus();
}
<INPUT TYPE="button" tabindex = "1" VALUE="Close" NAME="closeIt" onblur="resetFocusIndex()">
UPDATE
<html>
<head>
<script>
function resetFocusIndex(){
var obj = document.getElementsByName('addIt');
if(obj){
obj[0].focus();
}
}
</script>
</head>
<body>
<TABLE WIDTH=95%>
<TR>
<TD><INPUT TYPE="button" tabindex = "1" id="test2" VALUE="Add" NAME="addIt"></TD>
<TD><INPUT TYPE="button" tabindex = "1" VALUE="Subtract" NAME="minusIt"></TD>
<TD> </TD>
</TR>
</TABLE>
<DIV>
<TABLE BORDER="1" CELLSPACING="0" CELLPADDING="1">
<TD> </TD>
<TD>First</TD>
<TD>Second</TD>
<TD>Third</TD>
<TD>Fourth</TD>
<TR>
<!-- Jumping Here --> <TD><INPUT TYPE="radio" NAME="selected" tabindex = "-1" VALUE=""></TD>
<TD><INPUT TYPE="text" tabindex="-1" NAME="" VALUE="" disabled></TD>
<TD ></TD>
<TD><INPUT TYPE="text" tabindex="-1" NAME="" VALUE="" disabled> </TD>
<TD><INPUT TYPE="text" tabindex="-1" NAME="" VALUE="" disabled> </TD>
<INPUT TYPE="hidden" NAME="" VALUE="">
<INPUT TYPE="hidden" NAME="" VALUE=""></TD>
</TR>
</TABLE>
</DIV>
<TABLE>
<TR>
<TD>Click Here!</TD>
</TR>
<TR><TD> </TD></TR>
<TR>
<TD>
<INPUT TYPE="button" tabindex = "1" VALUE="Save" NAME="saveIt">
<INPUT TYPE="button" tabindex = "1" VALUE="Close" id="test" NAME="closeIt" onblur="resetFocusIndex()">
</TD>
</TR>
</TABLE>
</body>
</html>
Don't do this it's an awful idea.
Have focusable items (eg input) preceding the first and following the last button in tab order. Hide them (eg by positioning them off the side of the page with position: absolute; left: -1000px;). When they get a focus event, move focus to the button at the other end of the tab order.
Don't do this it's an awful idea. Really.
First you need 'id's for the buttons, that's always good.
Then you need to set two handlers; one on the last to tab to first, and one on the first button to shif+tab to the last.
See this: http://jsfiddle.net/aNxjy/11/.
I used jquery, but you can accomplish this with regular js as well. Just that you'll have to do cross-browser compatibility.
Further to gmcalab's answer instead of getElementsByName you could use getElementById
var obj = document.getElementById('test2');
if(obj){
obj.focus();
}
or possibly:
var obj = document.getElementById('test2');
if(obj){
obj.select();
}