how to hide column when input is empty in Angular - html

I have three flex column and I want the middle column to hide when the input is empty.
My goal is to hide the middle column first, show the middle column when the user start typing and hide again when the input is empty.
I was able to do the first two but not sure how to hide it again
I believe I might have to do something with *ngIf in the input or inside middle column but I'm new to angular and not sure how.
My Input
<input matInput [formControl]="inputCtrl" [(ngModel)]="searchText"
(ngModelChange)="searchTextChanged($event)" placeholder="Search" class="input"
My Middle Column
<div class="center" *ngIf="showColumn">
TS
showColumn = false;
ngOnInit(): void {
this.inputCtrl.valueChanges
.pipe(takeUntil(this.death$))
.subscribe((value: string) => {
this.showColumn = true;
});

You can use searchTextChanged($event)
Add this function to your app:
searchTextChanged(event){
if(this.searchText == '')
{
this.showColumn = false;
}
else{
this.showColumn = true;
}
or
you can do something like this
this.inputCtrl.valueChanges
.pipe(takeUntil(this.death$))
.subscribe((value: string) => {
if(value == ''){
this.showColumn = false;
}
else{
this.showColumn = true;
}
});
Hope this helps!

Related

email check and comma separated check jQuery codes work correctly in separation but not when mixed for disabling the submit button

I have to functions that each of them looks for an email being valid and the other one looks for an input being comma separated (not sure if I have the best comma separated jQuery code).
However, when I write a proper comma separated input in the input box, it still allows me to click on the submit button which is strange because the email is empty and I expect the submit button to stay disabled. Each of these two functions work correctly separately.
$("#category_names").on('keyup', function (event) {
$(".error").hide();
let hasError = false;
let isValid = true;
$('#category_names').each(function() {
if (($.trim($(this).val()).indexOf(",") == -1)) {
//alert('Please separate multiple keywords with a comma.');
$('#commaerror').show();
hasError = true;
} else {
$('#commaerror').hide()
hasError = false;
}
});
$('button[type="submit"]').prop('disabled', hasError);
})
$("#email").on('keyup', function() {
$(".error").hide();
let hasError = false;
let emailReg = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
let emailAddressVal = $(this).val();
if (emailAddressVal == '') {
$("#email").after('<span class="error">Please enter your email address.</span>');
hasError = true;
}
else if (!emailReg.test(emailAddressVal)) {
$("#email").after('<span class="error">Enter a valid email address.</span>');
hasError = true;
}
$('button[type="submit"]').prop('disabled', hasError);
})
You need to check both (all, if there are other inputs as well) conditions are valid in each event handler. It's probably simplest to put all the validation code into a function and call that in the event handler. For example:
function validate() {
$(".error, #commaerror").hide();
let hasError = false;
// validate category names
$('#category_names').each(function() {
if ($(this).val().indexOf(",") == -1) {
//alert('Please separate multiple keywords with a comma.');
$('#commaerror').show();
hasError = true;
}
});
// validate email
let emailReg = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
let emailAddressVal = $('#email').val();
if (emailAddressVal == '') {
$("#email").after('<span class="error">Please enter your email address.</span>');
hasError = true;
}
else if (!emailReg.test(emailAddressVal)) {
$("#email").after('<span class="error">Enter a valid email address.</span>');
hasError = true;
}
return hasError;
}
$("#category_names, #email").on('keyup', function(event) {
$('button[type="submit"]').prop('disabled', validate());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-3">
<label data-error="wrong" data-success="right">Enter your email</label>
<input type="email" class="form-control validate purple-border" id="email">
<br/>
<label>Enter categories:</label>
<input type="text" id="category_names" />
<br/>
</div>
<button type="submit" class="btn btn-purple purple-border" disabled="disabled">Perform Frame Classification</button>
Note I've cleaned up your category_names code a bit, but you need to add further checking so that if there is only one value it still passes, or that something like abc, doesn't pass. You might find something like
$(this).val().match(/^\s*\w+(\s*,\s*\w+)*\s*$/)
more useful.

Button hide and show reset

Hello I have a question I want to reset button if the fields are empty, the reset is not displayed if a value is entered, the reset is displayed
Reset
for jquery ill recomand this
$( "input" ).change(function() {
var val = $(this).val();
if(val == ""){
$("#yourresetbutton").hide();
}else{
$("#yourresetbutton").show();
}
});
I'm not sure what you mean by this but I take it you want a button to appear only if a textbox if left blank. If so you this is some code you can use:
<input id='textbox' type='text'></input>
<button id='resetButton'></button>
setInterval(checkText, 10)
function checkText() {
if(document.getElementById('textbox').value == '') {
document.getElementById('resetButton').visibility = 'visible';
}
else() {
document.getElementById('resetButton').visibility = 'hidden';
}
}

Get the value of all checkbox when checkall checkbox is checked

I'am new to angularjs, I'm creating an application of attendance. When i check the checkall checkbox all the checkbox of name is also check and What i really wanted to achieve is to get the value of checked checkboxes. I'm done with checking all checkboxes. I just want to store all the value of checkboxes in an array. I can only get data when i check those checkboxes one by one. Thank you in advance.
In my html here is my code.
<ion-checkbox ng-model="Selected" ng-click="checkAll()">
<div class="wew">
Check All Checkbox
</div></ion-checkbox>
</label></div>
<table><tr><th><center>
List of Names
</center></th>
<th colspan="3">
Actions
</th></tr><tr><td><label>
<ion-checkbox ng-repeat="role in roles" ng-model="isChecked" ng-
change="format(isChecked,role,$index)"><div class="wew">
{{role}}
</div></ion-checkbox>
</label></td>
And in my controllers code. First this is my code where i get the list of names.
$http.post(link1, {section: section}).success(function(attendance){
for(a = 0; a<attendance.length; a++){
$scope.roles = [
attendance[0].Full_Name,
attendance[1].Full_Name,
attendance[2].Full_Name,
attendance[3].Full_Name,
attendance[4].Full_Name,
attendance[5].Full_Name,
attendance[6].Full_Name,
attendance[7].Full_Name,
attendance[8].Full_Name,
attendance[9].Full_Name,
]
}
})
.error(function(err) {
console.log(err)
})
And this is my code where i wanted to execute the checkall and automatically store the data in $scope.selected = []; if i click the check all checkbox..
$scope.checkAll = function () {
if ($scope.Selected) {
$scope.Selected = false;
} else {
$scope.Selected = true;
}
$scope.isChecked= $scope.Selected;
$scope.selected = [];
$scope.format = function (isChecked, role, $index) {
if (isChecked == true) {
$scope.selected.push(role);
}
else {
var _index = $scope.selected.indexOf(role);
$scope.selected.splice(_index, 1);
}
var students = $scope.selected;
console.log(students);
}
}
try this code
<script>
$(function(){
var numbers = $("input[type='checkbox']:checked").map(function(_, el) {
return $(el).val();
}).get();
});
</script>

cleaning up angularjs controller and function - for drop down list with multi select custom made

how can I clean up this code. here is my function in my controller. I am using this to control 3 different custom made drop down lists in my html. I want when you click out of them, they close. I wish I didn't need to give them each their own variable for ng-show, but I think I do because if ng-show is true for one then they would all show together. Also is it wrong to put all this in a controller rather than a directive or factory or something - besides for cleaning up the function itself.
$scope.toggle = function (option,type) {
if (option == 'subdiv') {
$scope.notMain = true;
if ($scope.notMain) {
if (type == 'item') {
$scope.showItemOptions = true;
$scope.showOptions = false;
$scope.showOrderOptions = false;
}
else if (type == 'style') {
$scope.showOptions = true;
$scope.showItemOptions = false;
$scope.showOrderOptions = false;
}
else if (type == 'order') {
$scope.showOrderOptions = true;
$scope.showItemOptions = false;
$scope.showOptions = false;
}
}
}//end of if subdiv
else if (option == 'maindiv') {
if (!$scope.notMain) {
$scope.showItemOptions = false;
$scope.showOptions = false;
$scope.showOrderOptions = false;
}
$scope.notMain = false;
}//end of if maindiv
};
here is the html for just one of the drop downs (but they are all the same with different variables:
<div class="dropdownlistheader" ng-click="toggle('subdiv','order')">
<input type="text" readonly="readonly" class="dropdownlistinput" value="{{selectedOrderValuesDisplay}}" /> </div>
<div id="ddl123" ng-show="showOrderOptions" class="dropdownlist">
<div ng-show="showOrderOptions" ng-repeat="option in OrdersDDL">
<label> <input type="checkbox" ng-model="selected[$index]" ng-click="toggleOrderSelection(option.Number, option.Details)"> {{option.Details}}</label> </div></div>
I changed the whole thing to one function. was able to do it easily by changing the ng-show variables to contain the text of of the name of the ddl that should show instead of bool like they were.
$scope.toggle = function (option,type) {
if (option == 'subdiv') {
$scope.notMain = true;
if ($scope.notMain) {
$scope.showDDL = type;
}
}
else if (option == 'maindiv') {
if (!$scope.notMain) {
$scope.showDDL = '';
}
$scope.notMain = false;
}
};
in html: ng-show=="type" //type is hardcoded in html

HTML 5 make Checkbox required

I have multiple checkbox on my form. I need to make user to select atleast one checkbox or else the form will not submit. I tried required attribute but it checks if user has checked all check boxes.
How to do this?
One solution is to add required attributes to all the checkboxes, and when at least one of these checkboxes is checked, remove the required attributes for all the checkboxes using jQuery.
var requiredCheckboxes = $(':checkbox[required]');
requiredCheckboxes.change(function(){
if(requiredCheckboxes.is(':checked')) {
requiredCheckboxes.removeAttr('required');
}
else {
requiredCheckboxes.attr('required', 'required');
}
});
DEMO
You can use Javascript that just loops through your html checkboxes and sets the Variable issomethingchecked to TRUE if at least one of them is cheched .
demo http://jsfiddle.net/updpxrfj/2/
var checkboxElements = document.getElementsByTagName("input");
var issomethingchecked = false;
for(var i = 0; i < checkboxElements.length; i++) {
if(checkboxElements[i].type.toLowerCase() == 'checkbox') {
if (checkboxElements[i].checked == true) {
issomethingchecked = true;
}
}
if (issomethingchecked === true) {
// Continue with submiting the form
console.log(issomethingchecked);
}
}