Cosmin Ionascu
Cosmin Ionascu

Reputation: 7518

Reset filter value on primeNG table

According to the documentation from https://www.primefaces.org/primeng/#/table the reset method should "Resets sort, filter and paginator state." The problem is that reset table method is not deleting the filters from UI. (although filters field from table.ts is {} after reset)

Please see the this where I reproduced it. The code can be seen here Filter the Summary table (see example) by Failed field (or any other field). Press reset. => The table values will be reset but the filter value will still be visible.

The example is with the basic table but it's also NOT working with dynamic cols.

<ng-template pTemplate="header" let-columns>
<tr>
  <th *ngFor="let col of columns" [pSortableColumn]="col.field">
    {{col.header}}
    <p-sortIcon [field]="col.field"></p-sortIcon>
  </th>
</tr>
<tr>
  <th *ngFor="let col of columns">
    <input pInputText type="text" (input)="dt.filter($event.target.value, col.field, 'contains')">
  </th>
</tr>

Do you have any ideea on how I can clear the filters from the inputs?

Upvotes: 9

Views: 35277

Answers (5)

Kiran Sankar
Kiran Sankar

Reputation: 21

In case you are using p-calendar as a filter input and not using ngmodel :

<p-calendar pInputText #pCalendar1 *ngSwitchCase="'createdDate'"
(onSelect)="dt.filter($event, 'createdDate', 'customDateFilter')" class="p-column-filter form-control pCalendar" [dateFormat]="pCalendarFormat"
    [selectOtherMonths]="true" yearRange="0000:3000" [yearNavigator]="true"
    [monthNavigator]="true" [readonlyInput]="true"
    [value]="dt.filters[col.field] ? datePipe.transform(dt.filters[col.field].value, dateFormat) : ''">
</p-calendar>
@ViewChild('pCalendar1') calendar1: Calendar

clearFilter() {
    this.table.reset();
    if (this.calendar1 && this.calendar1.inputFieldValue) {
      this.calendar1.value = '';
      this.calendar1.inputFieldValue = '';
      this.calendar1.updateInputfield();

    }
}

Upvotes: -1

My solution:

(onChange)="value = $event.value === 'Reset Value' ? '' : 
 $event.value;
datatable.filter(value, column.field, column.filterMatchMode); 
doFilter($event.value)"

Because dt.filter() with empty value will be meaning clear the filter.hope it works...

Upvotes: 1

MrAlexBailey
MrAlexBailey

Reputation: 5289

Expanding on your question about the p-multiSelects... you can fix this by binding the ngModel property to checking the value of the data table's filter. You can use this same approach for the input fields as well.

For an input field:

<input pInputText placeholder='Search' 
                  type='text'
                  [ngModel]="dt.filters['foobar']? dt.filters['foobar'].value : ''" 
                  (input)="dt.filter($event.target.value, 'foobar', 'contains')"
>

For a p-multiSelect dropdown:

<p-multiSelect appendTo="body" 
               [options]="[{label: 'Foo', value: 'Foo'}, {label: 'Bar', value: "Bar'}]" 
               [ngModel]="dt.filters['foobar']? dt.filters['foobar'].value: []" 
               (onChange)="dt.filter($event.value, 'foobar', 'in')">
</p-multiSelect>

Finally, calling Table.reset() will completely reset all sorting, filtering and pagination.

Upvotes: 1

Cosmin Ionascu
Cosmin Ionascu

Reputation: 7518

Fixed it here

For input fields just add

[value]="dt.filters[<field>] ? dt.filters[<field>].value : ''" 

where <field> is the field send in the (input) method.

 (input)="dt.filter($event.target.value,<field>, 'contains')"

For example:

    <th>
      <input pInputText type="text" (input)="dt.filter($event.target.value, 'date', 'contains')"
       [value]="dt.filters['date'] ? dt.filters['date'].value : ''">
    </th>

Upvotes: 12

Woworks
Woworks

Reputation: 1468

What if you just add ngModel to your inputs like:

<input pInputText type="text" [(ngModel)]="dt22" (input)="dt.filter($event.target.value, 'date', 'contains')">

in code you'll define:

  dt22:string = '';

and then onClick will be:

onClick() {
    this.dt22 = '';
    this.table.reset();
  }

I know that this will require additional code, but this will definitely work (a just tried on your stackblitz code)

Upvotes: 5

Related Questions