1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Conditional product menu

Discussion in 'JavaScript' started by bellcom, Apr 22, 2014.

  1. #1
    I saw a menu on this site http://www.royaltycore.com/
    scroll down where it says "Find your Grille"
    It populates the 2 menus based on previous selections.

    So before hiring someone I am trying to create something similar with different products so I copied the page and left all the scripts in tact since I wasn't sure on what was safe to remove.

    So I uploaded and loaded that page in chrome and it shows its trying to load from the originals site: http://www.royaltycore.com/listcats_17?stpl=getmodels

    Not sure how this works but viewing the source & the console in chrome, would this be easy to get working on another site or is there a lot missing?

    bellcom, Apr 22, 2014 IP
  2. Serinth

    Serinth Peon

    Likes Received:
    Best Answers:
    Trophy Points:
    If you wanted to do it in pure javascript you would need to pre-store all the different combinations in variables and do the fetching based on events e.g. click events. Of course this means that it's not as flexible and you'll have to hand edit the combinations each time you have new products and implement some kind of versioning system on the javascript filename so that it loads the new combinations when an existing user hits the page.

    However, this looks like it has a backend that has the combinations loaded somewhere and it's requesting the results through an ajax call. This would be nicer since you can just simply add or remove products from a database somewhere. This way is a bit more involved than the first but more flexible. A copy and paste job won't work here if you're looking to use your own dataset and ui.
    Serinth, Apr 23, 2014 IP
  3. deathshadow

    deathshadow Prominent Member

    Likes Received:
    Best Answers:
    Trophy Points:
    This is one of those things where the way most people go about it just pisses me off; the vast majority of implementations are inaccessible garbage because the people writing them never seem to remember the unwritten rule of JavaScript -- if you can't make the page work without scripting FIRST, you likely have no business adding scripting to it. They throw nonsense like AJAX at something that should be relatively simple by first making a useful page without scripting.

    I would probably just output all the options first as a list of anchors, with some form of delimiter (vertical break good as any) you can use to split them apart.

    Something like:
    Code (Text):
    1. <li><a href="ford_raptor_2009-2014.html">Ford | Raptor | 2009-2014</a></li>
    I would then getElementsByTagName('a') off the parent UL to grab those anchors, use SPLIT to break them apart, then plug them into an object by Make, then models inside each make via object, then year under make. Then I'd delete the original list to replace it with the scripted version built from those objects.

    This way you have your fancy script-tard menu when scripting is available, AND a flat menu of anchors for when scripting is disabled/unavailable and/or meaningless; screen readers, braille readers, and search engines being great examples of when that type of scripted drill-down is pretty much useless. It also means no need for goofy tricks like AJAX that would make the client side scripting AND server code more complex.

    That page for example has what? ~40 possible outcomes? 40 sets of LI/A won't even break 3k of markup. Most people piss away more than that on a single presentational image in the theme. Even 400 possibilities would be ~30k; still smaller than that fat bloated train wreck of idiocy known as jQuery compressed!

    It's why I laugh when people make endless pointless scripttard bull, throwing more scripttard nonsense at it, when their entire data set can usually be sent in less code.

    I'm busy today (at least I will be assuming I get the phone call I'm SUPPOSED to get the next hour or so), but I'll try to squeeze in the time to put together a quick demo showing my approach in action.
    deathshadow, Apr 23, 2014 IP
  4. bellcom

    bellcom Member

    Likes Received:
    Best Answers:
    Trophy Points:
    Here is what I have so far after getting some help from someone here:

    Here is the code he gave me:

    There are a couple commented area for retrieving the data to be displayed but I don't know what is needed?
    //Here you would usually do an ajax request to retrieve the children based on parent,
    //Below is just a faked version.

    Then another:
    /* Could Use something like this retrieve the html you want to display */
    function requestDataFor2(parentID,target) {
    $(target).load('[URL TO GET THE DATA]/id='+parentID);

    Any help appreciated!!!!
    bellcom, Apr 25, 2014 IP
  5. ketting00

    ketting00 Active Member

    Likes Received:
    Best Answers:
    Trophy Points:
    You create some files like data-0.php to store data you want to display on click or pull it from database.
    And this is the laziest way to get it done (just for example) ;)
    Code (Text):
    2. $('ul li').click(function() {
    3.    $('#level2').load('data-' + $(this).index() + '.php');
    4.    console.log('data-' + $(this).index() + '.php'); // just to see which file data is from
    5. });
    No need to append those lists to your div
    ketting00, Apr 25, 2014 IP
  6. bellcom

    bellcom Member

    Likes Received:
    Best Answers:
    Trophy Points:
    So I changed the javascript to what is below and loads the "model" menu with data from the data-0.php files.
    Now how do I get my "Model" links to populate the next "Year" menu?
    Here is a test page:

    Code (Text):
    1. $('ul li').click(function() {
    2.    $('#level2').load('data-' + $(this).index() + '.php');
    3.    console.log('data-' + $(this).index() + '.php'); // just to see which file data is from
    4. });
    6. function requestDataFor(parentID,target) {
    7.     //Here you would usually do an ajax request to retrieve the children based on parent,
    8.     //Below is just a faked version.
    10.     var res = '<ul>';
    11.     for(var i =0; i<20; i++) {
    12.         res += '<li><a href="#" data-id="'+(i*parentID*5)+'">Model '+(i*parentID)+'</a></li>';
    13.     }
    14.     res += '</ul>';
    16.     $(target).html(res);
    17. }
    20. /* Could Use something like this retrieve the html you want to display */
    21. function requestDataFor2(parentID,target) {
    22.     $(target).load('[URL TO GET THE DATA]/id='+parentID);
    23. }
    bellcom, Apr 25, 2014 IP
  7. ketting00

    ketting00 Active Member

    Likes Received:
    Best Answers:
    Trophy Points:
    @bellcom You may put everything you want to display in the file which is loaded and style them with CSS (the level3 div). You don't need to keep that requestDataFor function.

    If you want to copy the features exactly off royaltycore.com you may do something like this:
    Code (Text):
    2. <!DOCTYPE html>
    3. <html language="en">
    4. <head>
    5.    <style>
    6.      ul#myUL li {
    7.        display:inline-block;
    8.        padding:5px;
    9.        height:80px;
    10.        width:80px;
    11.        border:#ccc solid 1px;
    12.      }
    13.      .hide {
    14.        display:none;
    15.      }
    16.    </style>
    17.    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    18. </head>
    19.    <body>
    20.      <h2>Make</h2>
    21.      <div>
    22.        <ul id="myUL">
    23.          <li><a href="#" data-id="1">Make 1</a></li>
    24.          <li><a href="#" data-id="2">Make 2</a></li>
    25.          <li><a href="#" data-id="3">Make 3</a></li>
    26.          <li><a href="#" data-id="4">Make 4</a></li>
    27.        </ul>
    28.      </div>
    29.      <h2>Model</h2>
    30.      <div id="level2">
    31.        <div id="box1" class="hide">
    32.          List 1
    33.        </div>
    34.        <div id="box2" class="hide">
    35.          List 2
    36.        </div>
    37.        <div id="box3" class="hide">
    38.          List 3
    39.        </div>
    40.        <div id="box4" class="hide">
    41.          List 4
    42.        </div>
    43.      </div>
    44.      <h2>Year</h2>
    45.      <div id="level3">
    46.      </div>
    47.      <script>
    48.        $('#myUL li').click(function() {
    49.          $('#box' + ($(this).index() + 1)).show().siblings("div").hide();
    50.          $('#level3').load('data-' + $(this).index() + '.php');
    51.        });
    52.      </script>
    53.    </body>
    54. </html>
    To show detail of each model and year, you might want to repeat the step above and create level4 div. This simple demo does not cover all the thing you need. It just shows how it might have worked.

    Maybe @deathshadow may come up with better solution.
    Last edited: Apr 26, 2014
    ketting00, Apr 25, 2014 IP