chris01
chris01

Reputation: 12321

PrimeNG SplitButton: How to pass an argument to the sub-menu?

I try to use a PrimeNG SplitButton.

@Component({
  selector: 'test',
  template: `
  
  <ul>
    <li *ngFor="let l of lst">{{ l }}
        <p-splitButton label="foo" (onClick)="foo (l)" [model]="menu"></p-splitButton>
    </li>
  </ul>
`
})


export class TestComponent
{
    lst : string [] = ["abc", "123", "xyz"];
    menu : MenuItem [] = [{label: "bar", command: (e) => { this.bar (e) }}];

    foo (x)
    {
        console.log ("foo", x);
    }
    
    bar (x)
    {
        console.log ("bar", x);
    }
}

How can I pass the value of the iteration of lst (l) to bar? I want use it the same way as I do it with foo but not sure how to get the argument down to menu.

Upvotes: 1

Views: 1301

Answers (3)

Tarakesh Pulikonda
Tarakesh Pulikonda

Reputation: 1

const getItems = function(data) {
    return [
    {
        label: 'Update',
        icon: 'pi pi-refresh',
        "row-data": data,
        command: (d) => {
            const rowData = d.item["row-data"] || arguments[0];
            console.log(rowData);
        }
    }]    
}
......
<DataTable>
    <Column>
        <template #body="{ data }">
            <SplitButton label="Save" icon="pi pi-plus"  :model="getItems(data)" />
        </template>
    </Column>
</DataTable>

Upvotes: 0

chris01
chris01

Reputation: 12321

I figured out this.

<p-splitButton label="foo" (onClick)="foo (l)" [model]="getMenu (l)"></p-splitButton>



getMenu (x):any
{
    return [{label: "bar", command: (e) => { this.bar (x) }}];
}

Is it bad style to cover it with a function in Angular??

Upvotes: 1

Shai
Shai

Reputation: 3872

You can use the onDropdownClick event to store the index of the splitButton you're currently opening and then use it inside bar, like this:

@Component({
  selector: 'test',
  template: `
  <ul>
    <li *ngFor="let l of lst">{{ l }}
        <p-splitButton label="foo" (onClick)="foo(l)" (onDropdownClick)="dropdownClickHandler(l)" [model]="menu"></p-splitButton>
    </li>
  </ul>
`
})
export class TestComponent
{
    lst : string [] = ["abc", "123", "xyz"];
    menu : MenuItem [] = [{label: "bar", command: () => { this.bar() }}];

    currentIndex: number;

    foo (x)
    {
        console.log("foo", x);
    }
    
    bar ()
    {
        console.log("bar", this.currentIndex);
    }

    dropdownClickHandler(index) {
        this.currentIndex = index;
    }
}

Upvotes: 1

Related Questions