ng-table-virtual-scroll

repository·master·Indexed 41 days ago

https://github.com/diprokon/ng-table-virtual-scroll

An Angular directive and module that enables virtual scrolling for Angular Material tables (mat-table) and CDK tables (cdk-table). Version 1.6.1 provides specialized data sources like TableVirtualScrollDataSource and CdkTableVirtualScrollDataSource, along with the tvsItemSize directive to configure row, header, and footer heights for efficient rendering within a cdk-virtual-scroll-viewport.

Tokens
2.6K
Snippets
10
Records
15
Agent score
50%

What's inside ng-table-virtual-scroll

  1. Install ng-table-virtual-scroll via npm

    master

    Install the package using npm to enable virtual scrolling for Angular Material tables.

    Version Compatibility:

    • Angular >= 15: Use latest version.
    • Angular 13 - 14: Use 1.5.* version.
    • Angular <= 12: Use 1.3.* version.
    $ npm install -save ng-table-virtual-scroll
  2. Import TableVirtualScrollModule

    master

    To use the library, import TableVirtualScrollModule into your Angular module.

    Prerequisites: You must have already installed and configured @angular/cdk/scrolling (ScrollingModule) and @angular/material/table (MatTableModule). TableVirtualScrollModule is designed to make these two modules work together.

    import { TableVirtualScrollModule } from 'ng-table-virtual-scroll';
    
    @NgModule({
      imports: [
        // ...
        TableVirtualScrollModule
      ]
    })
    export class AppModule { }
  3. Configure the virtual scroll table with tvsItemSize directive

    master

    Apply the tvsItemSize directive to the <cdk-virtual-scroll-viewport> element. You must ensure the viewport container has a defined height (e.g., via CSS style="height: 400px;").

    Available Directive Properties:

    • tvsItemSize: The row height in px (default: 48).
    • headerHeight: The header row height in px (default: 56).
    • footerHeight: The footer row height in px (default: 48).
    • headerEnabled: Boolean indicating if the header row is present (default: true).
    • footerEnabled: Boolean indicating if the footer row is present (default: false).
    • bufferMultiplier: Multiplier for the rendered buffer. The number of rows rendered before and after the visible part will be bufferMultiplier * visibleRowsCount.
    <cdk-virtual-scroll-viewport tvsItemSize="48" headerHeight="56" style="height: 400px;">
        <table mat-table [dataSource="dataSource">
        ...
        </table
    </cdk-virtual-scroll-viewport>
  4. Use TVSDataSource interface for virtual scrolling

    master

    When working with virtual scrolling in ng-table-virtual-scroll, your data source should implement the TVSDataSource<T> interface. This interface provides two essential RxJS Subjects that facilitate the communication between your data and the virtual scroll engine:

    • dataToRender$: A Subject<T[]> that emits the subset of data currently intended to be rendered in the table rows.
    • dataOfRange$: A Subject<T[]> used to signal the specific range of data that the virtual scroller should provide to the table's internal rendering stream.
  5. Import TableVirtualScrollModule

    master

    To use the virtual scroll functionality in your Angular application, import the TableVirtualScrollModule into your Angular module (e.g., app.module.ts). This module exports the TableItemSizeDirective, which is required to configure item sizes for the virtual scroll mechanism.

    import { TableVirtualScrollModule } from 'ng-table-virtual-scroll';
    
    @NgModule({
      imports: [
        TableVirtualScrollModule,
        // ... other imports
      ],
      // ...
    })
    export class AppModule { }
    import { TableVirtualScrollModule } from 'ng-table-virtual-scroll';
    
    @NgModule({
      imports: [
        TableVirtualScrollModule,
      ],
    })
    export class AppModule { }
  6. Use TableVirtualScrollDataSource

    master

    The directive requires a specific data source to function. You must use TableVirtualScrollDataSource (which extends MatTableDataSource) for mat-table.

    If you are using cdk-table instead of mat-table, use CdkTableVirtualScrollDataSource.

    import { TableVirtualScrollDataSource } from 'ng-table-virtual-scroll';
    
    @Component({...})
    export class MyComponent {
      dataSource = new TableVirtualScrollDataSource();
    }
  7. Error: `[tvsItemSize]` requires specific DataSources

    master

    The TableItemSizeDirective enforces strict data source types to ensure the virtual scroll strategy can correctly calculate the rendered range. If you provide an incompatible data source, you will encounter the following errors:

    • For mat-table: [tvsItemSize] requires TableVirtualScrollDataSource be set as [dataSource] of [mat-table]
    • For cdk-table: [tvsItemSize] requires CdkTableVirtualScrollDataSource be set as [dataSource] of [cdk-table]
    • General: [tvsItemSize] requires TableVirtualScrollDataSource or CdkTableVirtualScrollDataSource be set as [dataSource] of the table
  8. Use FixedSizeTableVirtualScrollStrategy properties and streams

    master

    The FixedSizeTableVirtualScrollStrategy provides several observable streams and properties to track the state of the virtualized table:

    • dataLength: A getter/setter to manage the total number of items in the data set. Setting this value updates the total content size of the viewport.
    • scrolledIndexChange: An Observable<number> that emits the index of the currently displayed row when the user scrolls.
    • renderedRangeStream: A BehaviorSubject<ListRange> that provides the current range of rendered items {start, end}.
    • stickyChange: A Subject<number> that emits the adjusted rendered offset, useful for managing sticky elements.
  9. Scroll to a specific index with scrollToIndex()

    master

    You can programmatically scroll the table to a specific row index using the scrollToIndex method. This method calculates the offset based on the configured rowHeight and headerHeight.

    // Scrolls to the row at index 10
    strategy.scrollToIndex(10);
    
    // Scrolls to index 10 with smooth behavior
    strategy.scrollToIndex(10, 'smooth');
  10. Implement CdkTableVirtualScrollDataSource

    master

    Use CdkTableVirtualScrollDataSource<T> when you are using a standard Angular CDK DataSource and want to add virtual scrolling capabilities. It extends the base DataSource<T> and implements TVSDataSource<T>.

    Key features:

    • Data Management: You can set the source data using the data property (e.g., dataSource.data = myItems;).
    • Stream Initialization: The class manages internal subscriptions to ensure dataToRender$ and dataOfRange$ are ready for use.
    • Lifecycle: It implements connect() and disconnect() as required by the CDK DataSource contract.
  11. Configure FixedSizeTableVirtualScrollStrategy

    master

    To configure the virtual scroll strategy, use the setConfig method with a TSVStrategyConfigs object. This allows you to define the dimensions of the table components and the buffering behavior. Changing these values will trigger an update to the content size calculation.

    strategy.setConfig({
      rowHeight: 50,
      headerHeight: 100,
      footerHeight: 50,
      bufferMultiplier: 2
    });
  12. Configure virtual scroll item sizes with `tvsItemSize`

    master

    To enable virtual scrolling in an Angular Material or CDK table, apply the tvsItemSize directive to your cdk-virtual-scroll-viewport. This directive manages the row heights and sticky header/footer offsets required for efficient virtual scrolling.

    Input Properties

    PropertyTypeDefaultDescription
    tvsItemSizestring | number48The height of a single row.
    headerEnabledbooleantrueWhether to enable sticky header support.
    headerHeightstring | number56The height of the table header.
    footerEnabledbooleanfalseWhether to enable sticky footer support.
    footerHeightstring | number48The height of the table footer.
    bufferMultiplierstring | number0.7Multiplier for the rendering buffer.

    Requirements

    For the directive to function correctly, the table's [dataSource] must be an instance of one of the following:

    • TableVirtualScrollDataSource (when using mat-table)
    • CdkTableVirtualScrollDataSource (when using cdk-table)
    <cdk-virtual-scroll-viewport 
      tvsItemSize="50" 
      [headerEnabled="true" 
      [headerHeight="60">
      
      <mat-table [dataSource="tvsDataSource"> ... </mat-table>
    
    </cdk-virtual-scroll-viewport>