ng2-smart-table icon indicating copy to clipboard operation
ng2-smart-table copied to clipboard

Adding filter change event

Open chaouiy opened this issue 9 years ago • 8 comments

It would be great if you add filter change event. this may be usefull to track number of lines using datasource count in real time when user use filters

chaouiy avatar Mar 10 '17 09:03 chaouiy

Are there any plans to add this feature @lexzhukov? I would like to use the built-in column filtering, but I have another component that needs to know when filtering has changed & get the new filtered data. If not, I supposed I'll need to take over handling of the filtering myself via setFilter.

bylertall avatar Jul 31 '17 21:07 bylertall

Nevermind, I think that LocalDataSource.onChanged() suits my needs. Though it is not documented.

bylertall avatar Aug 01 '17 19:08 bylertall

@bylertall how you did it? Can you help me?

lopenchi avatar Jan 16 '18 18:01 lopenchi

@lopenchi We dropped this lib from our app and went with Angular Material instead.

bylertall avatar Jan 16 '18 19:01 bylertall

Ok, i could did it with your explanation, i did it in this way:

In the method ngOnInit, I added this code:

this.tableData.onChanged().subscribe((change) => {

      if (change.action === 'filter' && !_.isEmpty(change.elements)) {

        let filters = _.compact(_.pluck(change.filter.filters, 'search'));
        let filterRows = change.elements;
        let totalRows = this.tables[this.selectedTableId].rows;

        if (_.isEmpty(filters)) {
          // There is no filters, it means filters were deleted, so dont do any 
          return;
        }

        // Do whatever you want with the filter event

      }
    });
  • this.tableData is the LocalDataSource.

lopenchi avatar Jan 19 '18 15:01 lopenchi

How can I update a second filter based on the selection of the first? Every column has a list filter. For instance:

ID | Country | City | Company

1 | Italy | Milan | Alfa 2 | Italy | Rome | Beta 3 | France | Paris | Gamma

After I filter the Country ("Italy" for instance) row 3 disappears but the City filter is still ['Milan', 'Rome', 'Paris']

with this.source.onChanged().subscribe((change) => { I am able to get the Country filter value but to update the second filter list I need to update the whole object

const updatedCitiesList = ['Milan', 'Rome']; const newSettings = this.settings; newSettings.columns.title.filter.config.list = updatedCitiesList; this.settings = Object.assign({}, newSettings );

But this puts me in an infinite loop on the .onChanged() event.

voidbrain avatar Mar 19 '20 14:03 voidbrain

@voidbrain How did you resolve the problem? I have same issue.

NguyenTienDat avatar May 18 '22 08:05 NguyenTienDat

@voidbrain In my case, I think this is good solution.

this.sub = this.source.onChanged().pipe(
      filter(event => event.action === 'filter'),
    ).subscribe(event => {
      console.log(event);
      if (this.oldFilter !== JSON.stringify(event.filter)) {
        this.oldFilter = JSON.stringify(event.filter);
        this.source.getFilteredAndSorted().then(res => {
          const newSettings = this.settings;
          newSettings.columns.role.filter.config.list = listFilters.roleList;
          newSettings.columns.gbm_name.filter.config.list = listFilters.gbmList;
          newSettings.columns.department_name.filter.config.list = listFilters.departmentList;
          this.settings = Object.assign({}, newSettings);
        });
      }
    });

NguyenTienDat avatar May 18 '22 11:05 NguyenTienDat