Advertisement
Imperative-Ideas

Front end sort order example - the jQuery part

Sep 23rd, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2.  * A set of JS and jQuery functions/executions. For plugins, see plugins.min.js.
  3.  */
  4.  
  5. // This allows quick retrieval of URL parameters
  6. function GetURLParameter(sParam) {
  7.     var sPageURL = window.location.search.substring(1);
  8.     var sURLVariables = sPageURL.split('&');
  9.     for (var i = 0; i < sURLVariables.length; i++)
  10.     {
  11.         var sParameterName = sURLVariables[i].split('=');
  12.         if (sParameterName[0] == sParam)
  13.         {
  14.             return sParameterName[1];
  15.         }
  16.     }
  17. }
  18.  
  19. jQuery(function($) {
  20.  
  21.     /** This function sets the up & down arrow & sort parameters on click in the front end **/
  22.  
  23.     // Set variables for possible URL parameters used in the script
  24.     var orderby = GetURLParameter('orderby');
  25.     var order = GetURLParameter('order');
  26.     var active = GetURLParameter('active');
  27.  
  28.     // Now that we know what parameters are used, let's build our output string. Note that
  29.     // we don't care about the "order" parameter because we're building this to control the
  30.     // arrow itself -- it's manually assigned in the switch (later).
  31.     function BuildURL() {
  32.         var findParams = [];
  33.         var iteration = 0;
  34.         var target = "";
  35.  
  36.         // Find out which params are defined and build the full strings into an array
  37.         if(typeof (orderby) != 'undefined' && orderby != null ) {
  38.             findParams.push("orderby=" + orderby);
  39.         }
  40.         if(typeof (active) != 'undefined' && orderby != null ) {
  41.             findParams.push("active=" + active);
  42.         }
  43.  
  44.         // Loop over the array and insert the appropriate separators
  45.         $.each(findParams, function(key,value) {
  46.             iteration++;
  47.             if ( iteration === 1 ) {
  48.                 target += "?";
  49.             } else {
  50.                 target += "&";
  51.             }
  52.             target += value;
  53.         });
  54.  
  55.         // Set final seperators depending on what was defined then return the result
  56.         if(typeof (target) != 'undefined' && target != '' && target != null) {
  57.             target = target + "&";
  58.             return target;
  59.         } else {
  60.             target = "?";
  61.             return target;
  62.         }
  63.     }
  64.  
  65.     // Grab the sort buttons for manipulation
  66.     var sortBar = $('.sortbar');
  67.     var sortButton = sortBar.find('div');
  68.     var byPrice = $('div.byprice');
  69.     var theURL = window.location.href.split('?');
  70.  
  71.     // Check which button is active and add it to a variable
  72.     switch(orderby) {
  73.         case 'property_price' :
  74.             byPrice.addClass('active');
  75.             break;
  76.         case 'property_revenue' :
  77.             $('.byrev').addClass('active');
  78.             break;
  79.         case 'adjusted_sde' :
  80.             $('.bysde').addClass('active');
  81.             break;
  82.         default :
  83.             // default sort is by price
  84.             byPrice.addClass('active');
  85.     }
  86.  
  87.     // On the active button, check the sort order
  88.     var activeSort = sortButton.find('.active');
  89.  
  90.     switch(order) {
  91.         case 'asc' :
  92.             sortBar.find('.active').find('i').removeClass('fa-sort-desc').addClass('fa-sort-asc');
  93.             sortBar.find('.active').find('a').attr('href', theURL[0] + BuildURL() + "order=desc");
  94.             break;
  95.         case 'desc' :
  96.             sortBar.find('.active').find('a').attr('href', theURL[0] + BuildURL() + "order=asc");
  97.             break;
  98.         default :
  99.             sortBar.find('.active').find('a').attr('href', theURL[0] + BuildURL() + "order=asc");
  100.     }
  101. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement