下面是代码,首先加载php文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link rel="stylesheet" type="text/css" href="resources/css/ext-all.css"/>
    <!-- GC -->
    <!-- LIBS -->
    <script type="text/javascript" src="adapter/ext/ext-base.js"></script>    <!-- ENDLIBS -->    <script type="text/javascript" src="ext-all.js"></script>
<script type="text/javascript">
Ext.onReady(function(){
var tree =new Ext.tree.TreePanel({
renderTo:'mytree',
loader:new Ext.tree.TreeLoader({dataUrl : "http://localhost/ext/treedata.php"}),
root:new Ext.tree.AsyncTreeNode({
id:'0',
text:'我是根节点'
})
});
});
</script>
<title>人事管理系统</title>
</head><body>
<div id="mytree" style="width:300px; height:500px"></div>
</body>
</html>
然后是php的输出json
<?php
/*
*/
$conn=mysql_connect('localhost','root','sanzhixiong') or die('数据库链接失败');
mysql_select_db('lianxi') or die('数据库选择失败');
$node=$_POST['node'];
if($node=="0")
{
$sql="SELECT * FROM tree WHERE p_id='0'";
$query=mysql_query($sql) or die('sql 执行错误');
while($row=mysql_fetch_array($query))
{
$dom[]=array('id'=>$row['id'],'text'=>$row['text']);
}
echo json_encode($dom);
}
else if($node=="1")
{
$sql="SELECT * FROM tree WHERE p_id='1'";
$query=mysql_query($sql) or die('sql 执行错误');
while($row=mysql_fetch_array($query))
{
$dom[]=array('id'=>$row['id'],'text'=>$row['text']);
}
echo json_encode($dom);
}
else
{
$sql="SELECT * FROM tree WHERE p_id='$node'";
$query=mysql_query($sql) or die('sql 执行错误');
while($row=mysql_fetch_array($query))
{
$dom[]=array('id'=>$row['id'],'text'=>$row['text'],'leaf'=>'true');
}
echo json_encode($dom);
}
?>
我这个只能分三级而且我觉得好像没做一级要写一个判断很麻烦,有没简单点的办法