2011-08-20 22 views
6

मैं मेनू आइटम की एक सरणी, प्रत्येक युक्त नाम और URL ऐसा है का URL पथ संरचना के आधार पर:एक नेस्ट यूएल मेनू बनाएं मेनू आइटम

var menuItems = [ 
    { 
     name : "Store", 
     url : "/store" 
    }, 
    { 
     name : "Travel", 
     url : "/store/travel" 
    }, 
    { 
     name : "Gardening", 
     url : "/store/gardening" 
    }, 
    { 
     name : "Healthy Eating", 
     url : "/store/healthy-eating" 
    }, 
    { 
     name : "Cook Books", 
     url : "/store/healthy-eating/cook-books" 
    }, 
    { 
     name : "Single Meal Gifts", 
     url : "/store/healthy-eating/single-meal-gifts" 
    }, 
    { 
     name : "Outdoor Recreation", 
     url : "/store/outdoor-recreation" 
    }, 
    { 
     name : "Hiking", 
     url : "/store/outdoor-recreation/hiking" 
    }, 
    { 
     name : "Snowshoeing", 
     url : "/store/outdoor-recreation/hiking/snowshoeing" 
    }, 
    { 
     name : "Skiing", 
     url : "/store/outdoor-recreation/skiing" 
    }, 
    { 
     name : "Physical Fitness", 
     url : "/store/physical-fitness" 
    }, 
    { 
     name : "Provident Living", 
     url : "/store/provident-living" 
    } 
] 

मैं रेंडर करने के लिए कोई सफलता के साथ की कोशिश कर रहा है यह एक नेस्ट यूएल संरचना के साथ एक बिना क्रम वाली सूची कि इतने तरह URL पथ संरचना इस प्रकार है:

<ul> 
    <li><a href="/store">Store</a> 
     <ul> 
     <li><a href="/store/travel">Travel</a></li> 
     <li><a href="/store/gardening">Gardening</a></li> 
     <li><a href="/store/healthy-eating">Healthy Eating</a> 
      <ul> 
      <li><a href="/store/healthy-eating/cook-books">Cook Books</a></li> 
      <li><a href="/store/healthy-eating/single-meal-gifts">Single Meal Gifts</a></li> 
      </ul> 
     </li> 
     <li><a href="/store/outdoor-recreation">Outdoor Recreation</a> 
      <ul> 
      <li><a href="/store/outdoor-recreation/hiking">Hiking</a> 
       <ul> 
       <li><a href="/store/outdoor-recreation/hiking/snowshoeing">Snowshoeing</a></li> 
       </ul> 
      </li> 
      <li><a href="/store/outdoor-recreation/skiing">Skiing</a></li> 
      </ul> 
     </li> 
     <li><a href="/store/physical-fitness">Physical Fitness</a></li> 
     <li><a href="/store/provident-living">Provident Living</a></li> 
     </ul> 
    </li> 
</ul> 

उदाहरण मैं एक डेटा संरचना है कि माता-पिता के बच्चे का रिश्ता (जैसे xml या दर्शाता है के साथ शुरू देखा है सब के सब जेएसओएन), लेकिन मुझे यूआरएल से बाहर खींचने और उपयोग करने में बहुत मुश्किल समय है यह नई संरचना प्रस्तुत करने के लिए।

अगर कोई मुझे jQuery का उपयोग करके ऐसा करने के लिए सही दिशा में चला सकता है, तो मैं वास्तव में इसकी सराहना करता हूं। मुझे एहसास है कि मुझे शायद कुछ रिकर्सिव फ़ंक्शंस या शायद jQuery टेम्पलेट्स का उपयोग करने की आवश्यकता है, लेकिन ये चीजें अभी भी मेरे लिए थोड़ी नई हैं।
धन्यवाद

उत्तर

0

यह jQuery में नहीं है, लेकिन शायद यह मदद कर सकता है। मैंने यह वही करने के लिए वेब की तलाश करने के बाद इसे विकसित किया जो आप चाहते हैं।

http://www.chapleau.info/article/ArrayofUrlsToASitemap.html

6

मुझे लगता है कि सबसे अच्छा समाधान सबसे पहले है माता-पिता/बच्चों संबंधों के साथ, एक पेड़ से एक करने के लिए अपने डेटा संरचना कन्वर्ट करने के लिए। इस संरचना को प्रस्तुत करना आसान होगा, क्योंकि यूएल के पास पेड़ की संरचना है।

आप कार्यों

// Add an item node in the tree, at the right position 
function addToTree(node, treeNodes) { 

    // Check if the item node should inserted in a subnode 
    for (var i=0; i<treeNodes.length; i++) { 
     var treeNode = treeNodes[i]; 

     // "/store/travel".indexOf('/store/') 
     if (node.url.indexOf(treeNode.url + '/') == 0) { 
      addToTree(node, treeNode.children); 

      // Item node was added, we can quit 
      return; 
     } 
    } 

    // Item node was not added to a subnode, so it's a sibling of these treeNodes 
    treeNodes.push({ 
     name: node.name, 
     url: node.url, 
     children: [] 
    }); 
} 

//Create the item tree starting from menuItems 
function createTree(nodes) { 
    var tree = []; 

    for (var i=0; i<nodes.length; i++) { 
     var node = nodes[i]; 
     addToTree(node, tree); 
    } 

    return tree; 
} 

var menuItemsTree = createTree(menuItems); 
console.log(menuItemsTree); 

के इन जोड़ी का उपयोग कर menuItems परिवर्तित कर सकते हैं, जिसके परिणामस्वरूप menuItemsTree इस

[ 
    { 
    "name":"Store", 
    "url":"/store", 
    "children":[ 
     { 
     "name":"Travel", 
     "url":"/store/travel", 
     "children":[ 

     ] 
     }, 
     { 
     "name":"Gardening", 
     "url":"/store/gardening", 
     "children":[ 

     ] 
     }, 
     { 
     "name":"Healthy Eating", 
     "url":"/store/healthy-eating", 
     "children":[ 
      { 
      "name":"Cook Books", 
      "url":"/store/healthy-eating/cook-books", 
      "children":[ 

      ] 
      }, 
      { 
      "name":"Single Meal Gifts", 
      "url":"/store/healthy-eating/single-meal-gifts", 
      "children":[ 

      ] 
      } 
     ] 
     }, 
     { 
     "name":"Outdoor Recreation", 
     "url":"/store/outdoor-recreation", 
     "children":[ 
      { 
      "name":"Hiking", 
      "url":"/store/outdoor-recreation/hiking", 
      "children":[ 
       { 
       "name":"Snowshoeing", 
       "url":"/store/outdoor-recreation/hiking/snowshoeing", 
       "children":[ 

       ] 
       } 
      ] 
      }, 
      { 
      "name":"Skiing", 
      "url":"/store/outdoor-recreation/skiing", 
      "children":[ 

      ] 
      } 
     ] 
     }, 
     { 
     "name":"Physical Fitness", 
     "url":"/store/physical-fitness", 
     "children":[ 

     ] 
     }, 
     { 
     "name":"Provident Living", 
     "url":"/store/provident-living", 
     "children":[ 

     ] 
     } 
    ] 
    } 
] 

आपने कहा आप पहले से ही, पेड़ों के लिए HTML रेंडरर है सही तरह एक वस्तु हो जाएगा? अगर आपको और मदद की ज़रूरत है तो हमें बताएं!

+2

यह बहुत अच्छी तरह से काम करता है अद्यतन, लेकिन एक 'अपवाद': यह एक माता पिता/mainnode की तरह जोड़ दिया जाएगा अगर वहाँ एक परिभाषित माता पिता नोड के बिना एक उपनोड है। मुझे नहीं पता कि यह एक वांछित दुष्प्रभाव है, लेकिन यह व्यवहार्य होगा। – buschtoens

+0

क्षमा करें, लेकिन यह काम नहीं करता है, मैंने कई बार आपके कोड की कोशिश की। यह पहले नोड के लिए ठीक काम करता है लेकिन जब तत्काल अगले भाई में बच्चे होते हैं- अगले भाई के बच्चे पहले नोड के बच्चों की सूची में जोड़े जाते हैं। –

+0

@ डेविड, पेड़ के लिए प्रस्तुत करने के लिए आकार वाले HTML डेटा का उपयोग कैसे करें? –

0

इस तरह कुछ कोशिश करें।

function Directory(parentNode) { 
    //Structure for directories. Subdirectories container as a generic object, initially empty 
    this.hasSubdirectories = false; 
    this.subdirectories = {}; 

    //Render in steps. Until subdirectories or a link are added, all it needs is an LI and a blank anchor 
    this.nodeLi = document.createElement("li"); 
    parentNode.appendChild(this.nodeLi); 
    this.nodeA = document.createElement("a"); 
    this.nodeLi.appendChild(this.nodeA); 

    //if a subdirectory is added, this.nodeUl will be added at the same time. 
} 

Directory.prototype.setLabel = function (sLabel) { 
    this.nodeA.innerHTML = sLabel; 
} 

Directory.prototype.setLink = function (sLink) { 
    this.nodeA.href = sLink; 
} 

Directory.prototype.getSubdirectory = function (sPath) { 
    //if there were no previous subdirectories, the directory needs a new UL node. 
    if (!this.hasSubdirectories) { 
     this.nodeUl = document.createElement("ul"); 
     this.nodeLi.appendChild(this.nodeUl); 
     this.hasSubdirectories = true; 
    } 

    //split the path string into the base directory and the rest of the path. 
    var r = /^\/?(?:((?:\w|\s|\d)+)\/)(.*)$/; 
    var path = r.exec(sPath); 

    //if the desired path is in a subdirectory, find or create it in the subdirectories container. 

    var subDirName = path[1] || path[2]; 
    var subDir; 
    if (this.subdirectories[subDirName] === undefined) this.subdirectories[subDirName] = new Directory(this.nodeUl); 
    subDir = this.subdirectories[subDirName]; 

    if (path[1] && path[2]) { 
     return subDir.getSubdirectory(path[2]); 
    } else { 
     return subDir; 
    } 
} 

function main(whichNode, aMenuItems) { 
    //whichNode is the node that is to be the parent of the directory listing. 
    //aMenuItems is the array of menu items. 
    var i; 
    var l = aItems.length; 
    var topDir = new Directory(whichNode); 

    //for each menu item, add a directory and set its properties. 
    var dirToAdd; 
    for (i = 0; i < l; i++) { 
     dirToAdd = topDir.getSubdirectory(aMenuItems[i].url); 
     dirToAdd.setLabel(aMenuItems[i].name); 
     dirToAdd.setLink(aMenuItems[i].url); 
    } 

    //and that's it. 
} 

यह काम कैसा है?

0

या हो सकता है पूरा jQuery प्लगइन http://jsfiddle.net/9FGRC/

(संपादित)

एक अद्यतन पिछले संस्करण में http://jsfiddle.net/9FGRC/1/

इस संस्करण मामले

var menuItems = [ 
    { 
     name : "Store", 
     url : "/store" 
    }, 
    { 
     name : "Cook Books", 
     url : "/store/healthy-eating/cook-books" 
    }, 
    { 
     name : "Single Meal Gifts", 
     url : "/store/healthy-eating/single-meal-gifts" 
    } 
] 

निम्नलिखित का समर्थन करता है के बाद से वहाँ छोड़ दिया है

{ 
     name : "Healthy Eating", 
     url : "/store/healthy-eating" 
    }, 

यह निम्न एचटीएमएल का उत्पादन करेगा

<ul> 
    <li><a href="/store">Store</a> 
     <ul> 
      <li><a href="/store/healthy-eating/cook-books">Cook Books</a></li> 
      <li><a href="/store/healthy-eating/single-meal-gifts">Single Meal Gifts</a></li> 
     </ul> 
    </li> 
</ul> 

मुझे लगता है कि यह मामला नहीं होगा, लेकिन कोड की

2

12 सरल रेखाओं किसी के लिए उपयोगी हो सकता है:

var rootList = $("<ul>").appendTo("body"); 
var elements = {}; 
$.each(menuItems, function() { 
    var parent = elements[this.url.substr(0, this.url.lastIndexOf("/"))]; 
    var list = parent ? parent.next("ul") : rootList; 
    if (!list.length) { 
     list = $("<ul>").insertAfter(parent); 
    } 
    var item = $("<li>").appendTo(list); 
    $("<a>").attr("href", this.url).text(this.name).appendTo(item); 
    elements[this.url] = item; 
}); 

http://jsfiddle.net/gilly3/CJKgp/

2

हालांकि मुझे gilly3 की स्क्रिप्ट पसंद है, लेकिन स्क्रिप्ट अलग-अलग el के साथ सूची बनाती है मूल रूप से पूछे जाने वाले <li> और <ul> की ईमेंट घोंसले। तो


    <li><a href="/store">Store</a> 
    <ul> 
     <li><a href="/store/travel">Travel</a></li> 
     ... 
    </ul> 
    </li> 
के बजाय यह

    <li><a href="/store">Store</a> 
    </li> 
    <ul> 
     <li><a href="/store/travel">Travel</a></li> 
     ... 
    </ul>
उत्पन्न करता है यह इस तरह के जेनरेट किए गए मेनू के साथ काम करने वाली यूटिलिटीज या ढांचे के लिए असंगतताओं का कारण बन सकता है और एनीमेशन (उदा। Superfish.js) के साथ इंटरैक्टिव मेनू का उत्पादन कर सकता है। तो मैं 12 लाइनें स्क्रिप्ट

var rootList = $("<ul>").appendTo("body"); 
var elements = {}; 
$.each(menuItems, function() { 
    var parent = elements[this.url.substr(0, this.url.lastIndexOf("/"))]; 
    var list = parent ? parent.children("ul") : rootList; 
    if (!list.length) { 
     list = $("<ul>").appendTo(parent); 
    } 
    var item = $("<li>").appendTo(list); 
    $("<a>").attr("href", this.url).text(this.name).appendTo(item); 
    elements[this.url] = item; 
}); 

http://jsfiddle.net/tomaton/NaU4E/

संबंधित मुद्दे