如果你需要更好看的無限分類,請查看
http://www.cnblogs.com/zx-admin/p/8021734.html
//新聞類別管理 public function new_classify() { $arr = M('news_classify')->where("fid = 0")->select(); $html = '<ul class="easyui-tree"><li><span>新聞中心</span><ul>'; if (!empty($arr)) { $html .= $this->_tree($arr); } $html .= '</ul></li></ul>'; $this->assign("html", $html); $this->display(); } //遞歸樹 public function _tree($arr) { foreach ($arr as $key => $v) { $fid1 = $v['id']; //查詢是否有子類 $res = M('news_classify')->where("fid = $fid1")->select(); if (!empty($res)) { $html .= '<li '; if (!empty($res)) { $html .= 'data-options="state:\'closed\'"'; } $html .= '> <span> ' . $v["new_classify_name"] . ' <a id="new_classify_edit" rel=' . $v["id"] . '">編輯</a> <a id="new_classify_del" rel=' . $v["id"] . '>刪除</a></span><ul>'; $html .= $this->_tree($res); $html .= '</ul></li>'; } else { $html .= '<li '; if (!empty($res)) { $html .= 'data-options="state:\'closed\'"'; } $html .= '> <span> ' . $v["new_classify_name"] . ' <a id="new_classify_edit" rel=' . $v["id"] . '">編輯</a> <a id="new_classify_del" rel=' . $v["id"] . '>刪除</a></span><ul>'; // $html .= $this->_tree($res); //為空就不需要繼續遍歷 $html .= '</ul></li>'; } } return $html; }
表結構就這樣了,還需要的一個結構的下拉框的結構
function _tree($arr, $fid = 0, $level = 0) { static $tree = array(); #用於保存重組的結果,注意使用靜態變量 foreach ($arr as $v) { if ($v['fid'] == $fid) { //說明找到了以$pid為父節點的子節點,將其保存 $v['level'] = $level; $tree[] = $v; //然后以當前節點為父節點,繼續找其后代節點 _tree($arr, $v['id'], $level + 1); } } return $tree; }
調用返回結果,這段代碼隨便拔下來的,需要改一下才能看到結果
<?php foreach ($classify as $value) { ?> <form action="<?php echo site_url('admin/classify_update');?>" class="jqtransform" method="post"> <table class="form_table" width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td class="td_center">第<?php echo ($value['level']+1);?>級分類:</td> <td class=""> <input type="hidden" name="id" value="<?php echo $value['id'];?>" /> <?php echo str_repeat("+++++++", $value['level'])?> <input type="text" name="title" class="input-text lh30" size="15" value="<?php echo $value['title'];?>"> <input type="text" name="url" class="input-text lh30" size="15" value="<?php echo $value['url'];?>"> <input type="submit" value="更新" class="ext_btn ext_btn_submit" onclick="location.href='<?php echo base_url('')."index.php/admin/classify_update/".$value['id'];?>'"> <input type="button" class="ext_btn ext_btn_error" value="刪除" onclick="location.href='<?php echo base_url('')."index.php/admin/classify_del/".$value['id'];?>'"> </td> </tr> </table> </form> <?php } ?>
html
http://www.jeasyui.com/demo/main/index.php?plugin=Tree&theme=gray&dir=ltr&pitem=
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Basic Tree - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css" href="easyui.css">
<link rel="stylesheet" type="text/css" href="icon.css">
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.easyui.min.js"></script>
</head>
<body>
<h2>Basic Tree</h2>
<p>Click the arrow on the left to expand or collapse nodes.</p>
<div style="margin:20px 0;"></div>
<div class="easyui-panel" style="padding:5px">
<ul class="easyui-tree">
<li>
<span>My Documents</span>
<ul>
<li data-options="state:'closed'">
<span>Photos <a href="http://www.baidu.com">編輯</a> <a href="http://www.baidu.com">刪除</a></span>
<ul>
<li>
<span>Friend22222 <a href="http://www.baidu.com">編1111111111111輯</a> </span>
</li>
<li>
<span>Wife</span>
</li>
<li>
<span>Company</span>
</li>
</ul>
</li>
<li>
<span>Program Files</span>
<ul>
<li>Intel</li>
<li>Java</li>
<li>Microsoft Office</li>
<li>Games</li>
</ul>
</li>
<li>index.html</li>
<li>about.html</li>
<li>welcome.html</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
