javascript - jQuery filter - sort by attribute and sort by price -
i'm trying create dropdown filters content. have 2 different types of filters in this: 1) filter sorts price 2) filter sorts attribute tagged in product.
i'm new implementing type of request. have 2 semi-working, seems 2 filters conflicting each other. example: if filter price first, , try filter attribute (dropdown), doesn't filter correctly. filters correctly if filter down either price or dropdown.
also, right have price filtering buttons, price in separate dropdown well. i'd add "best seller" attribute in there, , tag products in terms of best-selling. possible?
can help? take look?
thanks , help!
fiddle: https://jsfiddle.net/daysable/eags255n/
my code below:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> </head> <body> <p>what speed need? </p> <button id="asc"> sort price asd</button><button id="desc"> sort price desc</button> <p>what speed bike need? </p> <select class="filterby"> <option value="all"><h5>see all</h5></option> <option value="15"><h5>15</h5></option> <option value="20"><h5>20</h5></option> <option value="25"><h5>25</h5></option> <option value="50"><h5>50</h5></option> <option value="150"><h5>150</h5></option> <option value="160"><h5>160</h5></option> </select> <p>what watch? </p> <select class="filterby"> <option value="all"><h5>see packages</h5></option> <option value="showtime"><h5>showtime</h5></option> <option value="hbo"><h5>hbo</h5></option> <option value="cinemax"><h5>cinemax</h5></option> <option value="starz"><h5>starz</h5></option> <option value="ondemand"><h5>on demand</h5></option> </select> <p>do want contract? </p> <select class="filterby"> <option value="all"><h5>see deals?</h5></option> <option value="contract"><h5>contract</h5></option> <option value="nocontract"><h5>no contract</h5></option> </select> </ul> <div class="filtersorting"> <div id="filtercontainer"> <!-- card 1 --> <li class="ux-offer ondemand hbo 50 contract"> <div class="offer"> <p class="offer-lob">50 mph</p> <div class="container"> <div class="offer-price"> <p class="title">bike 1</p> <p class="ux-priceoffer"> <span class="ux-priceoffer--currency">$</span> <span class="ux-priceoffer--dollars">89</span> <span class="ux-priceoffer--cents">99/mo</span> <span class="ux-priceoffer--duration">for 12 mo.</span> </p> <a class="ux-button" href="#">add cart</a> </div> </div> </li> <!-- card 2 --> <li class="ux-offer showtime 15 nocontract"> <div class="offer"> <p class="offer-lob">15 mph</p> <div class="container"> <div class="offer-price"> <p class="title">bike 2</p> <p class="ux-priceoffer"> <span class="ux-priceoffer--currency">$</span> <span class="ux-priceoffer--dollars">44</span> <span class="ux-priceoffer--cents">99/mo</span> <span class="ux-priceoffer--duration">for 6 mo.</span> </p> <a class="ux-button" href="#">add cart</a> </div> </div> </li> <!-- card 3 --> <li class="ux-offer ondemand cinemax 20 contract"> <div class="offer"> <p class="offer-lob">20 mph</p> <div class="container"> <div class="offer-price"> <p class="title">bike 3</p> <p class="ux-priceoffer"> <span class="ux-priceoffer--currency">$</span> <span class="ux-priceoffer--dollars">109</span> <span class="ux-priceoffer--cents">99/mo</span> <span class="ux-priceoffer--duration">for 6 mo.</span> </p> <a class="ux-button" href="#">add cart</a> </div> </div> </li> <!-- card 4 --> <li class="ux-offer hbo 150 contract"> <div class="offer"> <p class="offer-lob">150 mph</p> <div class="container"> <div class="offer-price"> <p class="title">bike 4</p> <p class="ux-priceoffer"> <span class="ux-priceoffer--currency">$</span> <span class="ux-priceoffer--dollars">80</span> <span class="ux-priceoffer--cents">99/mo</span> <span class="ux-priceoffer--duration">for 12 mo.</span> </p> <a class="ux-button" href="#">add cart</a> </div> </div> </li> <!-- card 5 --> <li class="ux-offer showtime starz 25 nocontract"> <div class="offer"> <p class="offer-lob">25 mph</p> <div class="container"> <div class="offer-price"> <p class="title">bike 5</p> <p class="ux-priceoffer"> <span class="ux-priceoffer--currency">$</span> <span class="ux-priceoffer--dollars">220</span> <span class="ux-priceoffer--cents">99/mo</span> <span class="ux-priceoffer--duration">for 12 mo.</span> </p> <a class="ux-button" href="#">add cart</a> </div> </div> </li> <!-- card 6 --> <li class="ux-offer hbo starz ondemand 160 contract"> <div class="offer"> <p class="offer-lob">160 mph</p> <div class="container"> <div class="offer-price"> <p class="title">bike 6</p> <p class="ux-priceoffer"> <span class="ux-priceoffer--currency">$</span> <span class="ux-priceoffer--dollars">50</span> <span class="ux-priceoffer--cents">99/mo</span> <span class="ux-priceoffer--duration">for 3 mo.</span> </p> <a class="ux-button" href="#">add cart</a> </div> </div> </li> </div> </body> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> $("select.filterby").change(function(){ var filters = $.map($("select.filterby").toarray(), function(e){ return $(e).val(); }).join("."); $("div#filtercontainer").find("li.ux-offer").hide(); $("div#filtercontainer").find("li." + filters).show(); }); </script> <script> function sortbyprice(a,b){ return $(a).find('.ux-priceoffer').text() > $(b).find('.ux-priceoffer').text(); } function sortbypricedesc(a,b){ return $(a).find('.ux-priceoffer').text() < $(b).find('.ux-priceoffer').text(); } function reorderel(el){ var container = $('.filtersorting'); container.html(''); el.each(function(){ $(this).appendto(container); }); } $('#asc').click(function(){ reorderel($('.ux-offer').sort(sortbyprice)); }); $('#desc').click(function(){ reorderel($('.ux-offer').sort(sortbypricedesc)); }); </script> </html>
okay, couple of things, might wise insert data somewhere instead of hard-coding html. make text file , read array, even. way can array of total cost, , run .sort
against since you're doing weird stuff payment plans(i.e $x x months). (like hinted) want use sort on elements, way return entire sorted list, , can append entire list.
and of course isn't way this, seems intuitive me, @ least price, since want sort list in ascending or descending order.
Comments
Post a Comment