Skip to content

Commit 4e04c93

Browse files
authored
Set current selection values on header/footer selection screens (#328)
When selecting header and footer rows, they are stored in the current session for when the user gets to the mapping screen. Unfortunately, if the user goes back to the header or footer selection screen, their previous selections are not shown even if they are still held in the backend state. This PR includes the current header/footer selection in the appropriate hidden form elements that denote the previous selection. Because when showing footers the row index in the range is for the entire sheet, not just the n rows being shown, this was failing to highlight the footer rows (when present). To resolve this, we added data-row-index to the rows in the table, allowing us to access the row directly. Perhaps it might be worth adding data-cell-position to TDs or data-column-index to columns to avoid these kind of errors in future?
1 parent 472b6f1 commit 4e04c93

9 files changed

Lines changed: 59 additions & 15 deletions

File tree

lib/importer/assets/js/selectable_table.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ window.addEventListener("load", function() {
4040
const brRowTarget = document.getElementById("importer:selection:BRRow");
4141
const brColTarget = document.getElementById("importer:selection:BRCol");
4242

43+
// If we have a value for the row target, then we want to select the cells in row tlr, column tlc
44+
// all the way to row brr, column brc by adding the selected class to each td in the range.
45+
if (tlRowTarget && tlRowTarget.value != "-1") {
46+
const [tlr, tlc, brr, brc] = [parseInt(tlRowTarget.value), parseInt(tlColTarget.value), parseInt(brRowTarget.value), parseInt(brColTarget.value)];
47+
const tables = document.querySelectorAll("table.selectable");
48+
for(const table of tables) {
49+
const tbody = table.querySelector("tbody");
50+
if (tbody) {
51+
52+
for (let rowIndex = tlr; rowIndex <= brr; rowIndex++) {
53+
const row = tbody.querySelector(`tr[data-row-index="${rowIndex + 1}"]`);
54+
if (row) {
55+
const cells = row.querySelectorAll("td");
56+
for (let colIndex = tlc; colIndex <= brc; colIndex++) {
57+
if (colIndex >= 0 && colIndex < cells.length) {
58+
cells[colIndex].classList.add(CellSelectedClassName);
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
};
66+
4367
const fromCoordsCache = new Map(); // table -> "row,col" -> CellRef
4468
const fromNodeCache = new Map(); // td node -> CellRef
4569

lib/importer/nunjucks/importer/macros/footer_selector.njk

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
{% set rows = importerGetTrailingRows(params.data, params.count) %}
55
{% set caption = importerGetTableCaption(params.data, "Last", params.count) %}
66

7+
{% set currentSelection = params.current %}
8+
{% set setSelectionValue = currentSelection.start.row > -1 %}
9+
710
<div>
8-
<input type="hidden" name="importer:selection:TLRow" id="importer:selection:TLRow"/>
9-
<input type="hidden" name="importer:selection:TLCol" id="importer:selection:TLCol"/>
10-
<input type="hidden" name="importer:selection:BRRow" id="importer:selection:BRRow"/>
11-
<input type="hidden" name="importer:selection:BRCol" id="importer:selection:BRCol"/>
11+
<input type="hidden" name="importer:selection:TLRow" id="importer:selection:TLRow" {% if setSelectionValue %} value="{{ currentSelection.start.row }}" {% endif %}/>
12+
<input type="hidden" name="importer:selection:TLCol" id="importer:selection:TLCol" {% if setSelectionValue %} value="{{ currentSelection.start.column }}" {% endif %}/>
13+
<input type="hidden" name="importer:selection:BRRow" id="importer:selection:BRRow" {% if setSelectionValue %} value="{{ currentSelection.end.row }}" {% endif %}/>
14+
<input type="hidden" name="importer:selection:BRCol" id="importer:selection:BRCol" {% if setSelectionValue %} value="{{ currentSelection.end.column }}" {% endif %}/>
1215
</div>
1316

1417
<div class="rd-range-selector">

lib/importer/nunjucks/importer/macros/header_selector.njk

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
{% set rows = importerGetRows(params.data, params.start, params.count) %}
66
{% set caption = importerGetTableCaption(params.data, "First", params.count) %}
77

8+
{% set currentSelection = params.current %}
9+
{% set setSelectionValue = currentSelection.start.row > -1 %}
10+
811
<div>
9-
<input type="hidden" name="importer:selection:TLRow" id="importer:selection:TLRow"/>
10-
<input type="hidden" name="importer:selection:TLCol" id="importer:selection:TLCol"/>
11-
<input type="hidden" name="importer:selection:BRRow" id="importer:selection:BRRow"/>
12-
<input type="hidden" name="importer:selection:BRCol" id="importer:selection:BRCol"/>
12+
<input type="hidden" name="importer:selection:TLRow" id="importer:selection:TLRow" {% if setSelectionValue %} value="{{ currentSelection.start.row }}" {% endif %}/>
13+
<input type="hidden" name="importer:selection:TLCol" id="importer:selection:TLCol" {% if setSelectionValue %} value="{{ currentSelection.start.column }}" {% endif %}/>
14+
<input type="hidden" name="importer:selection:BRRow" id="importer:selection:BRRow" {% if setSelectionValue %} value="{{ currentSelection.end.row }}" {% endif %}/>
15+
<input type="hidden" name="importer:selection:BRCol" id="importer:selection:BRCol" {% if setSelectionValue %} value="{{ currentSelection.end.column }}" {% endif %}/>
1316
</div>
1417

1518
<div class="rd-range-selector">

lib/importer/nunjucks/importer/macros/table_view.njk

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ Renders a table view using the provided parameters to determine what is being sh
2828
<tbody>
2929
{% for rowObj in params.rows %}
3030
<tr class="govuk-table__row {% if rowObj.error %}
31-
validation-error{%endif%}">
32-
33-
31+
validation-error{%endif%}" data-row-index="{{rowObj.index}}">
3432
{% if params.showRowNumbers %}
3533
<th scope="row" class="rowIndex">{{ rowObj.index }}</th>
3634
{% endif %}

lib/importer/src/functions.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ const importerHeaderRowDisplay = (data, mode) => {
178178
return session_lib.HeaderRowDisplay(session, mode)
179179
}
180180

181+
const importerCurrentHeaderSelection = (data) => {
182+
const session_data = data[IMPORTER_SESSION_KEY];
183+
const session = new session_lib.Session(session_data)
184+
185+
return session.headerRange
186+
}
187+
188+
const importerCurrentFooterSelection = (data) => {
189+
const session_data = data[IMPORTER_SESSION_KEY];
190+
const session = new session_lib.Session(session_data)
191+
192+
return session.footerRange
193+
}
194+
181195
//--------------------------------------------------------------------
182196
// Helper functions that can be used on the review page to show
183197
// information about the data that has been mapped.
@@ -254,6 +268,8 @@ module.exports = {
254268
importerGetTableCaption,
255269
importerMappedData,
256270
importerHeaderRowDisplay,
271+
importerCurrentHeaderSelection,
272+
importerCurrentFooterSelection,
257273
data_sum,
258274
data_avg
259275
}

lib/importer/templates/select_footer_row.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ <h1 class="govuk-fieldset__heading">
3131

3232
<form action="{{ importerSelectFooterPath('/mapping') }}" method="post">
3333
<div class="govuk-form-group">
34-
{{ dudkFooterSelector({data: data}) }}
34+
{{ dudkFooterSelector({data: data, current: importerCurrentFooterSelection(data)})}) }}
3535
</div>
3636

3737
<div class="govuk-button-group">

lib/importer/templates/select_header_row.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ <h1 class="govuk-fieldset__heading">
3131

3232
<form action="{{ importerSelectHeaderPath('/select_footer_row') }}" method="post">
3333
<div class="govuk-form-group">
34-
{{ dudkHeaderSelector({data: data, start: 0, count: 10}) }}
34+
{{ dudkHeaderSelector({data: data, start: 0, count: 10, current: importerCurrentHeaderSelection(data)}) }}
3535
</div>
3636

3737
<div class="govuk-button-group">

prototypes/basic/app/views/select_footer_row.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h1 class="govuk-heading-l">
5656

5757
<form action="{{ importerSelectFooterPath('/mapping') }}" method="post">
5858
<div class="govuk-form-group">
59-
{{ dudkFooterSelector({data: data}) }}
59+
{{ dudkFooterSelector({data: data, current: importerCurrentFooterSelection(data)}) }}
6060
</div>
6161

6262
<div class="govuk-button-group">

prototypes/basic/app/views/select_header_row.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ <h1 class="govuk-heading-l">
5353

5454
<form action="{{ importerSelectHeaderPath('/select_footer_row') }}" method="post">
5555
<div class="govuk-form-group">
56-
{{ dudkHeaderSelector({data: data, start: 0, count: 10}) }}
56+
{{ dudkHeaderSelector({data: data, start: 0, count: 10, current: importerCurrentHeaderSelection(data) }) }}
5757
</div>
5858

5959
<div class="govuk-button-group">

0 commit comments

Comments
 (0)