2

PrimeNg datatable uses built in [filter]="true". This will create a input text box internally that is used to filter data. How can I place this textbox outside the datatable and get the filter done for a particular column?

Arun
  • 3,701
  • 5
  • 32
  • 43
  • are you looking for server side filtering or client side? – DirtyMind Sep 02 '17 at 21:49
  • Client side filtering – Arun Sep 02 '17 at 23:15
  • 1
    You should try this way also .. [https://stackoverflow.com/questions/50741591/prime-ng-create-custom-filter-for-column-filed-set-outside-of-data-table-in-angu/50754243#50754243](https://stackoverflow.com/questions/50741591/prime-ng-create-custom-filter-for-column-filed-set-outside-of-data-table-in-angu/50754243#50754243) – Nitin Wahale Jun 08 '18 at 06:06

4 Answers4

5

Below is the code with external dropdown to filter the primeng datatable.

html page:

<!-- Top of the page -->
<div>
<p-dropdown [options]="orgs" [(ngModel)]="selectedOrg"  (onChange)="updateOrgFilter(dt);getFilteredOutput($event,dt)" styleClass="ui-column-filter"></p-dropdown>
</div>

<!-- Pie Chart -->

<!-- Bar Chart -->

<!-- Datatable -->

<p-dataTable #dt [value]="itemslist"  [rows]="30" [paginator]="true" [rowsPerPageOptions]="[30,50,75]" sortMode="multiple" scrollable="true"   resizableColumns="true" scrollHeight="350px">
    <p-header>
        <div class="ui-helper-clearfix">
            List of Items
        </div>
    </p-header>

    <p-column [style]="{'width':'100px'}"field="wipJobNum" header ="Title" [sortable]="true" [filter]="true" ></p-column>
    <p-column [style]="{'width':'150px'}"field="partNum" header ="Part Number" [sortable]="true" [filter]="true" ></p-column>
    <p-column [style]="{'width':'90px'}" field="org" header ="Org" [sortable]="true" [filter]="true"  filterMatchMode="equals">
        <ng-template pTemplate="filter" let-col>
            <p-dropdown [options]="orgs" [(ngModel)]="selectedOrg" appendTo="body" [style]="{'width':'100%'}"  (onChange)="dt.filter($event.value,col.field,col.filterMatchMode);getFilteredOutput(event,dt)"  styleClass="ui-column-filter"></p-dropdown>
        </ng-template>
    </p-column>
</p-dataTable>

component.ts:

updateOrgFilter(dt) {
    dt.filter(this.selectedOrg, 'org', 'equals');
}

In this example if you change value of org drop down inside the datatable , then the external dropdown will change and my charts will be updated. if you change the external drop down value then primeng datatable filter will be updated and displays the filtered output + charts will be updated.

Milo
  • 3,365
  • 9
  • 30
  • 44
Jan69
  • 1,109
  • 5
  • 25
  • 48
2

Please have a look at https://www.primefaces.org/primeng/#/table/filter

If you replace dt.filterGlobal($event.target.value, 'contains') with dt.filter($event.target.value, 'your field name','contains'), the datatable will filter with the field.

If you have an external search field, call the same function from the event handler.

Harikrishnan
  • 3,664
  • 7
  • 48
  • 77
1

After searching entire primeng documentation I found that primeng currently do not support this feature. We will have to filer the data by ourselves and update prime ng datatable [value] model.

Arun
  • 3,701
  • 5
  • 32
  • 43
-4

Exactly like the example on the showcase.

DataTable Filtering - Global Filter

BillF
  • 804
  • 10
  • 20