How to reset check status in html - html

I have popup in HTML
<div id="term_flags" class="term_flags">
<div class="modal-users-content flagsContent">
<div class="modal-users-header">
<span class="close" ng-click="closeFlagsPopup()">×</span>
<a> Check terminal flags </a>
</div>
<div class="modal-flags-body">
<div class="checkBoxes">
<div class="checkerDiv">
<input type="checkbox" ng-model='reservedFlag' ng-click='changeReservedStatus(reservedFlag)' value="flag" ng-checked="reservedFlag"> Reserved
</div>
<div class="checkerDiv">
<input type="checkbox" ng-model='seasonFlag' ng-click='changeSeasonStatus(seasonFlag)' value="flag" ng-checked="seasonFlag"> Season
</div>
<div class="checkerDiv">
<input type="checkbox" ng-model='networkFlag' ng-click='changeNetworkStatus(networkFlag)' value="flag" ng-checked="networkFlag"> Network
</div>
</div>
<div class="saveFlags">
<button class="button button6" name="changeFlags" value="Change Flags" type="submit" ng-click="saveFlags(item.terminalId)"> Save <p> </button>
</div>
</div>
<div class="modal-footer"></div>
</div>
</div>
this div's display is none in the beginning. but some ng-click is called from outside of this div and display is changed from none to block and initializes checkbox statuses in this angular function
$scope.changeFlagStatus = function(item)
{
$scope.reservedFlag=(item.reservedFlag=='T')?true:false;
$scope.networkFlag=(item.networkFlag=='T')?true:false;
$scope.seasonFlag=(item.seasonFlag=='T')?true:false;
document.getElementById('term_flags').style.display = "block";
}
everything is okay , but when i click on reservedFlag changeReservedStatus(reservedFlag) method was called and change reservedFlag's checked status
$scope.changeReservedStatus = function(item) {
$scope.reservedForSave=item;
}
I saved this status in other variable and close my popup windows document.getElementById('term_flags').style.display=none
when i open this popup window again my function changeFlagStatus(item) is called again and initializes my variables for checkbox correctly but my checkbox are incorrect checked .
In example when i opened my popup window first time my variables after initialize were
$scope.reservedFlag=true;
$scope.networkFlag=false;
$scope.seasonFlag=true;
and my checkbox statuses were
reservedFlag = checked
networkFlag = unchecked
seasonFlag = checked
then i clicked on reservedFlag and changed his status from checked to unchecked and close my popup windows.
then i opened it second time and changeFlagStatus(item) method is called again to initialize my variables again for checkbox statuses
and i want to get
reservedFlag = checked
networkFlag = unchecked
seasonFlag = checked
again, but result is
reservedFlag = unchecked
networkFlag = unchecked
seasonFlag = checked
How can i get it ?

Angularjs won't work pretty good with $scope's properties for primitive variables when used again and again.
I would recommend you to declare an object to scope and set these flags as properties to this object.
$scope.flags = {};
$scope.changeFlagStatus = function(item)
{
$scope.flags.reservedFlag=(item.reservedFlag=='T')?true:false;
$scope.flags.networkFlag=(item.networkFlag=='T')?true:false;
$scope.flags.seasonFlag=(item.seasonFlag=='T')?true:false;
document.getElementById('term_flags').style.display = "block";
}
and the html part as
<input type="checkbox" ng-model='flags.reservedFlag' ng-change='changeReservedStatus()' value="flag" ng-checked="flags.reservedFlag"> Reserved
Instead of ng-click, I would recommend to use ng-change because that is the event to be used with checkbox. If you use ng-change, the injection of the model as a parameter can be avoided which helps in utilizing angularjs's feature of 2-way binding.
You can add ng-true-value and ng-false-value to the checkbox with true and false to make it more easier to handle instead of ng-value.
I would write the html part like this.
<input type="checkbox" ng-model='flags.reservedFlag' ng-change='changeReservedStatus()' ng-true-value="true" ng-false-value="false"> Reserved
Hope this will fix the issue.

Related

Hiding and Showing Edit Input not working(Angular)

I'm trying to make an Edit button, with an input field that appears/disappears when the button is pressed. It was working previously, however when I tried to make a Display form, it doesn't seem to recognize the "title.value" This is very strange. I'm using Boolean for an "edit" variable combined with a *ngIf to show/hide the form. If I take the *ngIf="edit" off, it works normally as a form that displays what you're written. Am I missing something?
Here's the HTML:
<input type="text" #title *ngIf="edit"/>
<button (click)="edit = !edit">Edit</button>
<button (click)="getTitle(title.value)">Get Title</button>
<h2>{{groupTitle}}</h2>
and here's the .ts:
public edit = false;
public groupTitle = "";
getTitle(val) {
this.groupTitle = val;
}
You have a problem with implementing together the ngIf directive and a reference to your input element as #title. In that case you can use hidden instead of ngIf.
Here's your html:
<input type="text" #title [hidden]="!edit"/>
<button (click)="edit = !edit">Edit</button>
<button (click)="getTitle(title.value)">Get Title</button>
<h2>{{groupTitle}}</h2>
There are couple more elegant ways to bind a value and render it on a page.
The first one is to get rid of the Get title button and use (input) method directly on an input element.
In that case, Html looks like:
<input type="text" #title *ngIf="edit" (input)="getTitle(title.value)"/>
<button (click)="edit = !edit">Edit</button>
<h2>{{groupTitle}}</h2>
The second one is to use [(ngModel]) instead of the getTitle method and bind your input value directly to the groupTitle variable.
Html will look like:
<input type="text" #title *ngIf="edit" [(ngModel)]="groupTitle"/>
<button (click)="edit = !edit">Edit</button>
<h2>{{groupTitle}}</h2>
Your .ts file:
edit = false;
groupTitle = "";

AngularJS- how to hide elements using ng-hide?

I have an app that has been written in AngularJS, and am currently trying to add the functionality to hide the headings of some widgets displayed on one of the web pages when the user selects a checkbox to indicate that they should be hidden.
At the moment, I have a page that displays a number of widgets- on the 'heading' of each widget, there is a 'Settings' button. When the user clicks the Settings button, a dialog box opens up on top of the current page (i.e. the user does not navigate to another page- the URL does not change at all). That dialog box contains a form with a number of input fields- one of which is a checkbox that I want to use to 'hide' the headings of all of the widgets on the webpage.
I have been following the example at: https://docs.angularjs.org/api/ng/directive/ngHide to try and do this, but can't quite seem to get it working...
I have added the ng-hide attribute to my HTML element, as in the example:
<div data-ng-if="widget.name === 'tag-box'" ng-hide="hideWidgetHeading"> <!-- class="ng-hide"-->
<div class="divider"></div>
...
<div class="divider"></div>
<div class="row ui-checkbox-row">
<label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
<div class="col-sm-8 col-xs-6">
<label class="ui-checkbox">
<input type="checkbox" name="noWidgetHeading" id="noWidgetHeading" ng-true-value= "'YES'" ng-false-value= "'NO'" ng-change="hideWidgetHeading()" ng-click="hideWidgetHeading()" ng-checked="hideWidgetHeading" ng-model="viewModel.hideWidgetHeading">
<!-- ng-model="viewModel.hideWidgetHeading" -->
<span></span>
</label>
</div>
</div>
</div>
I defined the hideWidgetHeading() function in the ctrl.js file as follows:
function hideWidgetHeading(){
if($scope.widgetHeadingCheckbox==false) {
$scope.$watch('noWidgetHeading', function() {
$scope.hideWidgetHeading = true;
console.log("Value of hideWidgetHeading: ", $scope.hideWidgetHeading);
});
return true;
} else {
console.log("hideWidgetHeading() else called (Widget/ctrl.js line 440) ");
$scope.$watch('noWidgetHeading', function() {
$scope.hideWidgetHeading = false; //document.getElementById('noWidgetHeading');
});
return false;
}
if($scope.hideWidgetHeading) {
console.log("hideWidgetHeading is true- hide the widget heading: ");
}
return $scope.hideWidgetHeading;
}
and I have added the following CSS to my widgets.scss file:
.animate-show-hide.ng-hide {
opacity: 0;
}
.animate-show-hide.ng-hide-add,
.animate-show-hide.ng-hide-remove {
transition: all linear 0.5s;
}
.umw-tag-box {
opacity: 1;
}
When I load my page as it is presently, when I click the Settings button, the dialog opens up. If I then check the 'Hide widget heading' checkbox, and click Submit, the debug that I have added displays the following in the console:
Value of hideWidgetHeading: true
which tells me that the code inside the $scope.$watch(...){...} function is running.
However, if I click the Settings button, and then either don't check the 'Hide widget heading' checkbox, or check it and uncheck it again, and then click Submit, I get the same true value displayed in the console debug, which indicates that the code inside the $scope.$watch(...){...} function is running regardless of whether the 'watched' element changes or not.
Questions
How can I ensure that the code inside the $scope.$watch(...){...} only runs when the 'watched' element (i.e. the checkbox) has its value changed?
How do I actually 'call' the CSS that I've added to hide the HTML elements on the particular HTML that I want to hide? Or how do I make that CSS 'apply' to the HTML elements that I want to hide?
Edit
I changed the HTML for the checkbox to:
<input type="checkbox" ng-model="checked" name="noWidgetHeading" id="noWidgetHeading">
as suggested, and when I now browse to my page, and open the dialog, it displays the widget as I expect:
When I click the 'Settings' button on the widget, the 'Configure Item' dialog opens up on top of the page:
But when I select the 'Hide widget heading' checkbox, it actually hides the 'Tag' label & input box from the dialog:
The element I want to hide is actually displayed on the page from which the dialog box is opened, not on the dialog box itself... but I can't seem to work out how I can hide that using a control on the dialog... Any suggestions?
So, this should work, it is even the first example shown in their docs, with your code it will be:
<div data-ng-if="widget.name === 'tag-box'" ng-hide="viewModel.hideWidgetHeading"> <!-- class="ng-hide"-->
<div class="divider"></div>
...
<div class="divider"></div>
<div class="row ui-checkbox-row">
<label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
<div class="col-sm-8 col-xs-6">
<label class="ui-checkbox">
<input type="checkbox" name="noWidgetHeading" id="noWidgetHeading" ng-true-value= "'YES'" ng-false-value= "'NO'" ng-change="logWidgetHeading()" ng-click="logWidgetHeading()" ng-checked="viewModel.hideWidgetHeading" ng-model="viewModel.hideWidgetHeading">
<!-- ng-model="viewModel.hideWidgetHeading" -->
<span></span>
</label>
</div>
</div>
</div>
you can keep functions on the events if you wan to log values, but there is no need for hiding. The ng-model directive will update your value and the ng-hide should follow.
function logWidgetHeading(){
console.log("Value of hideWidgetHeading: ", $scope.hideWidgetHeading);
}
What i was saying about function or var: in some cases, i used to have values from the scope that were not updated, and the solution was to introduce a function to be called to retreive the value. It is what i thought you were trying because ng-hide="hideWidgetHeading" shows the same name as your function: function hideWidgetHeading(){, that's why i first said that the round brackets were missing. So another version would be something like this (without ng-model on purpose, to show an alternate way to modify stuff with your events):
<div data-ng-if="widget.name === 'tag-box'" ng-hide="getHideWidgetHeading()"> <!-- class="ng-hide"-->
<div class="divider"></div>
...
<div class="divider"></div>
<div class="row ui-checkbox-row">
<label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
<div class="col-sm-8 col-xs-6">
<label class="ui-checkbox">
<input type="checkbox" name="noWidgetHeading" id="noWidgetHeading" ng-true-value= "'YES'" ng-false-value= "'NO'" ng-change="toggleWidgetHeading()" ng-checked="isHiddenWidgetHeading">
<!-- ng-model="viewModel.hideWidgetHeading" -->
<span></span>
</label>
</div>
</div>
</div>
and the js:
//initialisation to the default value:
$scope.isHiddenWidgetHeading = false;
//function that toggles the value:
$scope.toggleWidgetHeading = function(){
$scope.isHiddenWidgetHeading = !$scope.isHiddenWidgetHeading;
};
//function to retreive the value:
$scope.getHideWidgetHeading = function(){
return $scope.isHiddenWidgetHeading;
};
Sorry i was a bit quick in naming vars and such, but you should get what you need here..

AngularJS- document.getElementById() - Why does element return null?

I have a checkbox on a dialog box that is opened when clicking a button in an AngularJS widget in my HTML form:
<form class="m-body" role="form" data-ng-submit="preview($event)" novalidate>
...
<div data-ng-if="widget.name === 'display-box'" ng-dige="hideWidgetHeading()">
<div class="divider"></div>
<div class="row">
...
</div>
...
<div class="row ui-checkbox-row">
<label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
<div class="col-sm-8 col-xs-6">
<label class="ui-checkbox">
<input type="checkbox" name="noWidgetHeading" ng-true-value= true ng-false-value= false ngChange="hideWidgetHeading()" ng-click="hideWidgetHeading()" >
<span></span>
</label>
</div>
</div>
</div>
...
In the ctrl.js file, I have the following function:
angular.module('app.widget').run(function($templateCache){
...
}).controller('WidgetPickerCtrl', function($scope, $timeout, fxTag, gPresets, appWidget){
...
$scope.widget = undefined;
...
$scope.checkboxModel = {
value1 : true,
value2 : false
};
...
var widgetHeadingCheckbox = document.getElementById("noWidgetHeading");
console.log(widgetHeadingCheckbox);
$scope.$watch('widgetHeadingCheckbox', function(){
console.log("Value of checkbox has changed: ", widgetHeadingCheckbox);
});
function hideWidgetHeading(){
if(widgetHeadingCheckbox){
console.log("Value of widgetHeadingCheckbox in 'if': ", widgetHeadingCheckbox);
return widgetHeadingCheckbox;
} else {
console.log("Value of widgetHeadingCheckbox in 'else: ", widgetHeadingCheckbox);
return widgetHeadingCheckbox;
}
}
Currently, when I load the page, and click the button to open the dialog box, the checkbox is displayed on the dialog box unchecked. When I click it, it shows that it is checked, and if I click it again, it becomes unchecked again, as you would expect.
I am trying to use the hideWidgetHeading() JS function above to perform a different action when the dialog box 'Submit' button is pressed, depending on whether the checkbox is selected or not.
At the moment, if I select the checkbox (i.e. it's checked- its value should be true), and click the 'Submit' button on the form, I get the following output in my console:
null
Value of checkbox has changed: null
Value of widgetHeadingCheckbox in else: null
I also get the same output in the console if I click the submit button when the checkbox is not checked...
Why is the value of my checkbox always null... I would expect it to be either true or false, but never null... Do I need to initialise it somewhere? I thought I had already done that in the HTML, when setting its ng-true-value & ng-false-value...
Edit
Following what dcrux has said in their answer, I have changed the HTML for the dialog box on which the checkbox is displayed to:
<div data-ng-if="widget.name === 'umw-tag-box'" ng-hide="hideWidgetHeading">
...
<div class="row ui-checkbox-row">
<label class="col-sm-4 col-xs-6" data-i18n="Hide widget heading:"></label>
<div class="col-sm-8 col-xs-6">
<label class="ui-checkbox">
<input type="checkbox" ... ng-model="hideWidgetHeading">
<span></span>
</label>
</div>
</div>
</div>
When I click the 'settings' button on the widget, the dialog box opens:
However, when I then check the 'Hide widget heading' checkbox, what it actually hides is stuff from the dialog, rather than from the widget:
What I want it to hide is the title bar of the widget itself, not anything from the dialog box:
How can I get it to hide something from the widget, rather than from the dialog box?
use ng-model in your input tag
<input type="checkbox" name="noWidgetHeading" ng-true-value= true ng-false-value= false ngChange="hideWidgetHeading()" ng-model="noWidgetHeading" ng-click="hideWidgetHeading()" />
keep ng-model in your input tag ng-model="noWidgetHeading"
And also instead of reading from document
var widgetHeadingCheckbox = document.getElementById("noWidgetHeading");
console.log(widgetHeadingCheckbox);
Try reading from scope
var widgetHeadingCheckbox = $scope.noWidgetHeading;
console.log(widgetHeadingCheckbox)
It's just suggestion try and let me know
You'll want to change that ngChange to ng-change. Also, the clue is in the method name getElementById() - the id is missing, you have the name, but not the id.
I would fist add ng-model="noWidgetHeading" back to the input.
Then change:
var widgetHeadingCheckbox = document.getElementById("noWidgetHeading");
console.log(widgetHeadingCheckbox);
$scope.$watch('widgetHeadingCheckbox', function(){
console.log("Value of checkbox has changed: ", widgetHeadingCheckbox);
});
to just:
$scope.$watch('noWidgetHeading', function(){
console.log("Value of checkbox has changed: ", $scope.noWidgetHeading);
});
Next:
On you HTML- the input does not require ng-true-value or ng-false-value as using 'true' and 'false' is the default. If you not want to use the default ensure you are using an expression so: ng-true-value="'YES'"
Ensure usage of directives is correct: ngChange should be ng-change.
hideWidgetHeading() is not exposed to the scope so can not be called from the front end. Consider binding it to $scope: $scope.hideWidgetHeading = function(){...}
See if these changes fix the problem.

Spring MVC - how to bind HTML checkbox value in a boolean variable

I am very new to spring mvc world. I am trying to send boolean value to from html form checkbox. When a user check the checkbox then it will send true, false otherwise.
<form class="attendanceBook" role="form" method="post" action="/attendances">
<div class="row">
<div class="form-group">
<div class="col-xs-4">
<label class="control-label">Check Here</label>
</div>
<div class="col-xs-4">
<input type="checkbox" name="i" id="i" value="true" />
</div>
<div class="col-xs-4">
<input type="submit" value="Click"/>
</div>
</div>
</div>
</form>
After some googilng I have found this so post, where it said standard behaviour is the value is only sent if the checkbox is checked. So what I have understand that is if the checkbox checked then the form will submit with the value of checkbox, otherwise it will not submit. When there is unchecked checkbox the initialization value in data class will be effective.
But in my case every time I am submitting the form it submitting true.
here is my rest controller for the bind html form submit.
#RestController
#RequestMapping("attendances")
class AttendanceRestController {
val logger = getLogger(AttendanceRestController::class.java)
#PostMapping
fun patchAttendance(#RequestBody attendanceJson: AttendanceJson): ResponseEntity<*> {
logger.info("attendanceJson {}", attendanceJson)
return responseOK(attendanceJson)
}
}
the data class(I am using kotlin)
data class AttendanceJson (
var i: Boolean = false,
var t: String = ""
)
So what will be the method to bind boolean data from a form submission with checkbox. I am also using Thymeleaf. Thanks in advance.
I'm working in Struts and don't know much about Spring. But I faced a similar situation.
What I did was I binded the checkbox with a boolean property in my From class. So for each checkbox, one boolean variable. And at the time of submitting in front end, I'll call a JS function code is below
function verifyCheckboxes() {
document.getElementById("researchPaper").value = document.getElementById("researchPaper").checked;
document.getElementById("researchPaperSeminarProceed").value = document.getElementById("researchPaperSeminarProceed").checked;
document.getElementById("extraActivities").value = document.getElementById("extraActivities").checked;
document.getElementById("studentAchivements").value = document.getElementById("studentAchivements").checked;
}
Here you can see I'm just assigning the value of checked property of that Checkbox just before submitting. It will be either true or false.
You should remove 'value' attribute from the input. If you want the checkbox checked when loading the page, add 'checked' attribute not 'value'.
Replace the input line with this:
<input type="checkbox" name="i" id="i" checked="checked"/>
This is the reason why you always get 'true' in code behind.
It's a bit of a hack, but if you change the type of the input tag from 'checkbox' to 'text' just before the form is posted, you will receive the value, whether it is checked or unchecked.
If you use jQuery:
$("input:checkbox").each(function(){this.type='text'})

Changing CSS property if no input is provided (Angular JS)

I am making a quiz using angular js.The first page of the quiz requires you to enter your name and doesn't allow you to go further if no input is provided.I want to animate input field more like the shake animation on this site (http://daneden.github.io/animate.css/) if the input field is empty and the user presses the start button.Please help me out.
CODE html :
<p>What's your name?</p>
<input type = "text" class = "playername" ng-model = "playername">
<h2>Welcome {{playername}}!</h2>
<p>Click start if you're ready!</p>
<p class = "btn" ng-click = "startQuiz()">START</p>
CODE angular :
$scope.presentQues = -1;
$scope.startQuiz = function(){
if ($scope.playername != null){
$scope.presentQues = 0;
}
}
PS. this is not the full code.
Building on #Chris's answer, if you wrap it in a form, you can also use the form.$submitted variable to check if the form has been submitted.
ng-class uses the following format: ng-class={'css-class':truthy-condition-to-evaluate
Try the following code (this will have to be changed if you already are wrapping everything in a form):
<div ng-controller="myCtrl as ctrl">
<form name="myForm">
<p>What's your name?</p>
<input type = "text" name="playername" class = "playername" ng-class="{'animated shake': !ctrl.playername && myForm.$submitted}" ng-model = "ctrl.playername">
<h2>Welcome {{ctrl.playername}}!</h2>
<p>Click start if you're ready!</p>
<input type="submit" class = "btn" ng-click = "startQuiz()" value="START">
</form>
</div>
Now, if the player submits the form without typing anything, angular will apply the animated and shake css classes to the text input.
Look at my plunker to view it in action.
There are many ways of doing this, one I can see is using ng-class.
<!-- add class 'bounceInUp' if the scope var 'isEmpty' is true -->
<input type="text" class="playername" ng-class={'bounceInUp':isEmpty,} ng-model="playername">