This is a follow up to this question: Sorting divs alphabetically in its own parent (due to many lists)
Hopefully the last problem I have with this. Now the lists sort themself nicely alphabetically and not depending on each other but I need to have one div stricly in the bottom in all lists. See code below, the div with green background (class: last), is it possible to always put this (with its hidden next div if existing) in the bottom in the lists? Maybe append it last and not alphabetically?
Since there is many lists, actually alot of them, I cant wrap some parts of the lists in divs or simular. So if its possible to append it when the rest as been apended alphabetically it would maybe work?
Many thanks for the help so far.
$('.parent').each(function(_, parent) {
$('.sort', parent).sort(function(a, b) {
return a.textContent.localeCompare(b.textContent);
}).each((index, el) => {
const hidden = $(el).next();
$(el).appendTo(parent);
if (hidden.hasClass('hidden')) {
hidden.appendTo(parent);
}
});
});
$(".btn_expand").on('click', function () {
$(this).next("div").toggle();
});
.btn_expand{background:red;}
.hidden{display: none;}
.last{background: green;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="parent">
<div class="btn_expand sort">CCC</div>
<div class="hidden">Hidden content shown by press CCC</div>
<div class="btn_no_expand sort">EEE</div>
<div class="btn_no_expand sort last">AAA</div>
<div class="btn_no_expand sort">DDD</div>
<div class="btn_expand sort">BBB</div>
<div class="hidden">Hidden content shown by press BBB</div>
</div>
<br><br>
<div class="parent">
<div class="btn_expand sort last">CCC</div>
<div class="hidden">Hidden content shown by press CCC</div>
<div class="btn_no_expand sort">EEE</div>
<div class="btn_no_expand sort">AAA</div>
<div class="btn_no_expand sort">DDD</div>
<div class="btn_expand sort">BBB</div>
<div class="hidden">Hidden content shown by press BBB</div>
</div>
2
Answers
You can achieve this in several ways. One is to adapt the sorting callback so that it considers items with a "last" class to be "greater" than others. This would also allow you to have multiple "last" items in one parent — these would all end up at the end, and among themselves they would be sorted by their texts.
One approach is as follows, with explanatory comments in the JavaScript:
JS Fiddle demo.
And an uncommented version:
It’s also worth noting that some of the requirements could be met by simply using appropriate HTML, and CSS; with the
<summary>
element handling the show/hide visibility of the related content, and theorder
property being used in CSS – if the.parent
elements havedisplay: grid
ordisplay: flex
– to move the.last
element(s) to the end of the.parent
:JS Fiddle demo.
display
.order
.<summary>
.Array.prototype.filter()
.Array.prototype.forEach()
.document.querySelector()
.document.querySelectorAll()
.Element.after()
.Element.append()
.Element.previousElementSibling
.Element.querySelector()
.Element.querySelectorAll()
.EventTarget.addEventListener()
.HTMLElement.hidden
.Map()
.Node.parentNode
.Node.textContent
.String.prototype.localeCompare()
.