[p-splitButton] Default button opens dropdown
should be possible to open dropdown menu when click on the default button???
thanks in advance.
An regular button which triggers an overlay panel with a dropdown inside seems more intuitive to me.
If you wanted to do it the way described I think it would work to get a ref to the component and catch the command button being clicked via "onClick" and then use the ref to the component to manually call "show" which will show the dropdown.
Please use forum for questions and help requests.
I think the original request is a perfectly valid issue. I am surprised triggering the SplitButton's dropdown with a click on the "main button" is not supported.
I think the issue is poorly worded which lead to it being closed. I can see how it looked like a help request. You can always open a new issue and reference this one or PR it yourself if you would like.
fyi I workaround this by doing something like:
<p-splitButton #ua label="foo" (onClick)="ua.onDropdownButtonClick(null)" [model]="actions"></p-splitButton>
Used @wvdhaute workaround to use it for me and added a extra step by calling one more function on (onClick)
<p-splitButton #ap label="foo" icon="pi pi-plus" [model]="items" (onClick)="ap.onDropdownButtonClick(); foo(value)"></p-splitButton>
onDropdownButtonClick() will expand the drop down foo(value) - you can pass the value you want and capture it in the component.ts and the on slecting a option from drop down will work as it works by default.
Update for those using Angular 14 and PrimeNG 14.1.2, and I assume later versions as more and more restrictive code is being targeted.
<p-splitButton #ua label="foo" (onClick)="ua.onDropdownButtonClick($event)" [model]="actions"></p-splitButton>
You have to pass an event
@lucafulgenzi The workaround does not work any more. I did not notice when it had stopped working. My curr version is 17.3.1
I've come across this issue also. Same is occurring in 16.9.1. Is there an alternative or newly recommended way to have the button itself trigger the split menu opening/closing?
@lucafulgenzi The workaround does not work any more. I did not notice when it had stopped working. My curr version is 17.3.1
The workaround doesnt work anymore because they have added the code to hide menu on defaultButtonClick
So basically now the dropdown click tries to open the menu and because we are triggering dropdown click on click of default button it hides the menu.
Can we get something like where we can stop defaultbuttonclick stop closing the menu ?
I ended up just replacing split buttons with primary button opening the menu. Instead of using split buttons we are now just using a normal button with a menu:
E.g. <p-menu #menu appendTo="body" [model]="items" [popup]="true"> <button pButton type="button" (click)="onClickDropdownMenu(rowData, menu, $event)" icon="pi pi-bars" label="Options">
It's not a split button anymore but it has the same effective outcome of a button that opens a menu.
@davidmolloydcu
Do you have any working demo ? and what is the rowData? How you are toggling the menu on click of button ?
I don't have a full example I could share publicly, but this is the core scenario. The button is an options button at start of each line in a primeng table. E.g.
Depending on the data, the options are dynamic so the 'onClickDropdownMenu' method I created is primarily pushing items to the menu that will be shown.
onClickDropdownMenu(item: FinalReport, menu: Menu, event: Event) {
this.dropdownMenuItems = [];
this.dropdownMenuItems.push({label: 'View', icon: 'fa fa-fw fa-eye', command: e => this.someFunction())});
.... other menu options....
menu.toggle(event);
}
So the rowData is just for passing relevant IDs to subsequent functions. The only thing you are concerned with is the menu.toggle() I would expect.
It's basically just this: PrimeNG Menu Popup
Hope this helps
Thanks @davidmolloydcu Working like a charm ! Cheers
@lucafulgenzi The workaround does not work any more. I did not notice when it had stopped working. My curr version is 17.3.1
The workaround doesnt work anymore because they have added the code to hide menu on defaultButtonClick
So basically now the dropdown click tries to open the menu and because we are triggering dropdown click on click of default button it hides the menu.
Can we get something like where we can stop defaultbuttonclick stop closing the menu ?
Just add setTimeout. E.g. <p-splitButton [model]="menu" #actionMenu (onClick)="click(actionMenu, $event)" label="Actions" appendTo="body" >
click(event: SplitButton, eventClick: MouseEvent): void { setTimeout(() => { event.onDropdownButtonClick(eventClick) }, 0); }
I have needed a similar case where I have had to reverse the functionality. The default button that opens the menu and the other button that does the default action.
So in the ngAfterViewInit I have inverted the functions.
E.g. @ViewChild("sb") splitButton: SplitButton;
ngAfterViewInit(): void {
const onDropdownButtonClick = this.splitButton.onDropdownButtonClick;
const onDefaultButtonClick = this.splitButton.onDefaultButtonClick;
this.splitButton.onDefaultButtonClick = onDropdownButtonClick;
this.splitButton.onDropdownButtonClick = onDefaultButtonClick;
}
Hope this helps
@lucafulgenzi The workaround does not work any more. I did not notice when it had stopped working. My curr version is 17.3.1
The workaround doesnt work anymore because they have added the code to hide menu on defaultButtonClick
So basically now the dropdown click tries to open the menu and because we are triggering dropdown click on click of default button it hides the menu. Can we get something like where we can stop defaultbuttonclick stop closing the menu ?
Just add setTimeout. E.g. <p-splitButton [model]="menu" #actionMenu (onClick)="click(actionMenu, $event)" label="Actions" appendTo="body" >
click(event: SplitButton, eventClick: MouseEvent): void { setTimeout(() => { event.onDropdownButtonClick(eventClick) }, 0); }
thanks this help me! but if you want a natural behavior you can do this: HTML:
<p-splitButton (onMenuHide)="hideProfileMenu(profileBtn)" (onMenuShow)="showProfileMenu()" #profileBtn (click)="expandProfileMenu(profileBtn, $event)" styleClass="profile-btn" icon="pi pi-user" [model]="items" />
TS:
expandProfileMenu(btn: SplitButton, eventClick: MouseEvent) {
if(!this.menuIsOpen) {
setTimeout(() => {
btn.onDropdownButtonClick(eventClick);
}, 0);
}
}
hideProfileMenu(btn: SplitButton) {
setTimeout(() => {
if(!btn.isExpanded.asReadonly()()) {
this.menuIsOpen = false;
}
}, 0);
}
showProfileMenu() {
this.menuIsOpen = true;
}
So basically now the dropdown click tries to open the menu and because we are triggering dropdown click on click of default button it hides the menu.