问题
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?
回答1:
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>
回答2:
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)
回答3:
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.
回答4:
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...
来源:https://stackoverflow.com/questions/51395624/reset-filter-value-on-primeng-table