Skip to content

Commit

Permalink
Add ability to sort taxa alphabetically.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkohei13 committed Jan 16, 2025
1 parent 1d3421f commit ef2d18f
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion app/templates/form_challenge100.html
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@
}, 300); // Wait 300 ms before processing the input
});
});

</script>

{% endblock %}
Expand Down Expand Up @@ -310,6 +309,8 @@ <h1>Osallistuminen: {{ html['challenge']['title'] }}</h1>
<button type="submit" id="submit_button">Tallenna osallistuminen</button>
</p>

<button type="button" id="sort_button">Järjestä aakkosjärjestykseen</button> Voit palauttaa listan alkuperäiseen järjestykseen tallentamalla osallistumisen.

{% if 'taxa_count' not in html['data_fields'] %}
<!-- Empty form -->
{% else %}
Expand Down Expand Up @@ -349,4 +350,41 @@ <h3 id="taxon_count_heading">{{ html['data_fields']['taxa_count'] }} lajia</h3>

</form>

<script>

// Sorting taxa list
// Select the list element
const taxaList = document.getElementById('taxa');

// Get all list items except the headings
function sortTaxaList() {
// Remove all heading elements first
const headings = Array.from(taxaList.children).filter(item =>
item.classList.contains('list_heading_3') || item.classList.contains('list_heading_4')
);
headings.forEach(item => taxaList.removeChild(item));

// Get and sort the remaining list items
const listItems = Array.from(taxaList.children);
listItems.sort((a, b) => {
const nameA = a.querySelector('.taxon_name').textContent.trim().toLowerCase();
const nameB = b.querySelector('.taxon_name').textContent.trim().toLowerCase();
return nameA.localeCompare(nameB);
});

// Remove all items from the DOM
listItems.forEach(item => taxaList.removeChild(item));

// Append the sorted list items back to the list
taxaList.append(...listItems);
}

// Attach the sorting function to an existing HTML button
const existingSortButton = document.getElementById('sort_button');
if (existingSortButton) {
existingSortButton.addEventListener('click', sortTaxaList);
}

</script>

{% endblock %}

0 comments on commit ef2d18f

Please sign in to comment.