Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Cascading Dropdown from CSV

  1. #11
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,130
    Thanks
    44
    Thanked 3,228 Times in 3,189 Posts
    Blog Entries
    12

    Default

    Using (curly brackets here):

    Code:
    var categories = {};
    Not (square brackets):

    Code:
    var categories = [];
    Would be more correct and still works. The use of square brackets connotes an array (used properly here):

    Code:
    ["Select Carrier", "ATT", "Verizon", "Tmobile", "Sprint"]
    or a property of an object (used properly for that here):

    Code:
    categories["Apple||Iphone 5 16GB"]
    Curly brackets connote an object. Examples:

    Code:
    var categories = {};
    Code:
    var conventionally_attractive = {girl: 'pretty', boy: 'handsome'};
    Code:
    var colors = {dark: ['black', 'navy'], light: ['yellow', 'white']};
    So your object could be written (tested and works) as:

    Code:
    var categories = {
    
    "Apple": ["Select Apple Device", "Iphone 5 16GB","Iphone 5 32GB","Iphone 5 64GB","Iphone 4S 16GB","Iphone 4S 32GB","Iphone 4S 64GB","Iphone 4 8GB","Iphone 4 16GB","Iphone 4 32GB"],
    
      "Apple||Iphone 5 16GB": ["Select Carrier", "ATT", "Verizon", "Tmobile", "Sprint"], 
    
       "Apple||Iphone 5 16GB||ATT": ["Select Condition", "NewInBox - $550.00","Good - $400.00","Cracked - $280.00","Broken - $225.00","Dead - $210.00"],  
     
       "Apple||Iphone 5 16GB||Verizon": ["Select Condition","NewInBox - $550.00","Good - $400.00","Cracked - $280.00","Broken - $225.00","Dead - $210.00"],  
       
       "Apple||Iphone 5 16GB||Tmobile": ["Select Condition", "NewInBox - $350.00","Good - $300.00","Cracked - $280.00","Broken - $150.00","Dead - $120.00"],  
       
       "Apple||Iphone 5 16GB||Sprint": ["Select Condition", "NewInBox - $350.00","Good - $280.00","Cracked - $280.00","Broken - $170.00","Dead - $140.00"],  
    
       
    "Samsung": ["Select Samsung Device", "Iphone 5 16GB","Iphone 5 32GB","Iphone 5 64GB","Iphone 4S 16GB","Iphone 4S 32GB","Iphone 4S 64GB","Iphone 4 8GB","Iphone 4 16GB","Iphone 4 32GB"],
    
        "Samsung||Iphone 5 16GB": ["Select Carrier","ATT","Verizon","Tmobile","Sprint"], 
    
       "Samsung||Iphone 5 16GB||ATT": ["Select Condition", "Samsung NewInBox - $550.00","Good - $400.00","Cracked - $280.00","Broken - $225.00","Dead - $210.00"],  
     
       "Samsung||Iphone 5 16GB||Verizon": ["Select Condition", "Samsung NewInBox - $550.00","Good - $400.00","Cracked - $280.00","Broken - $225.00","Dead - $210.00"],  
       
       "Samsung||Iphone 5 16GB||Tmobile": ["Select Condition", "Samsung NewInBox - $350.00","Good - $300.00","Cracked - $280.00","Broken - $150.00","Dead - $120.00"],  
       
       "Samsung||Iphone 5 16GB||Sprint": ["Select Condition", "Samsung NewInBox - $350.00","Good - $280.00","Cracked - $280.00","Broken - $170.00","Dead - $140.00"]
       
    };
    It's technically more correct and saves some typing.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #12
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Using (curly brackets here):

    Code:
    var categories = {};
    Not (square brackets):

    Code:
    var categories = [];
    Would be more correct and still works. The use of square brackets connotes an array
    I doubt that internally it makes any difference.

    Nesting the entire declaration inside {}, means that the properties of the object have to be comma-separated, which introduces far more opportunity for error, a major consideration in what may become a published script for use by non-programmers. Conversely declaring each property separately using associative array syntax, in my view makes any error easier to find.

  3. #13
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,130
    Thanks
    44
    Thanked 3,228 Times in 3,189 Posts
    Blog Entries
    12

    Default

    What you say about the structure makes sense and can be done regardless of whether or not the initial declaration of categories is as an Object or as an Array.

    That doesn't change the fact that there are no associative arrays as arrays in javascript. What's known as an associative array in other languages is in javascript an object. For the most part you can probably get away with exploiting the object properties of Array as you have done. But it could get you into trouble at some point. You can have both though, the more verbose, less error prone syntax you say you favor, and proper use of javascript Objects and Arrays:

    Code:
    var categories = {};
    
    categories["Apple"] = ["Select Apple Device", "Iphone 5 16GB","Iphone 5 32GB","Iphone 5 64GB","Iphone 4S 16GB","Iphone 4S 32GB","Iphone 4S 64GB","Iphone 4 8GB","Iphone 4 16GB","Iphone 4 32GB"];
    
      categories["Apple||Iphone 5 16GB"] = ["Select Carrier", "ATT", "Verizon", "Tmobile", "Sprint"]; 
    
       categories["Apple||Iphone 5 16GB||ATT"] = ["Select Condition", "NewInBox - $550.00","Good - $400.00","Cracked - $280.00","Broken - $225.00","Dead - $210.00"];  
     
       categories["Apple||Iphone 5 16GB||Verizon"] = ["Select Condition","NewInBox - $550.00","Good - $400.00","Cracked - $280.00","Broken - $225.00","Dead - $210.00"];  
       
       categories["Apple||Iphone 5 16GB||Tmobile"] = ["Select Condition", "NewInBox - $350.00","Good - $300.00","Cracked - $280.00","Broken - $150.00","Dead - $120.00"];  
       
       categories["Apple||Iphone 5 16GB||Sprint"] = ["Select Condition", "NewInBox - $350.00","Good - $280.00","Cracked - $280.00","Broken - $170.00","Dead - $140.00"];  
    
       
    categories["Samsung"] = ["Select Samsung Device", "Iphone 5 16GB","Iphone 5 32GB","Iphone 5 64GB","Iphone 4S 16GB","Iphone 4S 32GB","Iphone 4S 64GB","Iphone 4 8GB","Iphone 4 16GB","Iphone 4 32GB"];
    
       categories[ "Samsung||Iphone 5 16GB" ] = ["Select Carrier","ATT","Verizon","Tmobile","Sprint"]; 
    
       categories["Samsung||Iphone 5 16GB||ATT"] = ["Select Condition", "Samsung NewInBox - $550.00","Good - $400.00","Cracked - $280.00","Broken - $225.00","Dead - $210.00"];  
     
       categories["Samsung||Iphone 5 16GB||Verizon"] = ["Select Condition", "Samsung NewInBox - $550.00","Good - $400.00","Cracked - $280.00","Broken - $225.00","Dead - $210.00"];  
       
       categories["Samsung||Iphone 5 16GB||Tmobile"] = ["Select Condition", "Samsung NewInBox - $350.00","Good - $300.00","Cracked - $280.00","Broken - $150.00","Dead - $120.00"];  
       
       categories["Samsung||Iphone 5 16GB||Sprint"] = ["Select Condition", "Samsung NewInBox - $350.00","Good - $280.00","Cracked - $280.00","Broken - $170.00","Dead - $140.00"];
    I didn't mean to insist that you adopt the wrapped syntax, merely to suggest it and to point out that it was less verbose.

    Something you cannot do though is to use Array as an Object and use the wrapped syntax. So using the technically more correst object gives greater flexibility down the line. Also considering the wide use of jQuery.extend() and similar in other libraries (or even made using native code) when working with Objects, and javascript's own for(var p in Object) where you can use/iterate over both p and Object[p], which can be very handy for working with javascript objects, it's probably best, regardless of the syntax, to use an actual object, rather than an array as an object when an object is what you're constructing. My thinking being that one or more of these methods would not work with a javascript Array used as an Object.
    Last edited by jscheuer1; 06-26-2013 at 03:55 AM. Reason: detail
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. PHP AJAX Cascading Dropdown listbox
    By goldd1gger in forum PHP
    Replies: 2
    Last Post: 07-01-2013, 07:49 AM
  2. CSS not cascading into table
    By gib65 in forum CSS
    Replies: 3
    Last Post: 08-26-2012, 07:20 AM
  3. Replies: 0
    Last Post: 03-25-2010, 02:14 PM
  4. Cascading tabs
    By rod_didier in forum JavaScript
    Replies: 1
    Last Post: 08-18-2009, 11:07 PM
  5. CSS - Cascading Style Sheets
    By Angel8552 in forum CSS
    Replies: 1
    Last Post: 07-11-2007, 02:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •