chris01
chris01

Reputation: 12321

PrimeNG, SplitButton: How to get a parameter into the model?

Lets say I want to trigger functions foo and bar by a PrimeNG SplitButton. Both have one parameter regarding the button itself.

Regarding the action I want to have parameter for the assigned function.

I can do it for foo because I set it with onClick.

But how can I do it for bar??

    <p *ngFor="let x of ['aaa','bbb','ccc']">
      <p-splitButton label="FOO for {{x}}" (onClick)="foo (x)" [model]="cmds"></p-splitButton>
    </p>
    cmds : any = [{label: "BAR for x", command: () => { bar (x); }}];   // x is not here, how can I get it?

Upvotes: 3

Views: 1859

Answers (3)

yasinfazli
yasinfazli

Reputation: 21

you can use onDropdownClick method then update your model model

setItems(id: any){
    this.items =  [
      {label: '1', command: () => console.log(id)},
      {label: '2', command: () =>  console.log(id)}
    ]
  }
<p-splitButton [model]="items" (onDropdownClick)="setItems(id)"></p-splitButton>

Upvotes: 2

jlgranda
jlgranda

Reputation: 689

xxx.html
<p-splitButton icon="pi pi-print" label="Imprimir"
(onClick)="imprimirFactura(rowData)" [model]="cmds(rowData)"></p-splitButton>
//xxx.componente.ts
    cmds(row): any {
        const items = [
            {
                label: 'Acción 1', 
                icon: 'pi pi-clone', command: () => {
                    this.dummy(row);
                }
            },
            {
                label: 'Acción 2', 
                icon: 'pi pi-clone', command: () => {
                    this.confirmarPagoFactura(row);
                }
            },
        ];
        return items; (x); }}];   
    }

Upvotes: 1

chris01
chris01

Reputation: 12321

Found out I can bind a function there too.

    <p *ngFor="let x of ['aaa','bbb','ccc']">
      <p-splitButton label="FOO for {{x}}" (onClick)="foo (x)" [model]="cmds (x)"></p-splitButton>
    </p>
    cmds (x) : any
    {
      return [{label: "BAR for "+x, command: () => { bar (x); }}];   
    }

Upvotes: 5

Related Questions