Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search inside the array in primeNg p-table with multiselect?

I am having the data points like an array. So I am trying to search the value inside the array but it is not working in primeng

In the component file I am having the below part of code,

tableHeader = [
    { field: 'name', header: 'Name' },
    { field: 'skills', header: 'Skills' },
];

modelData = [
    { "label": "HTML", "value": "HTML" },
    { "label": "Css", "value": "Css" },
    { "label": "Angular", "value": "Angular" },
    { "label": "Python", "value": "Python" },
    { "label": "Perl", "value": "Perl" },
    { "label": "JS", "value": "JS" },
    { "label": "Java", "value": "Java" }
];

data = [
    {
        name:"User1",
        skills:["JS","Java","Angular"]
    },{
        name:"TestUser",
        skills:["HTML","Css"]
    },{
        name:"Root",
        skills:["HTML","Css","Angular","Python","Perl"]
    }
];

And the html is

<p-table #dt [value]="data">
    <ng-template pTemplate="header">
    <tr>
            <th>Name</th>
            <th>Skills</th>
    </tr>
    <tr>
        <th>
            <input pInputText type="text" (input)="dt.filter($event.target.value, 'name', 'contains')">
        </th>
        <th>
             <p-multiSelect [options]="modelData" (onChange)="dt.filter($event.value, 'skills', 'in')"></p-multiSelect>
        </th>
    </tr>
    </ng-template>
    <ng-template pTemplate="body" let-rowData>
        <tr>
            <td>{{rowData['name']}}</td>
            <td>
                <span *ngFor="let skill of rowData.skills;">
                {{skill}}
                </span>
            </td>
        </tr>
    </ng-template>
<p-table> 

I am able to search the Name field but I am not able to search the Skills field. because it contains the array value.

stackblitz

like image 964
mkHun Avatar asked May 03 '19 06:05

mkHun


People also ask

How do you create an index in PrimeNG table?

You can use let-rowIndex="rowIndex" property in your table. This will give you index of the row. Hope this help!

How do I add a new row to PrimeNG p table?

In the component where the PrimeNG p-table is used, a button is added at the bottom of the p-table. The button has a pAddRow directive, 'table' and 'newRow' @Input are assigned values. When the 'Add' button is clicked, a new row is added at the bottom of the table as shown below.

What is PrimeNG in angular?

PrimeNG is a collection of rich UI components for Angular. All widgets are open source and free to use under MIT License. PrimeNG is developed by PrimeTek Informatics, a vendor with years of expertise in developing open source UI solutions. For project news and updates, please follow us on twitter and visit our blog.


1 Answers

Work around to get proper search result i have tried and it worked for me.

Try add comma "," after the {{skill}} string Let me know in case of any confusion.

Like this:-

<p-table #dt [value]="data">
    <ng-template pTemplate="header">
    <tr>
            <th>Name</th>
            <th>Skills</th>
    </tr>
    <tr>
        <th>
            <input pInputText type="text" (input)="dt.filter($event.target.value, 'name', 'contains')">
        </th>
        <th>
            <input pInputText type="text" (input)="dt.filter($event.target.value, 'skills', 'contains')">
        </th>
    </tr>
    </ng-template>
    <ng-template pTemplate="body" let-rowData>
        <tr>
            <td>{{rowData['name']}}</td>
            <td>
                <span *ngFor="let skill of rowData.skills;">{{skill}},</span>
            </td>
        </tr>
    </ng-template>
<p-table> 
like image 84
paras shah Avatar answered Oct 27 '22 00:10

paras shah