在做后台导航时,需要异步加载,pid和id的循环问题,在controller中,建立另外一个方法,嵌套循环,查找是否pid〉1。
public function nav_list()
{
$this->load->database();
/*
测试demo json传值
$query = $this->db->query('select nav_name,nav_desc from nav');
$row = $query->result_array();
$json = json_encode($row);
echo $json;
*/ /**
* 异步加载
*/ $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$result = array();
$query = $this->db->query("select id,nav_name,nav_desc,pid from nav where pid =$id");
foreach($query->result_array() as $row)
{
$node = array();
$node['id'] = $row['id'];
$node['text'] = $row['nav_desc'];
$node['state'] = $this->has_child($row['id']) ? 'closed' : 'open';
array_push($result,$node);
} echo json_encode($result); } public function has_child($id)
{
$this->load->database();
$query = $this->db->query("select pid from nav where pid=$id");
$row = $query->num_rows();
return $row > 1 ? true : false;
}
html代码为:
<!-- 导航 --> <ul id="nav" class="easyui-tree" url="nav_list"> </ul>