2

I have this table, with a checkbox filter:

<p-dataTable [value]="newCountries" #dt>
  <p-column field="title" header="Name"></p-column>
  <p-column field="alpha_3" header="Code" [filter]="true" filterMatchMode="equals" filterType="checkbox">
      <ng-template pTemplate="filter" let-col>
          <p-checkbox (onChange)="dt.filter('', col.field, col.filterMatchMode)"></p-checkbox>
       </ng-template>
  </p-column>
</p-dataTable>

Whenever I click the checkbox filter, I want to hide all the rows with values other than an empty string. The data is either a three letter string or an empty string. However, if I put '' as the value for the filter, it just shows me all the fields. Is there any way to achieve this?

Alexandru Pufan
  • 1,842
  • 3
  • 26
  • 44

1 Answers1

0

you need to implement a custom filter and put col.filterMatchMode = 'empty'

filter.trim() === '' is the "culprit" in the default equals filter mode as it would return true for any value

ngOnInit() {
    this.tableRef.filterConstraints['empty'] = (value, filter: any): boolean => {
        if (filter === undefined || filter === null) {
            return true;
        }

        if (value === undefined || value === null) {
            return false;
        }

        return filter === value; // this includes when filter equals ''
    }
}
Chepaki
  • 16
  • 3