1

I need to filter the table on view load.

For example, I have a table with 5 columns (Woid, Customer, AdapterID, Assignee, Status). On load, I want to filter woid column using 'contains'. I accomplished filtering onLoad but when I want to filter again that column later I got an error:

[i]'Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.'[/i]

Program code below...

@ViewChild('dt', { static: true }) dt: any;
ngOnInit() {
    this.dt.filter('22', 'woid', 'contains');
}

enter image description here

Also, after filtering via .ts code (woid filter) and directly in view with <p-columnFilter> (customer filter) from PrimeNG Angular collection, I get this JSON. woid property is not an array, unlike custom which is. onFiltering method and JSON below.

onFiltering(event: any) {
    console.log('Filtered value: '+ JSON.stringify(event.filters));
}


{
    "woid": {
        "value": "22",
        "matchMode": "contains"
    },
    "customer": [{
        "value": "2",
        "matchMode": "contains",
        "operator": "and"
    }]
}
Domagoj Hamzic
  • 300
  • 3
  • 17
  • that means ngFor getting objects, rather ngFor requres you to pass array of objects – Developer Oct 09 '21 at 11:03
  • @GaurangDhorda I see, but why is this happening after this line of code: this.dt.filter('22', 'woid', 'contains'); – Domagoj Hamzic Oct 09 '21 at 11:06
  • check the return type of data, of what type you are getting after filter `this.dt.filter` – Developer Oct 09 '21 at 11:07
  • @GaurangDhorda I think that the `woid` is a problem because `woid` doesn't contain an array of filters unlike `customer`. But I don't know why the `filter` method doesn't add a new array... – Domagoj Hamzic Oct 09 '21 at 11:30
  • https://stackblitz.com/edit/primeng-tabledynamic-demo-jat3vn?file=src%2Fapp%2Fapp.component.ts here is demo, From my side its working fine. – Developer Oct 09 '21 at 14:10
  • @GaurangDhorda I see, but the problem starts when you want to filter again directly using . I've updated the code on stackblitz. Try to filter again and you'll get an error (Inspect->Console). https://stackblitz.com/edit/primeng-tabledynamic-demo-cnuc7e?file=src%2Fapp%2Fapp.component.ts – Domagoj Hamzic Oct 09 '21 at 15:46
  • @GaurangDhorda I've found a solution. You can see it in the answer to the topic! – Domagoj Hamzic Oct 10 '21 at 10:41

1 Answers1

4

I replaced this line of code (in ngAfterContentInit):

this.dt.filter('22', 'woid', 'contains'); 

with this:

this.dt.filters['woid'] = [{value: "22", matchMode: "contains", operator: "and"}];

Now it works fine :)

PS. It doesn't work if you're using stateKey & stateStorage

Domagoj Hamzic
  • 300
  • 3
  • 17