Prevent expand on click on prime ng accordion - angular6

I have an primeng accordion and the accordion header has a checkbox control . Whenever i check/uncheck checkbox , the accordion tab is getting open/closed automatically
Is there any way to prevent expand/collapse on click on checkbox ? It should expand/collapse on header other than check/uncheck check box ?

This is happening due to Event Bubbling. For this need to stop eventPropagation by calling stopPropagation() of MouseEvent.
Accordion html code sample
<p-accordion>
<p-accordionTab header="Godfather I" [selected]="true">
<p-header>
<span>Godfather I
<input type="checkbox" (click)="onClick($event)">
</span>
</p-header>
Some Text
</p-accordionTab>
</p-accordion>
Corresponding component ts code.
onClick($event: MouseEvent){
$event.stopPropagation();
}
For Reference added stackblitz code sample.

This is how I solved this issue. This is happening because of Event Bubbling. So when you click on child element. Event propagate to its parent and so on. So Just use stop propagation on event. It will prevent the click event on your accordion. Below code for your reference.
Accordian with Check box code I used (onChange) method.
<p-accordionTab>
<p-header>
<div class="ui-g" style="width:250px;margin-bottom:10px">
<div class="ui-g-12"><p-checkbox name="group1" #ck value="New York" label="New York" [(ngModel)]="selectedCities" (onChange)="checkChange($event)" inputId="ny"></p-checkbox></div>
</div>
</p-header>
</p-accordionTab>
component.ts
selectedCities: string[] = [];
//Simply you have to to stop propogation here.
checkChange(e:any){
console.log(e); // true or false.
event.stopPropagation(); // component will have direct access to event here.
}

Related

Space keypress on an HTML Label interpreted as checkbox click

When I add 'click' event handler on an HTML checkbox with a label, then space keypress on the label causes the checkbox's click event handler to be executed. I would like to prevent that. (The checkbox itself is hidden / not visible - this is done in some version of Bootstrap framework used in my code).
HTML:
<body>
<label for="checkbox" id="label">Click on me, I am Label for <strong>hidden</strong> Checkbox</label>
<!-- checkbox is not visible -->
<input type="checkbox" id="checkbox" style="opacity: 0" />
<!-- to be filled by JavaScript code -->
<div id="checkboxValue"></div>
</body>
JavaScript code:
import { fromEvent } from 'rxjs';
const checkbox: HTMLInputElement = document.querySelector('#checkbox');
const checkboxValue: HTMLDivElement = document.querySelector('#checkboxValue');
function updateCheckbox() {
checkboxValue.innerHTML = `Checkbox ${
checkbox.checked ? '✔️ (checked)' : '❌ (unchecked)'
}`;
}
// event handler for the checkbox being executed also for Space keypress:
fromEvent(checkbox, 'click').subscribe((event) => {
console.log('checkbox CLICK -------')
updateCheckbox();
});
updateCheckbox();
Open the Stackblitz console, click the Click on me, I am Label for hidden Checkbox.
Then press the space key and see the click handler being executed.
Demo on Stackblitz.
If a label has keyboard focus, pressing the Space key also raises the Click event.
If you want to remove focus, you can add a blur event (on the label) to remove focus so spacebar won't function to click the button :
e.target.blur();

How to disable item from different component in Angular

So I have Tags component and I imported that component in my Chart-details component. In my Chart Details component I have a checkbox that will disable all the input box, drop down box, buttons that are located inside chart-details page but some reason my imported tags component is not disabling when I click the checkbox. Any suggestion or help on how to fix this so that a user cannot add or remove tags when the checkbox is clicked will be really appreciated.
Chart Details Component. HTML
//Check box to disable all the input, drop down, buttons
<mat-checkbox *ngIf="chart && workspace" color="primary" [disabled]="this.workspace.type === WorkspaceType.user"
[(ngModel)]="chart.isPublished" (ngModelChange)="publishChange($event)">Published</mat-checkbox>
//Example Button
<button color="primary" mat-flat-button (click)="saveClick()" [disabled]="this.chart.isPublished">Save</button>
// Imported Tags Component
<mc-tags [_normalTags]="chart.tags" [removable]="true" [selectable]="true"
(added)="tagAdded($event)" (removed)="tagRemoved($event)" [disabled]="this.chart.isPublished" >
</mc-tags>
I have added [disabled]="this.chart.isPublished" but I got an error saying "Can't bind to 'disabled' since it isn't a known property of 'mc-tags'. ". Also I tried (disabled) but still not working and user can still add or remove tags even though checkbox is checked.
Tags Component.HTML
<mat-chip-list #chipList [disabled]="true">
<mat-chip *ngFor="let chip of normalTags" [selectable]="selectable"
[removable]="removable"
(removed)="removeTags(chip)">
{{chip.tag}}
</mat-chip>
<input matInput #input [(ngModel)]="tagIn" [formControl]="tagCtrl2"
[matAutocomplete]="auto" />
</mat-chip-list>
Right now I have to do [disabled]="true" on mat-chip-list in Tags component.html so that user can't add or remove it. I don't want to hard code this and want to control this using Chart Detail Component Checkbox.
The disable matchip and input look like this (PIC)
It's not gonna run but I've attached the whole code for these two component over here https://stackblitz.com/edit/angular-ivy-wwfcai . thanks
Have you declared "#Input() disabled" in your child component class?
In Child.component.ts
#Input() disabled: any;
ngOnInit() {
if(disabled){
//set properties to true/false accordingly
}
}
In Parent.component.html
[disabled]="this.workspace.type === WorkspaceType.user"

Quasar QTable Clicking an action button on clickable row

I am using quasar framework for my project and I have a problem on qtable component. I made table rows clickable by #row-click event. That's works fine but I have some action buttons cell on my table and when I click any action button, first #row-click event triggered.. I need to give an exception acitons body cell.. How can I do that?
I found the solution for my problem with method below;
<q-table :data="listProjects(this.$route.params.id)" :columns="columns" row-key="id" #row-click="rowClicker" selection="multiple" :selected.sync="selectedItems">
rowClicker (e, row) {
if (e.target.nodeName === 'TD') {
this.$router.push('project/parts/' + row.id)
}
}
All clicks come from TD element except my button's click. If it is triggered by TD element it will route, if not it will not.
You can use .stop on click and it will only trigger the button click event of actions.
<q-btn icon="info" #click.stop="btnclick" dense flat/>
Example : -
<q-table
title="Treats"
:data="data"
:columns="columns"
row-key="name"
#row-click="onRowClick"
>
<template v-slot:body-cell-name="props">
<q-td :props="props">
<div>
<q-badge color="purple" :label="props.value"></q-badge>
<q-btn icon="info" #click.stop="btnclick" dense flat/>
</div>
</q-td>
</template>
</q-table>
codepen - https://codepen.io/Pratik__007/pen/oNjQYBW
Type Below Code WhereEver you Want to Stop the click Action.
<q-btn v-on:click.stop="onClickFunction"/>
This code will cause Click event to stop the parent click function and run it's own Click Function Specified on that Component

Toggle md-button class on click event

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.

Angular 2 - Handle click on disabled input

This is what I have right now. Trying to call the checkToEnable function.
<input type="text" ([ngModel])="city.arr.date" [id]="city.id+'_arr_date'" [name]="city.id+'_arr_date'" [attr.disabled]="selectedTripType=='OT' ? true : null" class="input-icon-date input-default-last form-control" (click)="checkToEnable()" placeholder="Return Date"/>
Disabled elements don't fire mouse events. Most browsers will
propagate an event originating from the disabled element up the DOM
tree, so event handlers could be placed on container elements.
But you can achieve it by this way :
Component Side:
disableTextbox = false;
toggleDisable() {
this.disableTextbox = !this.disableTextbox;
}
Template side :
<div (click)='toggleDisable()'>
<input [disabled]='disableTextbox' >
</div>
WORKING DEMO