I currently have a series of divs that are generated by our MVC app. I am attempting to sort by a variety of 'data-' attributes. I was able to group the items by an attribute called 'data-categoryname'.
Each item also has a 'data-categorysortorder' in which I want to sort the group that gets created by the 'data-categoryname' by that sort order value.
I have a JS Fiddle started, and have the groupings grouped, I now need help sorting the groups by category name by the 'category sort order'. Any help is appreciated.
https://jsfiddle.net/c0fawcnf/9/
//CURRENTLY HAVE GROUPING BY CATEGORY ITEM NAME AND WRAPPING EACH GROUP BY CATEGORY NAME, BUT NOW NEED TO SORT EACH CATEGORY WRAPPER BY THE ITEMS 'data-categorydisplayorder'
var $item = $('.item-box'),
items = {};
$item.each(function () {
items[$(this).data('categoryitemname')] = '';
});
for (category in items) {
var categoryLowerCase = category.toLowerCase();
$item.filter('[data-categoryitemname=' + category + ']').wrapAll('<div id="' + categoryLowerCase + '" class="categoryWrapper" data-wrappercategorydisplayorder="NEED_CATEGORY_DISPLAY_VALUE_HERE_AND_SORT_BY_THIS_VALUE"></div>');
//return list;
$('div#' + categoryLowerCase + '').prepend('<div class="page-title"><h1>' + category.replace("-", " ") + '</h1></div>');
}
// THESE ITEMS ARE OUTPUTTED BY AN MVC APPLICATION TO THE UI - I NEED TO SORT ON THE CLIENT SIDE BASED ON PARAMETERS
<div class="item-box" data-categoryitemname="Fall" data-itemsortorder="2" data-categorydisplayorder="2">Fall Item</div>
<div class="item-box" data-categoryitemname="Fall" data-itemsortorder="0" data-categorydisplayorder="2">Fall Item</div>
<div class="item-box" data-categoryitemname="Fall" data-itemsortorder="1" data-categorydisplayorder="2">Fall Item</div>
<div class="item-box" data-categoryitemname="Summer" data-itemsortorder="2" data-categorydisplayorder="1">Summer Item</div>
<div class="item-box" data-categoryitemname="Summer" data-itemsortorder="1" data-categorydisplayorder="1">Summer Item</div>
<div class="item-box" data-categoryitemname="Summer" data-itemsortorder="0" data-categorydisplayorder="1">Summer Item</div>
<div class="item-box" data-categoryitemname="Winter" data-itemsortorder="2" data-categorydisplayorder="3">Winter Item</div>
<div class="item-box" data-categoryitemname="Winter" data-itemsortorder="1" data-categorydisplayorder="3">Winter Item</div>
<div class="item-box" data-categoryitemname="Winter" data-itemsortorder="0" data-categorydisplayorder="3">Winter Item</div>
<div class="item-box" data-categoryitemname="Spring" data-itemsortorder="1" data-categorydisplayorder="0">Spring Item</div>
<div class="item-box" data-categoryitemname="Spring" data-itemsortorder="2" data-categorydisplayorder="0">Spring Item</div>
<div class="item-box" data-categoryitemname="Spring" data-itemsortorder="0" data-categorydisplayorder="0">Spring Item</div>
I did a lot of searching on how to sort items, but found many that sorted elements in known element with an ID that exists on the page already. Since I am dynamically adding the groups to the DOM then sorting after the creation of the groups, I have not really found anything on how to do that.
UPDATE: I got the sort order value to attach to the group element. but the sort function I have is still not working. I updated my JS Fiddle
https://jsfiddle.net/c0fawcnf/12/
// CURRENTLY HAVE GROUPING BY CATEGORY ITEM NAME AND WRAPPING EACH GROUP BY CATEGORY NAME, BUT NOW NEED TO SORT EACH CATEGORY WRAPPER BY THE ITEMS 'data-categorydisplayorder'
var $item = $('.item-box'),
items = {};
$item.each(function () {
items[$(this).data('categoryitemname')] = '';
});
for (category in items) {
var categoryLowerCase = category.toLowerCase();
$item.filter('[data-categoryitemname=' + category + ']').wrapAll('<div id="' + categoryLowerCase + '" class="categoryWrapper" data-wrappercategorydisplayorder="NEED_CATEGORY_DISPLAY_VALUE_HERE_AND_SORT_BY_THIS_VALUE"></div>');
//return list;
$('div#' + categoryLowerCase + '').prepend('<div class="page-title"><h1>' + category.replace("-", " ") + '</h1></div>');
// GET ITEM BOX DISPLAY ORDER PROPERTY AND ATTACH IT TO THE WRAPPER CATEGORY DISPLAY PROPERTY
var elem = $('div#' + categoryLowerCase + ' .item-box');
elem.parent().attr("data-wrappercategorydisplayorder", elem.data("categorydisplayorder"));
}
// ATTEMPT TO SORT - BUT NOT WORKING
$('.productCategoryWrapper').sort(sort_items).appendTo('#item-grid');
//IS THIS EVEN CORRECT?
function sort_items(a, b) {
console.log('we shall sort');
return ($(b).data('wrappercategorydisplayorder')) < ($(a).data('wrappercategorydisplayorder')) ? 1 : -1;
}
categorydisplayorder(so the output will be Spring-Summer-Fall-Winter) and also sort the items within each group byitemsortorder(the items within a group all have the same text so that bit is confusing)data-sortorderattribute so you can use it in a similar way. I'm about to take a break, but if you have not worked it out, I'll add an answer in an hour or so