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

1

6

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

Dowsabel answered 3/5, 2019 at 6:23 Comment(4)
It works fine for me stackblitz.com/edit/primeng-tables-gavvhm .. check thisChilt
What is the version of primeng you use?Chud
@PatrykUszyński I am using PrimeNg 7Dowsabel
I can't reproduce that, it's working fine for me. Could you provide stackblitz with your code?Chud
C
3

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> 
Corporeity answered 11/5, 2019 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.