how to use scroll event in angular material mat select?

前端 未结 2 947
猫巷女王i
猫巷女王i 2020-12-18 09:07

i have a large list and i want to load it as the user scroll down the select field but how can i get the scroll event in mat-select there is no event that fire the scroll ev

2条回答
  •  温柔的废话
    2020-12-18 09:45

    To this date, infinite scroll for mat-select component is still not available and kept as an open issue. Meanwhile, have a look at ng-mat-select-infinite-scroll, you can use it to lazy load your data.

    import { MatFormFieldModule, MatSelectModule } from '@angular/material';
    import {MatSelectInfiniteScrollModule} from 'ng-mat-select-infinite-scroll';
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        BrowserAnimationsModule,
        MatFormFieldModule,
        MatSelectModule,
        MatSelectInfiniteScrollModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule {
    }
    

    Component

      total = 100;
      data = Array.from({length: this.total}).map((_, i) => `Option ${i}`);
      limit = 10;
      offset = 0;
      options = new BehaviorSubject([]);
      options$: Observable;
    
      constructor() {
        this.options$ = this.options.asObservable().pipe(
          scan((acc, curr) => {
            return [...acc, ...curr];
          }, [])
        );
      }
    
      ngOnInit() {
        this.getNextBatch();
      }
    
      getNextBatch() {
        const result = this.data.slice(this.offset, this.offset + this.limit);
        this.options.next(result);
        this.offset += this.limit;
      }
    

    Template

      
        Select
        
          {{option}}
        
      
    

    Here's a working example

提交回复
热议问题