components icon indicating copy to clipboard operation
components copied to clipboard

[Sort] Adjust sort interface to handle swapping out MatSort

Open jscharett opened this issue 8 years ago • 38 comments
trafficstars

Bug, feature request, or proposal:

It would be nice to be able to sort on multiple columns.

What is the expected behavior?

The user can sort the grid with multiple columns at a time

What is the current behavior?

The user can sort the grid with a single column

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

The app I'm working on has a requirement for multi-column sort. I've used other, non-angular, grids in the past and they have supported this.

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

Angular 4.3.6 Material 2.0.0-beta.10 Typescript 2.5.2

jscharett avatar Sep 21 '17 14:09 jscharett

There's should actually be nothing stopping one from doing multi-column sorting; you would just need to swap out the current mdSort directive (which holds the sort state) with a variant that has the state for multiple columns.

jelbourn avatar Sep 21 '17 17:09 jelbourn

Ah, good to know. Still getting familiar with Angular. Thanks

jscharett avatar Sep 22 '17 13:09 jscharett

@jscharett I was just thinking this would be nice myself, I will have to look into how mdSort runs etc it seems.

timwright35 avatar Oct 04 '17 21:10 timwright35

anyone has an example or a plunker implementation of the above suggestion?

prabhat112 avatar Oct 11 '17 13:10 prabhat112

@jelbourn I gave it a shot by providing a MultiSort directive that extends MatSort and uses a collection of active MatSortables instead of a single one.

However, this method in MatSortHeader, which controls whether a sort header is visible, requires a reimplementation of the header.

https://github.com/angular/material2/blob/9b0771286aa867ca111987e5d8232137d6a45d33/src/lib/sort/sort-header.ts#L143-L147

AFAIK it is not currently possible to extend a component while inheriting the decorator, so a reimplementation of MatSortHeader has to also reimplement the template, styles, animations, etc.

If I'm not missing something, this is quite a bit more involved than creating a MatSort variant.

willshowell avatar Oct 11 '17 16:10 willshowell

@willshowell thanks for giving that a try

@andrewseguin we need to change the matSort/sort-header to each conform to an interface that would allow for different sorting semantics

jelbourn avatar Oct 11 '17 18:10 jelbourn

When this feature will be ready and available? approximately

Kulikovpavel avatar Apr 11 '18 08:04 Kulikovpavel

Any updates?

dnemoga avatar Jul 14 '18 20:07 dnemoga

Is this planned anytime soon? Looks like there is nothing table related in the plan anymore..

fxck avatar Aug 19 '18 13:08 fxck

I am thinking about changing this myself, since I need multi column sorting and this doesn't seem to be addressed any time soon. So the problem right now is interface which is essentially a value of sort.active and _sort.direction which are just strings. I am thinking of either them being potentially an array (and add a check for it) - otherwise use current logic for backwards compatibility or alternatively have some new property with collection of tuples with id and direction and then have it work next to old properties which may become obsolete by this point.

relair avatar Sep 24 '18 09:09 relair

Hi guys, any update on this thread ?

elboukhari avatar Sep 26 '18 09:09 elboukhari

I have created a pull request with changes that add option to enable multi column sorting. I made it so it is as much backwards compatible as I could make it without adding additional properties. We will see if it is fine as is or material team will want me to do some changes.

relair avatar Oct 11 '18 08:10 relair

I have created a pull request with changes that add option to enable multi column sorting. I made it so it is as much backwards compatible as I could make it without adding additional properties. We will see if it is fine as is or material team will want me to do some changes.

@relair please show the working of this feature on stackblitz.com please. i am new in angular

Bhuvn12 avatar Dec 12 '18 09:12 Bhuvn12

Hi guys, I'm waiting for this feature and expecting it to be included in the next release (after @relair PR and all the discussion), angular 8 / ivy (?). For the meantime I created this working project to ng-mat-multi-sort just to understand better this problem and have something workable for dev demos.

farantesrodrigues avatar Apr 16 '19 22:04 farantesrodrigues

Is there any update on this?

matiasah avatar Sep 02 '19 18:09 matiasah

Hi guys, I'm waiting for this feature and expecting it to be included in the next release (after @relair PR and all the discussion), angular 8 / ivy (?). For the meantime I created this working project to ng-mat-multi-sort just to understand better this problem and have something workable for dev demos.

How about sending a PR against this repo instead of maintaining another package/repository?

thesayyn avatar Sep 16 '19 11:09 thesayyn

You can also just do something like this:

<tr>
    <th matSort (matSortChange)="someMethod($event)" mat-sort-header="firstName">firstname</th>
    <th matSort (matSortChange)="someMethod($event)" mat-sort-header="lastName">lastname</th>
    <th matSort (matSortChange)="someMethod($event)" mat-sort-header="language">language</th>
</tr>

private sortingValues = { firstName: '', lastName: '', language: '' };

public someMethod(event: any): void { 
    this.sortingValues[event.active] = event.direction;
    console.log(this.sortingValues);
    // the rest you want to do... 
}

nivle avatar Dec 03 '19 11:12 nivle

You can also just do something like this:

<tr>
    <th matSort (matSortChange)="someMethod($event)" mat-sort-header="firstName">firstname</th>
    <th matSort (matSortChange)="someMethod($event)" mat-sort-header="lastName">lastname</th>
    <th matSort (matSortChange)="someMethod($event)" mat-sort-header="language">language</th>
</tr>

private sortingValues = { firstName: '', lastName: '', language: '' };

public someMethod(event: any): void { 
    this.sortingValues[event.active] = event.direction;
    console.log(this.sortingValues);
    // the rest you want to do... 
}

Ok, what about arrows? I'm gues they calculated by angular inners? Any ways to have them alive?

totibi avatar Dec 17 '19 12:12 totibi

You can also just do something like this:

<tr>
    <th matSort (matSortChange)="someMethod($event)" mat-sort-header="firstName">firstname</th>
    <th matSort (matSortChange)="someMethod($event)" mat-sort-header="lastName">lastname</th>
    <th matSort (matSortChange)="someMethod($event)" mat-sort-header="language">language</th>
</tr>

private sortingValues = { firstName: '', lastName: '', language: '' };

public someMethod(event: any): void { 
    this.sortingValues[event.active] = event.direction;
    console.log(this.sortingValues);
    // the rest you want to do... 
}

Ok, what about arrows? I'm gues they calculated by angular inners? Any ways to have them alive?

Not sure what you mean? notice I added the "matSort" and call the method on each 'th', instead of adding them to the table, this way each column has their own arrow animations... For normal shared use, you could do like in this example Or do you mean something else by "alive"?

nivle avatar Dec 18 '19 08:12 nivle

I had the same problem and since there is no progress here yet I decided to write my own package. It provides as little bit more than just mulitsorting, but maybe some of you adapt it to your needs.

Have fun.

Is is based on Francisco Arantes Rodrigues approach, great thanks to him!

Maxl94 avatar Dec 18 '19 09:12 Maxl94

I have created a pull request with changes that add option to enable multi column sorting. I made it so it is as much backwards compatible as I could make it without adding additional properties. We will see if it is fine as is or material team will want me to do some changes.

i found the same issue , it is risolved or not , i'am currently using version 8

ajlif avatar Mar 03 '20 13:03 ajlif

Hey guys, Great work with IVY. Now that it is released, can we get an ETA on this ? Thanks.

himanshugarg574 avatar Apr 16 '20 07:04 himanshugarg574

Hey everyone, it's been years AND Ivy is out now. Is there an ETA?

@chris2011 any idea?

willkara avatar May 05 '20 21:05 willkara

Looks like the second I placed this I came across the multi-column sorting in the docs.

https://material.angular.io/components/table/overview#sorting

willkara avatar May 05 '20 21:05 willkara

Where is the "multi-column sorting in the docs"? I cannot find anything about it.

Looks like the second I placed this I came across the multi-column sorting in the docs.

https://material.angular.io/components/table/overview#sorting

woxingren avatar May 06 '20 17:05 woxingren

Could you please share the "multi-column" sorting in the docs?

Looks like the second I placed this I came across the multi-column sorting in the docs.

https://material.angular.io/components/table/overview#sorting

HemavaanKK avatar Jul 31 '20 06:07 HemavaanKK

Hey everyone, it's been years AND Ivy is out now. Is there an ETA?

@Chris2011 any idea?

Am interested in get an update as well. This seems like a pretty commonly requested feature.

brandonsmith86 avatar Aug 05 '20 15:08 brandonsmith86

Hi found this package on npm https://github.com/Maxl94/ngx-multi-sort-table. hope this is useful.

ngothanhluan avatar Oct 22 '20 04:10 ngothanhluan

Hey everyone, it's been years AND Ivy is out now. Is there an ETA?

@Chris2011 any idea?

Waiting for the update too..

patelatit53 avatar Dec 01 '20 14:12 patelatit53

Any update?

l0wskilled avatar Dec 02 '20 14:12 l0wskilled