|
| 1 | +# Rendering Data Rows |
| 2 | + |
| 3 | +The table's primary responsibility is to render rows of cells. The types of rows that may be rendered are header, |
| 4 | +footer, and data rows. This document focuses on how the table tries to efficienctly render the data rows. |
| 5 | + |
| 6 | +## Background |
| 7 | + |
| 8 | +Each table's template is defined as a set of row and column templates. The row template defines the template that should |
| 9 | +be rendered for a header, footer, or data row. The column templates include the cell templates that will be inserted |
| 10 | +into each rendered row. |
| 11 | + |
| 12 | +Each data object may be rendered with one or more row templates. When new data in provided to the table, the table |
| 13 | +determines which rows need to be rendered. In order to be efficient, the table attempts to understand how the new list |
| 14 | +of rendered rows differs from the previous list of rendered rows so that it can re-use the current list of rendered rows |
| 15 | +if possible. |
| 16 | + |
| 17 | +## Rendering |
| 18 | + |
| 19 | +Each time data is provided, the table needs to create the list of rows that will be rendered and keep track of which |
| 20 | +data object will be provided as context for each row. For each item in the list, this pair is combined into an object |
| 21 | +that uses the `RenderRow` interface. The interface also helps keep track of the data object's index in the provided |
| 22 | +data array input. |
| 23 | + |
| 24 | +```ts |
| 25 | +export interface RenderRow<T> { |
| 26 | + data: T; |
| 27 | + dataIndex: number; |
| 28 | + rowDef: CdkRowDef<T>; |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +When possible, `RenderRow` objects are re-used from the previous rendering. That is, if a particular data object and row |
| 33 | +template pairing was previously rendered, it should be used for the new list as well. This makes sure that the |
| 34 | +differ can use check-by-reference logic to find the changes between two lists. Note that if a `RenderRow` object is |
| 35 | +reused, it should be updated with the correct data index, in case it has moved since last used. |
| 36 | + |
| 37 | +Once the list of `RenderRow` objects has been created, it should be compared to the previous list of `RenderRow` |
| 38 | +objects to find the difference in terms of inserts/deletions/moves. This is trivially done using the `IterableDiffer` |
| 39 | +logic provided by Angular Core. |
| 40 | + |
| 41 | +Finally, the table uses the list of operations and manipulates the rows through add/remove/move operations. |
| 42 | + |
| 43 | +## Caching `RenderRow` objects |
| 44 | + |
| 45 | +Each `RenderRow` should be cached such that it is a constant-time lookup and retrieval based on the data object and |
| 46 | +row template pairing. |
| 47 | + |
| 48 | +In order to achieve this, the cache is built as a map of maps where the key of the outer map is the data object and |
| 49 | +the key of the inner map is the row template. The value of the inner map should be an array of the matching cached |
| 50 | +`RenderRow` objects that were previously rendered. |
| 51 | + |
0 commit comments