問題描述
我正在嘗試使用 Suckerfish css 菜單和 Jquery 從表創建菜單.我使用它作為我的參考:Suckerfish menu with ASP.NET 和 JQuery,我讓它使用手動提供的鏈接(很像文章中的那樣).
I'm attempting at creating a menu from a table using the Suckerfish css menu and Jquery. I'm using this as my reference: Suckerfish menu with ASP.NET and JQuery and I have it working with manually supplied links (much like in the article).
我遇到問題的地方是編寫遞歸函數以從數據庫中獲取菜單項并在適當的層次結構中創建新菜單項.我的數據庫表看起來像這樣:
Where I'm having issues is writing the recursive function to get the menu items from the database and create the new menu items in the proper hierarchy. My database table looks like so:
餐桌菜單
菜單ID家長編號關聯文字
MenuID ParentID Link Text
這個想法是,如果一個項目是父級項目,MenuID 和 ParentID 是相同的,如果它是一個子項,它將在 ParentID 字段中具有它的父項的 MenuID.我需要創建一個函數,該函數可以通過并為父母找到所有孩子(可能是幾個級別)并讓它替換如下手動條目:
The idea being that if an item is a parent-level item the MenuID and ParentID are the same, if it's a child it will have the MenuID of it's parent in the ParentID field. I'm needing to create a function that can go through and find all of the children for the parents (could be a few levels) and have it replace manual entries like this:
Dim Foo As New MenuItem("#", "Foo", Me)
Items.Add(Foo)
Foo.Items.Add(New MenuItem("#", "1", Me))
Foo.Items.Add(New MenuItem("#", "2", Me))
Foo.Items.Add(New MenuItem("#", "3", Me))
Foo.Items.Add(New MenuItem("#", "4", Me))
如有必要,我愿意更改數據庫表結構,并且基本上會做任何其他事情來實現這一目標.
I'm open to changing the database table structure if necessary and basically doing anything else to get this going.
感謝您的任何意見,非常感謝.
Thanks for any input, it's much appreciated.
推薦答案
這種表示分層數據的方法對于人類來說很容易理解,但很難從中提取數據,因為它需要遞歸來提取完整??的層次結構.某些 SQL 版本具有可以為您執行此操作的命令,但這就是幕后發生的事情.
That method of representing hierarchical data is easy to understand for humans but difficult to extract data from, because it requires recursion to extract the full hierarchy. Some flavors of SQL have commands that will do this for you, but that is what is going on behind the scenes.
我建議你閱讀更多的樹和;SQL 中的層次結構,并使用它解釋的具體化路徑方法重構您的架構.它很容易查詢并且擴展性非常好.
I suggest you read More Trees & Hierarchies in SQL, and restructure your schema using the materialized path method that it explains. It is easy to query against and scales really well.
這篇關于使用 ASP.NET、JQuery 和 Suckerfish 構建數據庫驅動的菜單的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!