問題描述
我有一個類別表.每個類別可以有一個可選的父級(如果沒有父級,則默認(rèn)為 0).
I've got a categories table. Each category can have an optional parent (Defaults to 0 if no parent).
我想要做的是用類別的級別構(gòu)建一個簡單的 html 列表樹.
What I want to do is build a simple html list tree with the levels of the categories.
示例日期:
Foods
-- Fruit
---- Apple
---- Banana
---- Orange
-- Veg
---- Cucumber
---- Lettuce
Drinks
-- Alcoholic
---- Beer
---- Vodka
Misc
-- Household Objects
---- Kitchen
------ Electrical
-------- Cooking
---------- Stove
---------- Toaster
---------- Microwave
請注意,這需要在大約 10 個級別"內(nèi)工作.我希望它是無限的,但我真的不想走使用嵌套集模型的路線,因?yàn)樗鼤?dǎo)致這個項(xiàng)目的巨大延遲.
Note that this needs to work for around 10 'levels'. I'd love it to be infinite but I really dont want to be going down the route of using a nested set model as it'll cause huge delays on this project.
關(guān)于 laravel 的文檔很糟糕,甚至沒有真正提到從哪里開始.我已經(jīng)玩了好幾天了,試圖弄清楚該做什么,但如果沒有一個巨大的 for each 循環(huán)塊 10 次,我似乎一無所獲.
The docs on this for laravel are terrible, with no real reference as to where to even start. I've been playing with it for days trying to work out what to do and seem to be getting nowhere without a huge messy block of for each loops within each other 10 times.
我在我的模型中使用了以下數(shù)據(jù)樹:
I've got my tree of data using the following in my model:
<?php
class Item extends Eloquent {
public function parent()
{
return $this->hasOne('Item', 'id', 'parent_id');
}
public function children()
{
return $this->hasMany('Item', 'parent_id', 'id');
}
public function tree()
{
return static::with(implode('.', array_fill(0,10, 'children')))->where('parent_id', '=', '0')->get();
}
}
這會將所有父級和子級提升到 10 級.這很好用,但如果不手動在彼此內(nèi)部使用 10 個 foreach 循環(huán),您就無法真正對子數(shù)據(jù)執(zhí)行任何操作.
This gets all the parent and children up to a level of 10. This works fine, but you cant really then do anything with the child data without manually having 10 foreach loops within each other.
我在這里做錯了什么?當(dāng)然,這不應(yīng)該如此艱難/執(zhí)行不力嗎?我想要做的就是獲得一個簡單的 html 列表,其中包含樹結(jié)構(gòu)中的項(xiàng)目.
What am I doing wrong here? Surely this shouldn't be this hard/poorly executed? All I want do do is get a simple html list with the items in a tree structure.
我已經(jīng)整理了一個上面使用的虛擬數(shù)據(jù)的快速 SQLFiddle 示例:http://sqlfiddle.com/#!2/e6d18/1
I've put together a quick SQLFiddle example of the dummy data used above: http://sqlfiddle.com/#!2/e6d18/1
推薦答案
這比我平時早上的填字游戲有趣多了.:)
This was much more fun than my usual morning crossword puzzle. :)
這是一個 ItemsHelper 類,它將執(zhí)行您正在尋找的操作,并且更好地遞歸到您想要的范圍.
Here is an ItemsHelper class that will do what you are looking for, and better yet will recurse as far down as you want.
app/models/ItemsHelper.php:
<?php
class ItemsHelper {
private $items;
public function __construct($items) {
$this->items = $items;
}
public function htmlList() {
return $this->htmlFromArray($this->itemArray());
}
private function itemArray() {
$result = array();
foreach($this->items as $item) {
if ($item->parent_id == 0) {
$result[$item->name] = $this->itemWithChildren($item);
}
}
return $result;
}
private function childrenOf($item) {
$result = array();
foreach($this->items as $i) {
if ($i->parent_id == $item->id) {
$result[] = $i;
}
}
return $result;
}
private function itemWithChildren($item) {
$result = array();
$children = $this->childrenOf($item);
foreach ($children as $child) {
$result[$child->name] = $this->itemWithChildren($child);
}
return $result;
}
private function htmlFromArray($array) {
$html = '';
foreach($array as $k=>$v) {
$html .= "<ul>";
$html .= "<li>".$k."</li>";
if(count($v) > 0) {
$html .= $this->htmlFromArray($v);
}
$html .= "</ul>";
}
return $html;
}
}
我剛剛使用了新安裝的 Laravel 4 和基本的 hello.php
視圖.
I just used a new installation of Laravel 4 and the basic hello.php
view.
這是我在 app/routes.php
中的路線:
Here is my route in app/routes.php
:
Route::get('/', function()
{
$items = Item::all();
$itemsHelper = new ItemsHelper($items);
return View::make('hello',compact('items','itemsHelper'));
});
雖然我的視圖沒有使用 items 變量,但我在這里傳遞它是因?yàn)槟憧赡芤蚕胗盟鼈冏銎渌虑?
Although my view doesn't use the items variable, I'm passing it here because you probably will want to do something else with them too.
最后,我的 app/views/hello.php
只有一行:
And finally, my app/views/hello.php
just has one line:
= $itemsHelper->htmlList();?>
輸出如下:
- 食物
- 水果
- 蘋果
- 香蕉
- 橙色
- 蔬菜
- 黃瓜
- 生菜
- 飲料
- 酒精
- 啤酒
- 伏特加
- 雜項(xiàng)
- 家居用品
- 廚房
- 電器
- 烹飪
- 爐灶
- 烤面包機(jī)
- 微波爐
注意:您的 SQL Fiddle 有 5(Orange")作為 Cucumber 和 Lettuce 的 parent_id,我不得不將其更改為 6(Veg").
Note: your SQL Fiddle had 5 ("Orange") as the parent_id for Cucumber and Lettuce, I had to change it to 6 ("Veg").
這篇關(guān)于Laravel 4 - 雄辯.無限的孩子??變成可用的數(shù)組?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!