components icon indicating copy to clipboard operation
components copied to clipboard

MdSort is missing exportAs

Open sudheer-rj opened this issue 9 years ago • 13 comments

Hi,

How to access it (MdSort) from component if there are two md-tables in same component, or how to differentiate two mdsorts in same components.

like

<md-table #table1 [dataSource]="table1DataSource" mdSort>

And

<md-table #table2 [dataSource]="table2DataSource" mdSort>

@ViewChild(MdSort) table1Sort: MdSort; @ViewChild(MdSort) table2Sort: MdSort;

Issue is that, if i click to sort in the header column of one grid it sorts column in both grid or tables.

Thanks & Regards,

sudheer-rj avatar Sep 07 '17 14:09 sudheer-rj

As it does not have an exportAs associated, you can try a workaround:

@ContentChildren(MdSort) allMdSortDirectivesQuery: QueryList<MdSort>;

...

for(const mdSort of allMdSortDirectivesQuery) {
  // do something to try to identify the mdSort you want
}

Look at here to see all of the QueryList attributes.

As a last try, maybe you can assume the first mdSort in the page will be the first in the QueryList<MdSort> etc .

julianobrasil avatar Sep 07 '17 15:09 julianobrasil

This was an oversight, MdSort should have an exportAs. Should be added soon with #6747

andrewseguin avatar Sep 07 '17 18:09 andrewseguin

I need this :) :+1:

damsorian avatar Sep 09 '17 16:09 damsorian

@julianobrasil I have tried your suggestion, but couldn't make it work...

so to make it work, I have implemented these 2 grids in 2 seperate pages...

sudheer-rj avatar Sep 10 '17 16:09 sudheer-rj

@sudheer-rj, you mean you've built up custom compontents to encapsulate each of your datatables? IMO, even if there's an exportAs, encapsulating them in custom components will make your page easier to read. This is one of the best parts of Angular.

julianobrasil avatar Sep 10 '17 17:09 julianobrasil

can any one suggest other method to get list of all the MdSort in component html. i don't want to create individual component to encapsulate the table.

Thanks

Bsujeet avatar Oct 26 '17 12:10 Bsujeet

The problem is not to get the list of them... you can do it by

@ContentChildren(MatSort) allMatSortDirectivesQuery: QueryList<MatSort>;

The actual problem with this aproach arises if you have to execute an action in a particular MatSort: it looks like it is to not obvious way to identify it in the QueryList (according https://github.com/angular/material2/issues/6911#issuecomment-327879953 there's a plan to add an exportAs, probably in the next beta, I think). For now you can, for example, inspect the sortables property to try to identify which copy of MatSort your dealing with.

julianobrasil avatar Oct 26 '17 13:10 julianobrasil

@julianobrasil it returns undefined when try get allMatSortDirectivesQuery aqs suggested above and when i try using @viewChild it returns the mdsort of the first table.

Thanks.

Bsujeet avatar Oct 26 '17 14:10 Bsujeet

Sorry, it's @ViewChildren instead of @ContentChildren

julianobrasil avatar Oct 26 '17 15:10 julianobrasil

@julianobrasil

i know i tried @ViewChildren but it still gives only the values of starting mdsort and not the array. i have 4 tables in my component. i just wanted to ask if use component to render tables will it solve the problem.

Thanks

Bsujeet avatar Oct 27 '17 11:10 Bsujeet

@Bsujeet, that should work. Take a look at this stackblitz code: https://stackblitz.com/edit/test-with-matsort

julianobrasil avatar Oct 27 '17 12:10 julianobrasil

Thanks it was very helpfull

Bsujeet avatar Nov 03 '17 12:11 Bsujeet

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.