primeng icon indicating copy to clipboard operation
primeng copied to clipboard

[p-splitButton] Default button opens dropdown

Open mgutbor opened this issue 7 years ago • 17 comments

should be possible to open dropdown menu when click on the default button???

thanks in advance.

mgutbor avatar Dec 14 '18 08:12 mgutbor

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.

JacobSiegle avatar Jan 03 '19 14:01 JacobSiegle

Please use forum for questions and help requests.

yigitfindikli avatar Jan 21 '19 10:01 yigitfindikli

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.

IRCraziestTaxi avatar Mar 20 '19 19:03 IRCraziestTaxi

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.

JacobSiegle avatar Mar 20 '19 20:03 JacobSiegle

fyi I workaround this by doing something like:

<p-splitButton #ua label="foo" (onClick)="ua.onDropdownButtonClick(null)" [model]="actions"></p-splitButton>

wvdhaute avatar Sep 05 '19 14:09 wvdhaute

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.

apunekar avatar Aug 26 '21 15:08 apunekar

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 avatar Oct 27 '22 19:10 lucafulgenzi

@lucafulgenzi The workaround does not work any more. I did not notice when it had stopped working. My curr version is 17.3.1

buchatsky avatar Jan 06 '24 14:01 buchatsky

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?

davidmolloydcu avatar Jan 11 '24 12:01 davidmolloydcu

@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

image

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 ?

NavneetVaid avatar Jan 19 '24 12:01 NavneetVaid

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 avatar Jan 19 '24 12:01 davidmolloydcu

@davidmolloydcu

Do you have any working demo ? and what is the rowData? How you are toggling the menu on click of button ?

NavneetVaid avatar Jan 19 '24 13:01 NavneetVaid

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. image

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

davidmolloydcu avatar Jan 19 '24 14:01 davidmolloydcu

Thanks @davidmolloydcu Working like a charm ! Cheers

NavneetVaid avatar Jan 22 '24 08:01 NavneetVaid

@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

image 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); }

Andrew-Rodn avatar Mar 13 '24 15:03 Andrew-Rodn

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

javierarpa avatar Apr 10 '24 10:04 javierarpa

@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 image 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;
    }

rafacompanyus avatar Nov 02 '24 21:11 rafacompanyus