* Calculates the width of each column by finding * the longest value in a cell of a particular column. * * Returns a list of column names and their widths.
()
| 86 | * Returns a list of column names and their widths. |
| 87 | */ |
| 88 | getColumns(): Column<T>[] { |
| 89 | const { columns, padding } = this.getConfig(); |
| 90 | |
| 91 | const widths: Column<T>[] = columns.map((key) => { |
| 92 | const header = String(key).length; |
| 93 | /* Get the width of each cell in the column */ |
| 94 | const data = this.props.data.map((data) => { |
| 95 | const value = data[key]; |
| 96 | |
| 97 | if (value == undefined || value == null) return 0; |
| 98 | return String(value).length; |
| 99 | }); |
| 100 | |
| 101 | const width = Math.max(...data, header) + padding * 2; |
| 102 | |
| 103 | /* Construct a cell */ |
| 104 | return { |
| 105 | column: key, |
| 106 | width: width, |
| 107 | key: String(key), |
| 108 | }; |
| 109 | }); |
| 110 | |
| 111 | return widths; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns a (data) row representing the headings. |