jQueryUi
Tabs are good but they cannot be auto-sorted since there is no native function of the same, but i have created a custom solution for it.
What I have done:
- Sorted the DOM structure based on the
id
of each li
(tab) in ascending order.
- Updated the
li
(tabs) with sorted li
(tabs).
- called jQueryUi
.tabs()
to render the list as tabs.
HTML CODE:
<div id="tabs">
<ul>
<li id="1"><a href="#tabs-1">Nunc tincidunt</a>
</li>
<li id="3"><a href="#tab3">Aenean lacinia</a>
</li>
<li id="2"><a href="#tabs-2">Proin dolor</a>
</li>
</ul>
<div id="tabs-1">
<p>1.</p>
</div>
<div id="tabs-2">
<p>2.</p>
</div>
<div id="tab3">
<p>3.</p>
</div>
JS CODE:
$(function () {
//sort the tabs
sortTabs('#tabs');
var tabs = $("#tabs").tabs();
function sortTabs(target) {
var sortedTabs = $(target).find("li").sort(function (a, b) {
if (a.id < b.id) {
return -1;
} else {
return 1;
}
});
//update the sorted DOM
$(target).find('ul').html(sortedTabs);
}
});
Live Demo @ JSFiddle