I'm trying to get it so when one or more checkbox is clicked, I'm able to get a button to appear. I would like the toggle function to work if one or more checkbox is clicked, but instead, it will turn on when I select one checkbox, then turn off during the next selection. I'm sure I've got a couple of unnecessary properties added into here as well, but not too concerned about that. Any help would be appreciated.
HTML: Button
<button class="btn btn-primary"
*ngIf="switchCase" style="float:right"
>Save</button>
HTML: Checkbox Column
<kendo-grid-column field="checkbox" editor="boolean">
<ng-template kendoGridCellTemplate let-dataItem id="flexSwitchCheckChecked"
>
<input type="checkbox" (click)="toggleButton(dataItem, 'checkbox'
[checked]="dataItem.checkbox"
[width]="40"
>
TS: Button click method
public switchCase: boolean = false;
toggleButton() {
this.switchCase = !this.switchCase;
pass an event to your function then access its value from typescript class:
Step1(pass an $event):
on *ngFor tag level add an index var
<div *ngFor="let item of items;let i = index" >
<input type="checkbox" (change)="toggleButton($event,i)">
</div>
Step1(get its value):
toggleButton($event,i) {
let newValue = $event.target.value;
// this.switchCase =newValue;
this.items[i].checked = newValue;
}
Related
ive got a little problem with my Table. The implemented options are, that i can change the name from the item with clicking on the td or to change it by clicking on the pencil - but it does not disappear when i click on the X (means exit the input). When i'll change the name, a toast message appears, but the input field wont close (what its supposed to do).
I am attaching a screenshot of the table item and the html and ts code, just to know what is going on. Thanks in advance and happy replying! :)
input field (td)
It is the state when i have changed the name but the input field no longer closes!
<button
pButton
pRipple
class="p-button-outlined border-radius-0 bg-white p-button-icon-only p-button-default"
type="button"
id="cancel-edit-name"
(click)="cancel(group)"></button>
<button
pButton
pRipple
class="p-button-outlined p-button-icon-only bg-white border-radius-left-0 p-button-success"
type="button"
id="ok-edit-name"
(click)="onSubmit(group)"></button>
onSubmit(group: Group) {
if (group.name?.length > 0 && group.name.match('^[a-zA-Z0-9_ \-]+$')) {
group.active = false;
this.groupService.updateGroup(group).subscribe(this.messageService.observe('groups.group.edit.success'));
}
}
editGroup(group: Group) {
group.active = true;
}
cancel(group: Group) {
group.active = false;
}
<input id="group-name-edit" type="text" [(ngModel)]="group.name" pInputText (keyup.enter)="onSubmit(group)" (keyup.esc)="cancel(group)">
Did i miss something or am i just blind..
what do you mean by "the input field no longer closes" ? Do you mean lose focus ? If so you can call the blur function:
input.blur();
I am using ng-multiselect-dropdown when clicking the deselect option within the object itself the dropdown clears the selected items (visually and variable
When programmatically clearing the selectedItems the variable array itself is cleared but the selected items within the visual portion are still selected. If you were to then click within the object the values would no longer be available as a selection but visually the object does not clear.
I've tried a few options to clear I'll show here below. I've also tried to put it into a form and then manually clearing the control.In any scenario I've tried when not deselectingAll from the dropdown itself the visual portion remains cached and the selectedItems is empty.
Form:
<form [formGroup]="multiselect" (ngSubmit)="onSubmit()">
<div>
<ng-multiselect-dropdown [placeholder]=""
[settings]="settings"
[data]="propertiesCharUnique"
(onSelect)="onItemSelect($event)"
(onSelectAll)="onSelectAll($event)"
(onDeSelect)="onDeSelect($event)"
(onDeSelectAll)="onDeSelectAll($event)"
formControlName="multi"
[(ngModel)]="selectedItems"
>
</ng-multiselect-dropdown>
<input type="submit" value="Load Targets" />
</div>
#ViewChild('multiselect') multiSelect: any;
multiselect = this.formBuilder.group({
});
public reset(){
this.multiselect.get("multi")?.setValue([]);
this.multiselect.get("multi")?.reset()
this.selectedItems = [];
}
I removed the dropdown from the form and now ngModel binds properly.
<div>
<ng-multiselect-dropdown
[placeholder]="'Click here to select'"
[settings]="settings"
[data]="propertiesCharUnique"
[(ngModel)]="selectedItems"
(onSelect)="onItemSelect($event)"
(onSelectAll)="onSelectAll($event)"
(onDeSelect)="onDeSelect($event)"
(onDeSelectAll)="onDeSelectAll($event)" >
</ng-multiselect-dropdown>
<input type="submit" (ngSubmit)="onSubmit()" value="Load Targets" />
</div>
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 = "";
Execution:
I have several Material Checkboxes in a Material Dialog Component. They represent a column filter for a table:
<h1 mat-dialog-title>Filter</h1>
<div mat-dialog-content>
<ng-container *ngFor="let column of allColumns; index as i; first as firstEntry ">
<mat-checkbox *ngIf="!firstEntry" class="checkbox" [checked]="checkedList[i]" [aria-label]="column" (click)=doToggle(i)>{{column}}</mat-checkbox>
</ng-container>
</div>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()">Abbrechen</button>
<button mat-button [mat-dialog-close]="allColumns" cdkFocusInitial>Reset</button>
<button id="ok-button" mat-button [mat-dialog-close]="displayedColumns" cdkFocusInitial>Ok</button>
</div>
When I click on a Checkbox I want to check it. All booleans of the Checkboxes are saved in a boolean array named checkedList. When I click on a Checkbox the method doToggle(i) gets executed.
doToggle(i: number){
if(this.checkedList[i]){
this.displayedColumns[i] = this.allColumns[i];
}
else{
this.displayedColumns[i] = null;
}
this.checkedList[i] = !this.checkedList[i];
}
The method also fills or empties the values at the appropriate position of the column list. At the end of the method it negates the boolean at the appropriate position in the checkedList.
Problem:
I always have to click twice on the Checkbox to check or uncheck it. The values are added or removed properly. But still I have to click two times on the checkbox. Why is that?
Try sending the value of checkbox in the click event.
Add template reference variable(#ck) to checkbox and get its value ck.checked
[checked]="checkedList[i]" [aria-label]="column" #ck (click)="doToggle(i,!ck.checked)"
and in doToggle function
doToggle(i,value){
this.checkedList[i] = value;
if(this.checkedList[i]){
this.displayedColumns[i] = this.allColumns[i];
}
else{
this.displayedColumns[i] = null;
}
}
I want to use button as a selector, i have a toggle function on click event to get the value from the button click, now what i want to do is to show the user that the button is selected. That is i want to toggle class between md-primary and md-warn.
This is my button
<md-button class="md-fab" ng-click="getTime(0);"
value="0" aria-label="midnight" ng-class="{'active':
variable,'disable': !variable}">0</md-button>
This is the controller part
$scope.getTime = function (value) {
$scope.variable = !$scope.variable;
if($scope.variable){
console.log(value);
}
};
update ng-class like below:
<md-button class="md-fab" ng-click="getTime(0);"
value="0" aria-label="midnight" ng-class="{'md-primary':
variable,'md-warn': !variable}">0</md-button>
also make sure to add ngMaterial to your app module dependencies
here is working Plunkr hope it make it more clear for you.