Skip to content

[Sort] Adjust sort interface to handle swapping out MatSort #7226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jscharett opened this issue Sep 21, 2017 · 38 comments
Open

[Sort] Adjust sort interface to handle swapping out MatSort #7226

jscharett opened this issue Sep 21, 2017 · 38 comments
Labels
area: material/sort feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@jscharett
Copy link

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

@jelbourn jelbourn added the feature This issue represents a new feature or feature request rather than a bug or bug fix label Sep 21, 2017
@jelbourn
Copy link
Member

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.

@jscharett
Copy link
Author

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

@timwright35
Copy link

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

@prabhat112
Copy link

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

@willshowell
Copy link
Contributor

@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.

@jelbourn
Copy link
Member

@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

@andrewseguin andrewseguin added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Oct 31, 2017
@andrewseguin andrewseguin changed the title mdTable multi column sort [Table] Adjust sort interface to handle swapping out MatSort Oct 31, 2017
@andrewseguin andrewseguin changed the title [Table] Adjust sort interface to handle swapping out MatSort [Sort] Adjust sort interface to handle swapping out MatSort Jan 23, 2018
@Kulikovpavel
Copy link

When this feature will be ready and available?
approximately

@dnemoga
Copy link

dnemoga commented Jul 14, 2018

Any updates?

@fxck
Copy link
Contributor

fxck commented Aug 19, 2018

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

@relair
Copy link

relair commented Sep 24, 2018

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.

@elboukhari
Copy link

Hi guys, any update on this thread ?

relair pushed a commit to relair/material2 that referenced this issue Oct 10, 2018
Extended the Sort interface for active to allow string array and SortDirection to allow dictionary object. Adding matMultiColumn property to MatSort that will allow multi column sorting. Updating MatTableDataSource to handle multi column sorting. Adding a counter for sort-header that shows sorting precedence.

Because of extended interface of MatSort, properties active and direction now need to be cast to string for existing usages. Fixes angular#7226.
@relair
Copy link

relair commented Oct 11, 2018

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.

@Bhuvn12
Copy link

Bhuvn12 commented Dec 12, 2018

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

@farantesrodrigues
Copy link

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.

@matiasah
Copy link

matiasah commented Sep 2, 2019

Is there any update on this?

@thesayyn
Copy link

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?

@nivle
Copy link

nivle commented Dec 3, 2019

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... 
}

@totibi
Copy link

totibi commented Dec 17, 2019

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?

@nivle
Copy link

nivle commented Dec 18, 2019

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"?

@Maxl94
Copy link

Maxl94 commented Dec 18, 2019

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!

@ajlif
Copy link

ajlif commented Mar 3, 2020

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

@himanshugarg574
Copy link

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

@willkara
Copy link

willkara commented May 5, 2020

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

@Chris2011 any idea?

@willkara
Copy link

willkara commented May 5, 2020

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
Copy link

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

@HemavaanKK
Copy link

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

@brandonsmith86
Copy link

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.

@ngothanhluan
Copy link

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

@patelatit53
Copy link

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

@Chris2011 any idea?

Waiting for the update too..

@l0wskilled
Copy link

Any update?

@Chris2011
Copy link

Chris2011 commented Dec 2, 2020

Hey @willkara I didn't see you mentioning me. Is there a reason why or was it a mistake?

@loxy
Copy link

loxy commented May 18, 2021

Any progress, half a year later?

@fxck
Copy link
Contributor

fxck commented May 18, 2021

yea, give it couple more years

#15171

image

@WellspringCS
Copy link

Being a beneficiary of this OSS functionality I don't build or support, I'm in no way wanting to come across as whining or unappreciative. I'm very thankful for Angular and these components.

That said, I do dearly wish there movement on this request. I have to imagine that thousands upon thousands of Angular developers would love to see this working.

@nrdev88
Copy link

nrdev88 commented May 21, 2021

Can only wish the team agrees. I believe that the architecture of the table component with the matSort directive doesn't allow us to implement this other than to enhance core code.

@imerljak
Copy link

I would be happy to tackle this feature if possible.

I recently needed to implement this feature and being able to change the source code would be so much easier than having to extend the MatSort and MatSortHeader and duplicating the templates and styles.

In my opinion the best way to resolve this issue is to refactor MatSortHeader and remove the direct checks on _sort.active and _sort.direction, encapsulating the direction querying and "isActive" verification on MatSort.

Having both this features on MatSort, would enable to change behavior (single or multi sort) without impacting much more.

Only "big" problem I see is the sortChanged event that emits a single Sort, this should be changed to emit Sort[], or exposing a multiSortChanged (a bit convoluted.).

If there's any interest in this, I'd be happy to prepare a draft.

@OBorce
Copy link

OBorce commented Feb 28, 2022

People designing components seem to forget that they don't need a perfect design, this is angular, it can break API every 6 months, that is why we have migration guides.
It has been more than 4 years, design something functional and later you can always improve on it.

Perfect is the enemy of good.

@mvarano-prism
Copy link

mvarano-prism commented Dec 20, 2023

I'm working on a project where this feature is sorely needed. We love Material UI, but have a table-heavy application and would like multi-sort out of the box when working on a table. Would be happy to contribute to a solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/sort feature This issue represents a new feature or feature request rather than a bug or bug fix P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.