jEasyUI建立非同步樹狀選單
為了建立非同步的樹狀選單(Tree),每個樹節點必須要有一個 'id' 屬性,這個會提交回伺服器去檢索子節點資料。
建立樹形選單(Tree)
<ul id="tt" class="easyui-tree" url="tree2_getdata.php"> </ul>
伺服器端程式碼
$id = isset($_POST['id']) ? intval($_POST['id']) : 0; include 'conn.php'; $result = array(); $rs = mysql_query("select * from nodes where parentId=$id"); while($row = mysql_fetch_array($rs)){ $node = array(); $node['id'] = $row['id']; $node['text'] = $row['name']; $node['state'] = has_child($row['id']) ? 'closed' : 'open'; array_push($result,$node); } echo json_encode($result); function has_child($id){ $rs = mysql_query("select count(*) from nodes where parentId=$id"); $row = mysql_fetch_array($rs); return $row[0] > 0 ? true : false; }