TreeLiHelper
Helper 用于在 cakephp 2.1 中使用 tree->generateTreeList() 生成的无子节点的节点上创建带有链接的 html 列表。在控制器中放置
PHP 代码片段
<?php
public function index(){
$this->loadModel('Model');
$this->set('data',$this->Model->generateTreeList());
}
?>
在视图中
PHP 代码片段
<?php
echo '<ul data-role="listview" data-divider-theme="a" data-inset="true">';
echo $this->TreeView->createTree($data,array('controller'=>'Categorias','action'=>'view'));
echo '</ul>';
?>
在 /app/View/Helper/ 文件夹中创建一个名为 TreeLiHelper.php 的文件,内容如下:
PHP 代码片段
<?php
App::uses('AppHelper', 'View/Helper');
class TreeLiHelper extends AppHelper {
public $helpers = array('Html');
public $link;
public function createTree($tree,$link){
$this->link = $link;
$out = '';
$count = 0;
$buffer = null;
foreach($tree as $id => $item){
$depth = strrpos($item, '_');
if ($depth === false) {
$depth = 0;
$clean_item = $item;
} else {
$depth = $depth + 1;
$clean_item = substr($item, strrpos($item, '_')+1);
}
if($buffer != null){
$out .= $this->makeLi($buffer,$depth,$id);
}
$buffer['item'] = $clean_item;
$buffer['depth'] = $depth;
}
$out .=$this->makeLi($buffer,0,$id);
return $out;
}
protected function makeLi($buffer,$depth,$id){
if($buffer['depth']==$depth){
$out = '<li>'.$this->makeLabel($buffer['item'],$id)."</li>\n";
}elseif($buffer['depth']<$depth){
$out = '<li>'.$this->makeLabel($buffer['item'])."\n<ul>\n";
}elseif($buffer['depth']>$depth){
$out = "<li>".$this->makeLabel($buffer['item'],$id)."</li>\n";
$diff = $buffer['depth']-$depth;
for($i=0; $i<$diff;$i++){
$out .= "</ul> \n </li>\n";
}
}
return $out;
}
protected function makeLabel($item, $id = null){
if($id!=null){
$return = $this->Html->link($item, $this->mountLink($id));
}else{
$return = $item;
}
return $return;
}
protected function mountLink($id){
if(is_array($this->link)){
$link = $this->link;
$link[] = $id;
}else{
$link = rtrim($this->link,'/').'/'.$id;
}
return $link;
}
}
?>