question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Table] Support sorting / filtering table with pipes

See original GitHub issue

Bug, feature request, or proposal:

Request

What is the expected behavior?

I use the MatTable feature.

My data source contains a bunch of ids, so most of my columns is bound to an id property and use a pipe to display the name.

For example: <ng-container matColumnDef="type"> <th mat-header-cell *matHeaderCellDef> Type </th> <td mat-cell *matCellDef="let device"> {{product.TypeId | productTypePipe}} </td> </ng-container>

And it works great!

However when I wanted to implement sort or filter I stumble some difficulties.

Apparently the table uses the model to sort / filter data.

However I wish to sort the table using the piped value and not the id value.

Likewise, I would like to filter by the piped value (which are strings) and not the ids (which the client is not aware of).

For the sorting issue I tried to use the sortingDataAccessor however I couldn’t use the services that translate the ids into names. (I guess because the context is different)

What is the current behavior?

the current behavior uses the model given to the DataSource with no regard to what actually displayed in the table.

What are the steps to reproduce?

https://angular-material2-issue-m3sdhq.stackblitz.io (Try to sort the symbol column)

Manual reproduce

  1. Create a pipe that translates a number to a string.
  2. create table that the cell uses this pipe.
  3. try to sort / filter by the value name.

What is the use-case or motivation for changing an existing behavior?

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular: Angular 6. OS: Windows 7 TypeScript: 2.7.2. Browsers: Chrome (But I believe every browser is affected)

Is there anything else we should know?

My pipes uses DI so I can just create instance of them (I wish I could)

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:15

github_iconTop GitHub Comments

3reactions
erika9starcommented, Nov 6, 2018

Modifying the data model to transform the data in the component comes with its own set of problems when using a Material Table.

For example, piping a Date using a DatePipe, shortDate format in the component code outputs a string in M/d/yy format. If the user then clicks the column header above the date field to sort, it will sort alphabetically rather than chronologically because it’s a string and not a date.

Letting the true date exist in the data model, but still transforming it HTML-side using a DatePipe allows the data to display in a human-friendly format, and the sort to work as one would expect.

Unfortunately, the filter being tied to that same data model does not create an expected filtering behavior, as the user is filtering data that they may not be able to see (a true date rather than a formatted string).

1reaction
pshieldscommented, Oct 23, 2019

@leongrin I’ll try one more time to illustrate what I mean. In your code, you would change the transform method in your pipe as follows:

transform(
  rowObject: any,  // this is a reference to the row object
  ...  // leave other params the same
): any {
  const transformedValue = this.profilePerformanceServ.getInteractions(
    startDate,
    endDate,
    profileType,
    userId
  );
  // Save the transformed value onto the row object for future access by the sort data accessor
  rowObject.cachedInteractions = transformedValue;
  // Also return it from the pipe
  return transformedValue;
}

Then in your template, you would pass in the whole row object to the pipe:

<td mat-cell *matCellDef="let row">{{ ((row |
      getInteractions:startDate:
      endDate: row.profileType:
      row.userId | async)?.interactionsArray.length) | number : '1.2-2' }}</td>

Then somewhere in your component, you would set the sorting data accessor:

dataSource.sortingDataAccessor = (rowObject, columnName) => {
  switch(columnName) {
    case 'interactions':
      return rowObject.cachedInteractions;
    default:
      return rowObject[columnName];
  }
};

EDIT: This does not work correctly if the table is paginated and there is more than one page of results - see https://github.com/angular/components/issues/11782#issuecomment-545647745.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sort & Filter Data Tables in Angular - Armin Zia
We'll create a pipe for filtering data, and an attribute directive for sorting tables by columns. There are many free and commercial libraries ......
Read more >
Sorting and filtering table data after using a pipe in Angular
I am asking this question because I want to find generic way to sort and filter data after it is transformed by pipe....
Read more >
Filtering and Sorting an Array of Objects Using Pipes in Angular
Search and Sort Table. We have used 2 pipes:filter and sort as you can see below. The sort pipe is chained to the...
Read more >
Add Filtering and Sorting using angular pipes - YouTube
Angular pipes are useful in case when you want to transform any data in angular template without changing the underlying data format.
Read more >
Data table with sorting, pagination, and filtering. - StackBlitz
Data table with sorting, pagination, and filtering. ... templateUrl: 'table-overview-example.html', ... Set the paginator and sort after the.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found