@angular/cdk/collections#DataSource TypeScript Examples

The following examples show how to use @angular/cdk/collections#DataSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: sort.spec.ts    From alauda-ui with MIT License 6 votes vote down vote up
class FakeDataSource extends DataSource<any> {
  connect(collectionViewer: CollectionViewer): Observable<any[]> {
    return collectionViewer.viewChange.pipe(map(() => []));
  }

  disconnect() {
    //
  }
}
Example #2
Source File: table.spec.ts    From alauda-ui with MIT License 6 votes vote down vote up
class FakeDataSource extends DataSource<TestData> {
  _dataChange = new BehaviorSubject<TestData[]>([]);
  set data(data: TestData[]) {
    this._dataChange.next(data);
  }

  get data() {
    return this._dataChange.getValue();
  }

  constructor() {
    super();
    for (let i = 0; i < 4; i++) {
      this.addData();
    }
  }

  connect(): Observable<TestData[]> {
    return this._dataChange;
  }

  disconnect() {
    //
  }

  addData() {
    const nextIndex = this.data.length + 1;

    const copiedData = this.data.slice();
    copiedData.push({
      a: `a_${nextIndex}`,
      b: `b_${nextIndex}`,
      c: `c_${nextIndex}`,
    });

    this.data = copiedData;
  }
}
Example #3
Source File: table-data-source.class.ts    From halstack-angular with Apache License 2.0 6 votes vote down vote up
export class TableDataSource extends DataSource<any> {
  /** Stream of data that is provided to the table. */

  public data = new BehaviorSubject<[]>([]);

  constructor(items) {
    super();
    this.data = items;
  }

  getData() {
    return this.data;
  }

  /** Connect function called by the table to retrieve one stream containing the data to render. */
  connect(): Observable<[]> {
    return this.data;
  }

  disconnect() {}
}
Example #4
Source File: data-table-datasource.ts    From worktez with MIT License 5 votes vote down vote up
/**
 * Data source for the DataTable view. This class should
 * encapsulate all logic for fetching and manipulating the displayed data
 * (including sorting, pagination, and filtering).
 */
export class DataTableDataSource extends DataSource<Tasks> {
  public data: Tasks[];
  paginator: MatPaginator | undefined;
  sort: MatSort | undefined;

  constructor() {
    super();
  }

  /**
   * Connect this data source to the table. The table will only update when
   * the returned stream emits new items.
   * @returns A stream of the items to be rendered.
   */
  connect(): Observable<Tasks[]> {
    if (this.paginator && this.sort) {
      // Combine everything that affects the rendered data into one update
      // stream for the data-table to consume.
      return merge(observableOf(this.data), this.paginator.page, this.sort.sortChange)
        .pipe(map(() => {
          if(this.data != undefined)
            return this.getPagedData(this.getSortedData([...this.data ]));
        }));
    } else {
      throw Error('Please set the paginator and sort on the data source before connecting.');
    }
  }

  /**
   *  Called when the table is being destroyed. Use this function, to clean up
   * any open connections or free any held resources that were set up during connect.
   */
  disconnect(): void {}

  /**
   * Paginate the data (client-side). If you're using server-side pagination,
   * this would be replaced by requesting the appropriate data from the server.
   */
  private getPagedData(data: Tasks[]): Tasks[] {
    if (this.paginator) {
      const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
      return data.splice(startIndex, this.paginator.pageSize);
    } else {
      return data;
    }
  }

  /**
   * Sort the data (client-side). If you're using server-side sorting,
   * this would be replaced by requesting the appropriate data from the server.
   */
  private getSortedData(data: Tasks[]): Tasks[] {
    if (!this.sort || !this.sort.active || this.sort.direction === '') {
      return data;
    }

    return data.sort((a, b) => {
      const isAsc = this.sort?.direction === 'asc';
      switch (this.sort?.active) {
        case 'Status': return compare(a.Status, b.Status, isAsc);
        case 'Priority': return compare(a.Priority, b.Priority, isAsc);
        case 'Difficulty': return compare(a.Difficulty, b.Difficulty, isAsc);
        case 'SprintNumber': return compare(a.SprintNumber, b.SprintNumber, isAsc);
        case 'Id': return compare(a.Id, b.Id, isAsc);
        case 'Title': return compare(a.Title, b.Title, isAsc);
        case 'WorkDone': return compare(a.WorkDone, b.WorkDone, isAsc);
        case 'Assignee': return compare(a.Assignee, b.Assignee, isAsc);
        default: return 0;
      }
    });
  }
}
Example #5
Source File: expenses-table.component.ts    From budget-angular with GNU General Public License v3.0 5 votes vote down vote up
@Input()
  dataSource: DataSource<Expense>;