ngbDropdownMenu.close() on multiple dropdowns - html

I'm working on a data visualizer project. In this project users need to be able to make look ups to make it easy to find cretin data quickly. To do this I'm am using ngbDropdownMenu from NgBootstrap. They click a button and a little dropdown menu pops up so they can make a new look up.
When the menu pops up. They give a name and description to the new look up and hit submit. It will then create a new look up and fire an event I made so that I can close the dropdown menu NgbDropdownMenu.close();
HTML for the dropdown menu
<div ngbDropdown id="caseCodeDropdown"
(openChange)="openChange($event);"
class="input-group-append d-inline-block col-sm-1 px-0 mx-0">
<button ngbDropdownToggle class="btn btn-sm btn-outline" type="button" >
<i class="fal fa-plus"></i>
</button>
<div ngbDropdownMenu>
<newLookUpComponent></newLookUpComponent>
</div>
</div>
Component for dropdown menu
#ViewChild(NgbDropdownMenu) caseCodeDropdown: NgbDropdownMenu;
public dropdownCloseSub: Subscription = new Subscription;
constructor(private _eventEmitterService: EventEmitterService) { }
public ngOnInit(): void {
this.dropdownCloseSub = this._eventEmitterService.invokeSubmitNewMatterCode.subscribe(() => {
this.caseCodeDropdown.dropdown.close();
});
}
This works perfectly. The problem is when I need to have more than one dropdown menus on one page. I have five look ups for one of the pages and when I use the code below it only works for the caseCodeDropdown which is the first dropdown from the top of the html doc.
HTML with more than one dropdown menu
<div ngbDropdown id="caseCodeDropdown"
(openChange)="openChange($event);"
class="input-group-append d-inline-block col-sm-1 px-0 mx-0">
<button ngbDropdownToggle class="btn btn-sm btn-outline" type="button" >
<i class="fal fa-plus"></i>
</button>
<div ngbDropdownMenu>
<newLookUpComponent></newLookUpComponent>
</div>
</div>
<div ngbDropdown id="fileNoDropdown"
(openChange)="openChange($event);"
class="input-group-append d-inline-block col-sm-1 px-0 mx-0">
<button ngbDropdownToggle class="btn btn-sm btn-outline" type="button" >
<i class="fal fa-plus"></i>
</button>
<div ngbDropdownMenu>
<newLookUpComponent></newLookUpComponent>
</div>
</div>
<div ngbDropdown id="OPCDropdown"
(openChange)="openChange($event);"
class="input-group-append d-inline-block col-sm-1 px-0 mx-0">
<button ngbDropdownToggle class="btn btn-sm btn-outline" type="button" >
<i class="fal fa-plus"></i>
</button>
<div ngbDropdownMenu>
<newLookUpComponent></newLookUpComponent>
</div>
</div>
Component with more than one dropdown menu
#ViewChild(NgbDropdownMenu) caseCodeDropdown: NgbDropdownMenu;
#ViewChild(NgbDropdownMenu) fileNoDropdown: NgbDropdownMenu;
#ViewChild(NgbDropdownMenu) OPCDropdown: NgbDropdownMenu;
public dropdownCloseSub: Subscription = new Subscription;
constructor(private _eventEmitterService: EventEmitterService) { }
public ngOnInit(): void {
this.dropdownCloseSub = this._eventEmitterService.invokeSubmitNewMatterCode.subscribe(() => {
this.caseCodeDropdown.dropdown.close();
this.fileNoDropdown.dropdown.close();
this.OPCDropdown.dropdown.close();
});
}
WHAT I HAVE TRIED
First I tried
#ViewChild("caseCodeDropdown") caseCodeDropdown: NgbDropdownMenu;
#ViewChild("fileNoDropdown") fileNoDropdown: NgbDropdownMenu;
#ViewChild("OPCDropdown") OPCDropdown: NgbDropdownMenu;
Using #ViewChild() passing in the id of the element left all the caseCodeDropDown, fileNoDropdown, OPCDropdown as undefinded. I was unable to close them.
Next
var target = document.getElementId('caseCodeDropdown');
The problem with this method is I'm unable to use the ngbDropdownMenu functions. target.dropdown.close() throws an error.
Is there any other way I can get the element by its ID and still use the ngbDropdownMenu functions.
Thanks for your time!

Related

How to add buttons on the header of bootstrap accordian in such a way that clicking it that wont collapse or expand it?

Here I added 2 buttons on the header of the accordian, but when I click the button the entire accordian collapses or expands. Is there a way to add buttons to it in such a way that clicking them won't collapse or expand the accordion?
<div class="accordion-item">
<h2 class="accordion-header" id="panelsStayOpen-headingTwo">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
data-bs-target="#panelsStayOpen-collapseTwo" aria-expanded="false"
aria-controls="panelsStayOpen-collapseTwo">
<div>26-08-21</div>
<div style="margin-left: -20px;" class="col-1"></div>
<div>project_id</div>
<div class="col-3"></div>
<div>title</div>
<div class="col-3"></div>
</button>
<button type="button" class="btn btn-sm btn-success">
<em class="far fa-edit"></em> Edit
</button>
<button type="button" class="btn btn-sm btn-danger">
<i class="far fa-trash-alt"></i> Delete
</button>
</h2>
<div id="panelsStayOpen-collapseTwo" class="accordion-collapse collapse"
aria-labelledby="panelsStayOpen-headingTwo">
<div class="accordion-body">
<br>
<button style="margin-left: 20px;" type="button" class="btn btn-sm btn-outline-primary">Download CSV</button>
</div>
</div>
</div>
With the on click of buttons, you need to use $event.stopPropagation()
public onButtonClick($event): void {
// Do something on click
$event.stopPropagation()
}

PrimeNG p-table doesn't show anything but the name of the table

I've updatet PrimeNG from p-dataTable to p-table due to the upgrade of Angular to version 9.
I've a table users
users = ['one','two','three']
And I want to see them on the screen on rendering of the view, so I have a HTML
<p-table [value]="users" [scrollable]="true"
scrollHeight="45vh">
<ng-template pTemplate="caption">Users</ng-template>
<div class="row" style="padding-left:80px">
<ng-template pTemplate="body">
<tr>
<div class="col-sm-4">
<!--ADD USERS-->
<button type="button"
class="btn btn-success btn-sm btn-space center-block custom-font"
(click)="popup.openModal(); popup.refreshTree()">
<span class="glyphicon glyphicon-plus"></span>
</button>
</div>
<div class="col-sm-4">
<!--REMOVE USERS-->
<button type="button"
class="btn btn-danger btn-sm btn-space center-block custom-font"
(click)="popup.deleteList(); popup.refreshTree(); clearIdsArray();">
<span class="glyphicon glyphicon-trash"></span>
</button>
</div>
</tr>
</ng-template>
</div>
</p-table>
And then the [value] would be updated based on the action in popup mentioned in buttons.
The problem is I can not see any button on the screen and have no idea why. All I see is "Users" nothing more
Try to remove div which includes <ng-template pTemplate="body">
You can find basic example for templating here.

Reactive Forms, trigger three click events uniquely.1.To submit form 2.To send data from dropdown 3. Same as two, in same Reactive Form

I have written a Reactive form. The submit button is supposed to be outside the Reactive form.It has also got two dropdowns. In this form im reading data dynamically from api. I have written click events in the dropdown as well(for a purpose to bind that particular value when i submit the form). The issue here is that whenever i click on the dropdown...the submit button click event is getting triggered....i have been trying to make them(the three click events) be unique...but im unable to.... kindly help me. Below im providing my code.
<div class="container content-box-shadow tiles-top-spacing tiles-page">
<div class="row assessment-m-b">
<div class="col-lg-12 col-md-12 pt-3 pb-5">
<div class="d-flex align-items-center justify-content-between">
<span class="common-headding">{{pageText?.accountSettingPageTextData?.editHeader}}</span>
<div>
<button type="submit" class="common-button green"
(click)="onSubmit()">{{pageText?.accountSettingPageTextData?.save}}</button>
<button type="button"
class="common-button orange" (click)="previousModule()">{{pageText?.accountSettingPageTextData?.cancel}}</button>
</div>
</div>
<div class="account-settings-block">
<form *ngIf="personalData" [formGroup]="accountSettingsForm" >..................................................
<!-- Account-Settings Security Questions -->
<div *ngIf=" personalData?.dashBoardPersonalInfoData?.securityQuestionsAnswers.isVisible"
class="account-form-inner-block">
<legend>{{pageText?.accountSettingPageTextData?.questions?.header}}<span class="required">**</span></legend>
<div class="row">
<div class="col-md-6">
<div>
<label for="question1">
{{pageText?.accountSettingPageTextData?.questions?.question1}}
</label>
<div class="btn-group w-100 pt-1" ngbDropdown>
<button class="btn btn-secondary dropdown-toggle w-100 text-left"
aria-haspopup="true" aria-expanded="false" id="dropdownMenu1"
ngbDropdownToggle>
<span *ngIf="!SecurityQuestion1.question">
Select a Question
</span>
<span *ngIf="SecurityQuestion1.question">
{{SecurityQuestion1.question}}
</span>
</button>
<div class="dropdown-menu dropdown-menu-left" aria-labelledby="dropdownMenu">
<button (click)="selectQuestion1(questions)"
*ngFor="let questions of personalData?.dashBoardPersonalInfoData?.securityQuestions"
class="dropdown-item">{{questions?.question}}
</button>
</div>
</div>
</div>
<div>
</div>
</div>
</div>
<div class="col-md-6">
<div>
<label for="question2">
{{pageText?.accountSettingPageTextData?.questions?.question2}}
</label>
<div class="btn-group w-100 pt-1" ngbDropdown>
<button class="btn btn-secondary dropdown-toggle w-100 text-left"
aria-haspopup="true" aria-expanded="false" id="dropdownMenu2"
ngbDropdownToggle>
<span *ngIf="!SecurityQuestion2.question">
Select a Question
</span>
<span *ngIf="SecurityQuestion2.question">
{{SecurityQuestion2.question}}
</span>
</button>
<div class="dropdown-menu dropdown-menu-left" aria-labelledby="dropdownMenu">
<button (click)="selectQuestion2(questions)"
*ngFor="let questions of personalData?.dashBoardPersonalInfoData?.securityQuestions"
class="dropdown-item">{{questions?.question}}
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
I could solve my issue above by using form="myForm" attribute in the save button. and using id="myForm" attribute in the form tag.....just by using those attributes....i could have unique trigger for all the three events......Im stilll keeping posted so that someone neednot have to waste almost 4hours or more in finding it out....

ngbdropdown menu doesn't close after I clicked one of its items

I used ngb dropdown to display the different statuses that my task can have ('to do', 'in progress', 'done'). Everything works fine, but there is one small issue that's still bothering me. After clicking one of the options I want the dropdown menu to close. At the moment it remains open. How can I close this menu when I click on it?
as you can see below I changed the status on two posts, but the dropdown menu remains open, which is not really something I want
Template code
<div class="col-md-4 text-right padding-topright" style=" object-fit: cover;">
<div ngbDropdown class="d-inline-block">
<button class="btn btn-sm kbn-todo" *ngIf="post.task.status == 'to do'" id="dropdownDone" style=" color: white;"
ngbDropdownToggle>{{post.task.status}}</button>
<button class="btn btn-sm kbn-working" *ngIf="post.task.status == 'in progress'" id="dropdownDone" style=" color: white;"
ngbDropdownToggle>{{post.task.status}}</button>
<button class="btn btn-sm kbn-done" *ngIf="post.task.status == 'done'" id="dropdownDone" style=" color: white;"
ngbDropdownToggle>{{post.task.status}}</button>
<div ngbDropdownMenu aria-labelledby="dropdownToDo">
<button class="dropdown-item pumpkin-orange-bg" (click)="OnMarkTaskToDo(i, this.post._id)">To Do</button>
<button class="dropdown-item" (click)="OnMarkTaskInProgress(i, this.post._id)">In Progress</button>
<button class="dropdown-item" (click)="OnMarkTaskCompleted(i, this.post._id)">Done</button>
</div>
</div>
<p class="small font-weight-bold" style="margin-top: 5px" *ngIf="post.task?.due_to != 'Invalid date'"> due {{post.task?.due_to | date}}</p>
<!-- <p class="small font-weight-bold" style="margin-top: 5px"> status- {{post.task?.status}}</p> -->
</div>
You can do something like this:
<div class="d-inline-block" ngbDropdown #myDrop="ngbDropdown">
<button class="btn btn-outline-primary mr-2" id="dropdownManual" ngbDropdownAnchor (focus)="myDrop.open()">Toggle dropdown</button>
<div ngbDropdownMenu aria-labelledby="dropdownManual">
<button ngbDropdownItem>Action - 1</button>
<button ngbDropdownItem>Another Action</button>
<button ngbDropdownItem>Something else is here</button>
</div>
</div>
<button class="btn btn-outline-danger mr-2" (click)="myDrop.close();">Close from outside</button>
Take a closer look on:
#myDrop="ngbDropdown"
And:
(click)="myDrop.close();"
I suppose there are many people which would like this solution.
This is an example taken from: https://ng-bootstrap.github.io/#/components/dropdown/examples
If you question is about CSS/HTML, you just need to apply something as display: none in your menu container.
But you you question is about Angular, you will need something like this:
showMenu: boolean = false;
closeMenu() {
let displayMenu = showMenu ? true : false;
return displayMenu;
}
<div class="menu-container" *ngIf="showMenu">
<button class="close-menu" (click)="closeMenu()"></button>
</div>
This process can be done in very different ways and everything it will depend how you want to work, with animation, with a global event to click "out" of the menu and then the menu disappears, and on...
Anyway I hope these examples can make your path more clear.

How to make an increment/decrement input inside dropdown?

So my goal is to make an dropdown with decrement/increment button like below.
https://imgur.com/A4zleSX
and these are my current codes
<div class="dropdown">
<button class="btn border-0 dropdown-toggle form-control" type="button"
id="dropdownMenu2" data-toggle="dropdown" aria-expanded="false">
Dropdown</button>
<div class="dropdown-menu" style="width: 250px; background-color: #343a40"
aria-labelledby="dropdownMenu2">
<div class="dropdown-item">
<div class="row text-white">
<div class="col-md">
<i class="fa fa-user mr-2"></i>Adult</div>
<div class="col-md text-center">
<button class="btn btn-sm btn-success mx-2">+</button>
<span>1</span>
<button class="btn btn-sm btn-success mx-2">+</button>
</div>
</div>
</div>
</div>
So i've managed to make till this point, but i have problems when the button is clicked inside the dropdown, it scrolls to page the top and i also don't like the background color changes when the dropdown item is hovered.
please help me to make it happen, thank you
UPDATE:
so all left is preventing the page to scroll when the button inside dropdown is clicked, can anyone help please?
Firstly you need to keep on dropdown when user click anything in it.
JS:
$('.keep-open').on({
"shown.bs.dropdown": function() { $(this).attr('closable', false); },
"click": function() { },
"hide.bs.dropdown": function() { return $(this).attr('closable') == 'true'; }
});
$('.keep-open #dLabel').on({
"click": function() {
$(this).parent().attr('closable', true );
}
})
html:
<div class="dropdown-menu" role="menu" aria-labelledby="dLabel" style="width: 250px; background-color: #343a40" >
<div style="display: block;text-align:center;margin-bottom:5px;margin-top:5px">
<div style="display: inline-block;color:white">
<i class="fa fa-user mr-2" style="margin:15px;"></i>Adult</div>
<button class="btn btn-sm btn-success mx-2">-</button>
<span>1</span>
<button class="btn btn-sm btn-success mx-2">+</button>
</div>
<div style="display: block;text-align:center;margin-bottom:5px;">
<div style="display: inline-block;color:white;">
<i class="fa fa-user mr-2" style="color:white;margin:15px;"></i>Child</div>
<button class="btn btn-sm btn-success mx-2">-</button>
<span>1</span>
<button class="btn btn-sm btn-success mx-2">+</button>
</div>
</div>
plunker: http://plnkr.co/edit/s4nmZVXpLMzZLDSzVd24?p=preview