components icon indicating copy to clipboard operation
components copied to clipboard

MatTableDataSource default type shows as CDKMatTableDataSourceInput<T> instead of MatTableDataSource<T>

Open CieloVistaSoftware opened this issue 6 years ago β€’ 2 comments

🐞 bug report

Affected Package

MatTable

Is this a regression?

Yes (I think)

Description

image

The connect and disconnect functions are the only ones addressible, which doesn't seem correct.

Here's code to show the issue.

   test() {
      let CdkTable: CdkTable<Person>;
      // The datasource is type CDKMatTableDataSourceInput
      let CdKDataSource = CdkTable.dataSource;

      let matTable: MatTable<Person>;
      // shouldn't mat table datasource be of type MatTableDataSource?
      // And not CDKMatTableDataSourceInput?
      let matTableDataSource = matTable.dataSource;

      let personArray: Array<Person>;

      // Ok let's try to explicitly set it    
      let ExplicitDataSource: MatTableDataSource<Person>;
      let filter = "test Filter";
      let paginator: MatPaginator;

      ExplicitDataSource.data = personArray;
      // good now we can see filter, sort, and paginator
      ExplicitDataSource.filter = filter;
      ExplicitDataSource.paginator = paginator;
      //MatTable data source stays as a CDKMatTableDataSourceInput
      //It should instead be a MatTableDataSource right?
      matTable.dataSource = ExplicitDataSource;
      matTable.dataSource.???
   }

matTable.dataSource.??? will not allow addressibility to the paginator, filter or sort properties.

The problem is seen when we attach a paginator, run a filter, which reduces the number of pages seen, and then press the next page button in the paginator. Instead of showing the filtered, next page, it reverts to the entire data array (as if no filter was there) and shows page 2 of the entire array.

πŸ”¬ Minimal Reproduction

https://stackblitz.com/...

πŸ”₯ Exception or Error





🌍 Your Environment

Angular Version:



Angular CLI: 10.0.3
Node: 12.18.0      
OS: win32 x64

Angular: 10.0.4
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.901.11
@angular-devkit/build-angular     0.1000.3
@angular-devkit/build-optimizer   0.1000.3
@angular-devkit/build-webpack     0.1000.3
@angular-devkit/core              10.0.3
@angular-devkit/schematics        10.0.3
@angular/cdk                      10.1.0
@angular/cli                      10.0.3
@angular/material                 10.1.0
@ngtools/webpack                  10.0.3
@schematics/angular               10.0.3
@schematics/update                0.1000.3
rxjs                              6.6.0
typescript                        3.9.7
webpack                           4.43.0

Anything else relevant?

CieloVistaSoftware avatar Oct 25 '20 14:10 CieloVistaSoftware

Work Around:

The key to this is to allow Angular to create the datasource at start up.
Assume we have a MatTableComponent like this:

<table
        [id]="tableId"
        matSort
        mat-table
        [(dataSource)]="dataSource"
      
     >

Then in Typescript, we declare the datasource like this and ensure that there is a default value.

   initialData = ["Loading"];
   dataSource: MatTableDataSource<any> = new MatTableDataSource(
      this.initialData
   );

Allowing others to set the context of this component, we may define a function like this:

function funcSetMatTableDataSourceProperties<T>(
   context,
   mt: MaterialTableComponent<T>
) {
   mt.dataSource.data = context;
   mt.dataSource.paginator = mt.matPaginator;
   mt.dataSource.filter = mt.currentSearchFilter;
   mt.dataSource.sort = mt.matSort;

   if (mt.dataSource.paginator) {
      pipePaginator(mt.matPaginator, mt.dataSource);
   }
   return mt.dataSource;
}

This works because the initial data forces the mat-table to create a datasource prior to setting it's context.  

Our prior code wasn't working for a number of reasons, but just changing the dataSource properties of ; data, paginator, filter and sort are enough.

CieloVistaSoftware avatar Oct 27 '20 20:10 CieloVistaSoftware

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.