helbrary/node-item-tree
最新稳定版本:v1.2.3
Composer 安装命令:
composer require helbrary/node-item-tree
包简介
Tree structure library.
README 文档
README
Example: Products in categories
Imagine that nodes are categories and items are products.
$tree->addNode(1, 'All categories', array()); // root // subcategories of category 'All categories' $tree->addNode(2, 'Computer', array(), 1); $tree->addNode(3, 'Wifi', array(), 1); $tree->addNode(4, 'other', array(), 1); $tree->addNode(5, 'Gaming computer', array(), 2); // add products to categories $tree->addItem(2, 1448, 'My super computer', array('price' => 15000)); // 1448 = id of product $tree->addItem(3, 1453, 'My super wifi', array('price' => 800)); // 1453 = id of product $tree->addItem(4, 1350, 'My super printer', array('price' => 800)); // 1350 = id of product //instead of array with information we can put object $product = $db->getProduct(1351); $tree->addItem(4, $product->id, $product->name, $product);
And then we can render tree
foreach ($tree->findRootNodes() as $rootCategories) { foreach ($node->findNodes() as $subCategory) { echo $subCategory->getValue(); // name of category foreach ($subcategory->findItems() as $product) { echo "- " . $product->getValue(); // name of product } } }
Render items of node
$node = $tree->getNode(1); $items = $node->findItems(); // return all items which are direct in node with key 1 $items = $node->findItems(TRUE); // return all items in node 1 and all items in all descendants of node 1
Path to node
$pathToNodes = $tree->getPathToNode(5); foreach ($pathToNodes as $node) { echo '- ' . $node->getValue(); } // output will be: All categories - Computer - Gaming computer // if we do not want render last node 'Gaming computer' then we must set second parameter of method getPathToNode to FALSE $pathToNodes = $tree->getPathToNode(5, FALSE); foreach ($pathToNodes as $node) { echo '- ' . $node->getValue(); } // output will be: All categories - Computer
统计信息
- 总下载量: 7.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-12-06
