Issue
I have a list of items that will be shown to user. I know to sort the items I can send the list to back-end, sort them and show them again but how to do it in front-end so no need to send the list to back-end.
Ive found this table but it shows the results in columns. As you see I want to show them in a specific way as following.
<div class="row">
<div class="col-md-4">
<c:forEach var="item" items="${products}">
<div style="text-align: left;">
<div>
Name: ${item.name}
</div>
<div>
Price: ${item.price}
</div>
</div>
--------------------------
</c:forEach>
</div>
</div>
The output is not a table it would be like following
Name: item1
Price: 12
---------------
Name: item2
Price: 23
----------------
Solution
You can sort the elements, take a look the example I did to sort by number:
http://jsfiddle.net/tnnqyxcw/1/
js:
$(document).ready(function(){
$('button').on('click', function(){
var s = $(this).data('sort'); console.log(s);
if(s === 0){
$(this).data('sort', 1);
$('.clist div').sort(function(a,b){
return a.dataset.sid < b.dataset.sid
}).appendTo('.clist')
}else{
$(this).data('sort', 0);
$('.clist div').sort(function(a,b){
return a.dataset.sid > b.dataset.sid
}).appendTo('.clist')
}
});
});
Html
<button data-sort="0">Sort:</button><br>
<div class="clist">
<div data-sid=1>Number 1</div>
<div data-sid=4>Number 4</div>
<div data-sid=3>Number 3</div>
<div data-sid=1>Number 1</div>
<div data-sid=4>Number 4</div>
<div data-sid=2>Number 2</div>
<div data-sid=1>Number 1</div>
</div>
So you can use data-
to sort for all you want and then assign the click to the button or whatever you want.
I hope it's help.
Answered By - gon250