MdSort is missing exportAs
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,
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 .
This was an oversight, MdSort should have an exportAs. Should be added soon with #6747
I need this :) :+1:
@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, 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.
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
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 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.
Sorry, it's @ViewChildren instead of @ContentChildren
@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, that should work. Take a look at this stackblitz code: https://stackblitz.com/edit/test-with-matsort
Thanks it was very helpfull
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.