|
22 | 22 | return true; // All values are unique
|
23 | 23 | }
|
24 | 24 |
|
25 |
| - $inspect(localCopyOfData[0]); |
| 25 | + // $inspect( |
| 26 | + // localCopyOfData[0].areaName, |
| 27 | + // "data type is", |
| 28 | + // typeof localCopyOfData[0].areaName, |
| 29 | + // ); |
| 30 | + // $inspect( |
| 31 | + // localCopyOfData[0]["Household waste recycling rate"], |
| 32 | + // "data type is", |
| 33 | + // typeof localCopyOfData[0]["Household waste recycling rate"], |
| 34 | + // ); |
26 | 35 |
|
27 | 36 | let columns = [];
|
28 | 37 |
|
29 |
| - for (const key in localCopyOfData[0]) { |
| 38 | + for (const column in localCopyOfData[0]) { |
30 | 39 | // create a variable to store whether the key is unique or not
|
31 |
| - const keyIsUnique = hasUniqueValues(localCopyOfData, key); |
32 |
| - console.log(keyIsUnique); |
33 |
| -
|
| 40 | + const keyIsUnique = hasUniqueValues(localCopyOfData, column); |
| 41 | + // get data type of each column |
| 42 | + const columnDataType = typeof localCopyOfData[0][column]; |
34 | 43 | // for each one create an object and push it into the array
|
35 |
| - const columnObject = { key: key, isUnique: keyIsUnique }; |
| 44 | + const columnObject = { |
| 45 | + key: column, |
| 46 | + isUnique: keyIsUnique, |
| 47 | + dataType: columnDataType, |
| 48 | + }; |
36 | 49 | columns.push(columnObject);
|
37 | 50 | }
|
38 | 51 |
|
|
43 | 56 | localCopyOfData[0].length,
|
44 | 57 | );
|
45 | 58 |
|
| 59 | + $inspect("metrics is", metrics); |
| 60 | +
|
46 | 61 | let sortState = $state({ column: "sortedColumn", order: "ascending" });
|
47 | 62 |
|
48 | 63 | function updateSortState(columnToSort, sortOrder) {
|
|
109 | 124 | }
|
110 | 125 |
|
111 | 126 | const colorKey = Object.entries({ Good: 1, Ok: 0.5, Bad: 0 });
|
112 |
| -</script> |
113 | 127 |
|
114 |
| -{#snippet propNameAndValue(marginTW, paddingTW, text)} |
115 |
| - <span class="bg-slate-100 inline-block italic {marginTW} {paddingTW} rounded" |
116 |
| - >{text}</span |
117 |
| - > |
118 |
| -{/snippet} |
| 128 | + $inspect("the first column key is", columns[0].key); |
| 129 | +</script> |
119 | 130 |
|
120 | 131 | <div class="p-4">
|
121 | 132 | {#if colourScale === "On"}
|
|
134 | 145 | <caption class="govuk-table__caption">{caption}</caption>
|
135 | 146 | <thead class="govuk-table__head"
|
136 | 147 | ><tr class="govuk-table__row">
|
137 |
| - <th scope="col" class="govuk-table__header" aria-sort="ascending" |
| 148 | + <!-- <th scope="col" class="govuk-table__header" aria-sort="ascending" |
138 | 149 | >Area</th
|
139 |
| - > |
140 |
| - {#each metrics as metric} |
| 150 | + > --> |
| 151 | + {#each columns as column} |
141 | 152 | <th
|
142 | 153 | scope="col"
|
143 | 154 | class="govuk-table__header govuk-table__header--numeric"
|
144 |
| - title={metaData[metric].explainer} |
| 155 | + title={metaData[column.key].explainer} |
145 | 156 | aria-sort="none"
|
146 | 157 | >
|
147 | 158 | <div class="header">
|
148 | 159 | <Button
|
149 |
| - textContent={metaData[metric].shortLabel} |
| 160 | + textContent={metaData[column.key].shortLabel} |
150 | 161 | buttonType={"table header"}
|
151 | 162 | onClickFunction={() => {
|
152 | 163 | const newDirection =
|
153 |
| - sortState.column === metric && |
| 164 | + sortState.column === column.key && |
154 | 165 | sortState.order === "ascending"
|
155 | 166 | ? "descending"
|
156 | 167 | : "ascending";
|
157 | 168 |
|
158 |
| - updateSortState(metric, newDirection); |
| 169 | + updateSortState(column.key, newDirection); |
159 | 170 | sortFunction();
|
160 | 171 | }}
|
161 | 172 | ></Button>
|
|
167 | 178 | <tbody class="govuk-table__body">
|
168 | 179 | {#each localCopyOfData as row}
|
169 | 180 | <tr class="govuk-table__row">
|
170 |
| - <td class="govuk-table__cell">{row["areaName"]}</td> |
171 |
| - {#each metrics as metric} |
172 |
| - {#if colourScale === "On"} |
173 |
| - {#if metaData[metric].direction === "Higher is better"} |
174 |
| - <td |
175 |
| - class="govuk-table__cell govuk-table__cell--numeric" |
176 |
| - style="background-color: {normToColor( |
177 |
| - row[metric + '__normalised'], |
178 |
| - )}" |
179 |
| - data-sort-value="42">{row[metric]}</td |
180 |
| - > |
| 181 | + {#each columns as column} |
| 182 | + {#if column.dataType === "number"} |
| 183 | + {#if colourScale === "On"} |
| 184 | + {#if metaData[column.key].direction === "Higher is better"} |
| 185 | + <td |
| 186 | + class="govuk-table__cell govuk-table__cell--numeric" |
| 187 | + style="background-color: {normToColor( |
| 188 | + row[column.key + '__normalised'], |
| 189 | + )}" |
| 190 | + data-sort-value="42">{row[column.key]}</td |
| 191 | + > |
| 192 | + {:else} |
| 193 | + <td |
| 194 | + class="govuk-table__cell govuk-table__cell--numeric" |
| 195 | + style="background-color: {normToColorReverse( |
| 196 | + row[column.key + '__normalised'], |
| 197 | + )}" |
| 198 | + data-sort-value="42">{row[column.key]}</td |
| 199 | + > |
| 200 | + {/if} |
181 | 201 | {:else}
|
182 | 202 | <td
|
183 | 203 | class="govuk-table__cell govuk-table__cell--numeric"
|
184 |
| - style="background-color: {normToColorReverse( |
185 |
| - row[metric + '__normalised'], |
186 |
| - )}" |
187 |
| - data-sort-value="42">{row[metric]}</td |
| 204 | + data-sort-value="42">{row[column.key]}</td |
188 | 205 | >
|
189 | 206 | {/if}
|
190 | 207 | {:else}
|
191 |
| - <td |
192 |
| - class="govuk-table__cell govuk-table__cell--numeric" |
193 |
| - data-sort-value="42">{row[metric]}</td |
194 |
| - > |
| 208 | + <td class="govuk-table__cell">{row[column.key]}</td> |
195 | 209 | {/if}
|
196 | 210 | {/each}
|
197 | 211 | </tr>
|
|
0 commit comments