Skip to content

Instantly share code, notes, and snippets.

Created November 8, 2015 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/468ede1a367197b1ab41 to your computer and use it in GitHub Desktop.
Save anonymous/468ede1a367197b1ab41 to your computer and use it in GitHub Desktop.
New sorting method with Jade & Flexbox
//The slice, sort, and for loop are not necessary. However, they are there for your conveinence when adding new price keys. If you want to drop those things, then you would have to add and change a new "order" key & value each time you add a new price key & value. For example, in this demo, $245 is the highest price. You would want a priceAry.order value of 0 for it (or the corresponding CSS order property value). You can see how manual changes can become unmaintainable. I wanted to handle this side of things in the template and not on the client side JS.
- var priceAry = [{"price":"25"},{"price":"245"},{"price":"15"},{"price":"55"},{"price":"50"},{"price":"35"},{"price":"75"},{"price":"150"},{"price":"175"},{"price":"55"},{"price":"125"},{"price":"100"}]
- var newAry = priceAry.slice(0);
- priceAry.sort(function(a, b){
- return b.price-a.price
- })
- for (var i = 0; i < priceAry.length; i++) {
- priceAry[i].order = i
- }
.button-wrapper
button#default Arrange by relevance (default)
button#high-to-low Arrange highest to lowest
button#low-to-high Arrange lowest to highest
ul#grid
each val, index in newAry
li(class="sortable", data-order=val.order, data-default=index)
i.fa.fa-picture-o.fake-image
span.price= "$" + val.price

New sorting method with Jade & Flexbox

Generally, sorting is pretty easy. So is there a way to make it even easier? We can take advantage of CSS order to sort by 3 options. Here's the kicker: we don't need to use an array or object sort, and we don't need to use .append(). Most of the work is done in the Jade template. To watch it adapt to new data, add a new "price" key in the priceAry variable.

A Pen by Christopher Schuck on CodePen.

License.

//Tested in Chrome, Firefox, & IE11
//No jQuery used. I wanted to keep it as fast and lightweight as possible
document.getElementById("high-to-low").addEventListener("click",function(){
sortItems("order");
});
/*You are probably thinking to yourself, hey why not use row-reverse on the grid id? I tried that originally, but is was only reversing individual rows. If you wanted multiple rows then you were out of luck. This works even better.*/
document.getElementById("low-to-high").addEventListener("click",function(){
sortItems("order","-");
});
document.getElementById("default").addEventListener("click",function(){
sortItems("default");
});
function sortItems(dataType,neg) {
neg = neg || "";
var delay = 0;
var sortable = document.querySelectorAll(".sortable");
for (var i = 0; i < sortable.length; i++) {
var dataSelect = document.querySelector("[data-default='" + i + "']")
dataSelect.style.opacity = 0;
dataSelect.style.transitionDelay = (delay += 0.02) + "s";
}
sortable[sortable.length-1].addEventListener("transitionend", function(){
for (var i = 0; i < sortable.length; i++) {
//Get the element by data-order and change the order style. That's all you have to do! You could probably substitute data-order for an id, but the data attribute seemed more logical
var dataSelect = document.querySelector("[data-" + dataType + "='" + i + "']")
dataSelect.style.order = neg + i;
dataSelect.style.opacity = 1;
dataSelect.style.transitionDelay = (delay -= 0.02) + "s";
}
});
}
//reset & autoprefixed used
//I didn't use any transitions during the sort. I wanted to keep the concept extremely simple. Maybe in the future I'll add some cool transitions during the sort.
body, html {
height: 100%;
font-size: 100%
}
.button-wrapper {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
background: lighten(lightblue,13%);
padding-bottom: 10px;
}
button {
font: 1.3rem 'Open Sans Condensed', sans-serif;
padding: 10px;
margin-top: 10px;
cursor: pointer;
border: none;
background: rgb(55,55,55);
color: #fff;
font-family: 'Open Sans Condensed', sans-serif;
transition: background 0.3s ease-in-out
}
button:hover {
background: rgb(105,105,105);
}
ul {
list-style: none;
min-height: 100%;
display: flex;
flex-flow: row wrap;
justify-content: space-around;
align-items: center;
background: lighten(lightblue,17%);
padding-top: 20px
}
.sortable {
display: flex;
flex-flow: column nowrap;
padding-bottom: 20px;
width: calc(25% - 40px);
transition: opacity 0.3s ease-in-out,
}
.hidden {
opacity: 0
}
.fake-image, .price {
width: 100%;
text-align: center;
background: #fff;
}
.fake-image {
font-size: 7rem;
color: darken(DarkGreen,5%);
padding-top: 10px
}
.price {
font: 1.7rem "Oswald", sans-serif;
color: rgb(75,75,75);
}
//Some simple media queries
@media only screen and (max-width: 768px) {
.sortable {
width: calc(50% - 20px)
}
}
@media only screen and (max-width: 480px) {
.sortable {
width: 100%
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment