table-data-source.d.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import { DataSource } from '@angular/cdk/table';
  9. import { BehaviorSubject, Subscription } from 'rxjs';
  10. import { MatPaginator } from '@angular/material/paginator';
  11. import { MatSort } from '@angular/material/sort';
  12. /**
  13. * Data source that accepts a client-side data array and includes native support of filtering,
  14. * sorting (using MatSort), and pagination (using MatPaginator).
  15. *
  16. * Allows for sort customization by overriding sortingDataAccessor, which defines how data
  17. * properties are accessed. Also allows for filter customization by overriding filterTermAccessor,
  18. * which defines how row data is converted to a string for filter matching.
  19. */
  20. export declare class MatTableDataSource<T> extends DataSource<T> {
  21. /** Stream that emits when a new data array is set on the data source. */
  22. private readonly _data;
  23. /** Stream emitting render data to the table (depends on ordered data changes). */
  24. private readonly _renderData;
  25. /** Stream that emits when a new filter string is set on the data source. */
  26. private readonly _filter;
  27. /** Used to react to internal changes of the paginator that are made by the data source itself. */
  28. private readonly _internalPageChanges;
  29. /**
  30. * Subscription to the changes that should trigger an update to the table's rendered rows, such
  31. * as filtering, sorting, pagination, or base data changes.
  32. */
  33. _renderChangesSubscription: Subscription;
  34. /**
  35. * The filtered set of data that has been matched by the filter string, or all the data if there
  36. * is no filter. Useful for knowing the set of data the table represents.
  37. * For example, a 'selectAll()' function would likely want to select the set of filtered data
  38. * shown to the user rather than all the data.
  39. */
  40. filteredData: T[];
  41. /** Array of data that should be rendered by the table, where each object represents one row. */
  42. data: T[];
  43. /**
  44. * Filter term that should be used to filter out objects from the data array. To override how
  45. * data objects match to this filter string, provide a custom function for filterPredicate.
  46. */
  47. filter: string;
  48. /**
  49. * Instance of the MatSort directive used by the table to control its sorting. Sort changes
  50. * emitted by the MatSort will trigger an update to the table's rendered data.
  51. */
  52. sort: MatSort | null;
  53. private _sort;
  54. /**
  55. * Instance of the MatPaginator component used by the table to control what page of the data is
  56. * displayed. Page changes emitted by the MatPaginator will trigger an update to the
  57. * table's rendered data.
  58. *
  59. * Note that the data source uses the paginator's properties to calculate which page of data
  60. * should be displayed. If the paginator receives its properties as template inputs,
  61. * e.g. `[pageLength]=100` or `[pageIndex]=1`, then be sure that the paginator's view has been
  62. * initialized before assigning it to this data source.
  63. */
  64. paginator: MatPaginator | null;
  65. private _paginator;
  66. /**
  67. * Data accessor function that is used for accessing data properties for sorting through
  68. * the default sortData function.
  69. * This default function assumes that the sort header IDs (which defaults to the column name)
  70. * matches the data's properties (e.g. column Xyz represents data['Xyz']).
  71. * May be set to a custom function for different behavior.
  72. * @param data Data object that is being accessed.
  73. * @param sortHeaderId The name of the column that represents the data.
  74. */
  75. sortingDataAccessor: ((data: T, sortHeaderId: string) => string | number);
  76. /**
  77. * Gets a sorted copy of the data array based on the state of the MatSort. Called
  78. * after changes are made to the filtered data or when sort changes are emitted from MatSort.
  79. * By default, the function retrieves the active sort and its direction and compares data
  80. * by retrieving data using the sortingDataAccessor. May be overridden for a custom implementation
  81. * of data ordering.
  82. * @param data The array of data that should be sorted.
  83. * @param sort The connected MatSort that holds the current sort state.
  84. */
  85. sortData: ((data: T[], sort: MatSort) => T[]);
  86. /**
  87. * Checks if a data object matches the data source's filter string. By default, each data object
  88. * is converted to a string of its properties and returns true if the filter has
  89. * at least one occurrence in that string. By default, the filter string has its whitespace
  90. * trimmed and the match is case-insensitive. May be overridden for a custom implementation of
  91. * filter matching.
  92. * @param data Data object used to check against the filter.
  93. * @param filter Filter string that has been set on the data source.
  94. * @returns Whether the filter matches against the data
  95. */
  96. filterPredicate: ((data: T, filter: string) => boolean);
  97. constructor(initialData?: T[]);
  98. /**
  99. * Subscribe to changes that should trigger an update to the table's rendered rows. When the
  100. * changes occur, process the current state of the filter, sort, and pagination along with
  101. * the provided base data and send it to the table for rendering.
  102. */
  103. _updateChangeSubscription(): void;
  104. /**
  105. * Returns a filtered data array where each filter object contains the filter string within
  106. * the result of the filterTermAccessor function. If no filter is set, returns the data array
  107. * as provided.
  108. */
  109. _filterData(data: T[]): T[];
  110. /**
  111. * Returns a sorted copy of the data if MatSort has a sort applied, otherwise just returns the
  112. * data array as provided. Uses the default data accessor for data lookup, unless a
  113. * sortDataAccessor function is defined.
  114. */
  115. _orderData(data: T[]): T[];
  116. /**
  117. * Returns a paged slice of the provided data array according to the provided MatPaginator's page
  118. * index and length. If there is no paginator provided, returns the data array as provided.
  119. */
  120. _pageData(data: T[]): T[];
  121. /**
  122. * Updates the paginator to reflect the length of the filtered data, and makes sure that the page
  123. * index does not exceed the paginator's last page. Values are changed in a resolved promise to
  124. * guard against making property changes within a round of change detection.
  125. */
  126. _updatePaginator(filteredDataLength: number): void;
  127. /**
  128. * Used by the MatTable. Called when it connects to the data source.
  129. * @docs-private
  130. */
  131. connect(): BehaviorSubject<T[]>;
  132. /**
  133. * Used by the MatTable. Called when it is destroyed. No-op.
  134. * @docs-private
  135. */
  136. disconnect(): void;
  137. }