我试图在codeigniter中使用jstree和php以及mysql创建一个可扩展的树结构。我指的是这样做。但是我无法得到树的结构。页面显示为空白。我在代码中找不出问题所在。
这是我的密码:
表结构
CREATE TABLE `tab_family` (
`family_id` int(11) NOT NULL,
`family_name` varchar(250) NOT NULL,
`parent_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `tab_family`
--
INSERT INTO `tab_family` (`family_id`, `family_name`, `parent_id`) VALUES
(1, 'Pullippadavil', 0),
(2, 'Theveril (II)', 1),
(3, 'Valya Puthen Purayil (II)', 1),
(4, 'Pullippadavil (II)', 1),
(5, 'Ottavelil ( III )', 2),
(6, 'Theveril - Oommen Kora ( III )', 2);
控制器功能
public function getfamilytree()
{
$result = $this->Family_model->getFamilyTreeData();
if(!empty($result))
{
foreach($result as $res)
{
$data[] = array(
'id' => $res->family_id,
'parent_id' => $res->family_id,
'text' => $res->family_name,
);
}
$itemsByReference = array();
// Build array of item references:
foreach($data as $key => &$item) {
$itemsByReference[$item['id']] = &$item;
// Children array:
$itemsByReference[$item['id']]['children'] = array();
// Empty data class (so that json_encode adds "data: {}" )
$itemsByReference[$item['id']]['data'] = new StdClass();
}
// Set items as children of the relevant parent item.
foreach($data as $key => &$item)
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
$itemsByReference [$item['parent_id']]['children'][] = &$item;
// Remove items that were added to parents elsewhere:
foreach($data as $key => &$item) {
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
unset($data[$key]);
}
// Encode:
echo json_encode($data);
}
}
看法
<section class="content">
<!-- Default box -->
<div class="box">
<div class="box-body">
<div id="html" class="demo"></div>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<script>
var url="<?php echo base_url();?>";
$(document).ready(function () {
$('#html').jstree({
"state" : { "key" : "demo" },
"check_callback" : true,
'core' : {
'data' : {
"url" : url+"Applications/getfamilytree",
"dataType" : "json" // needed only if you do not supply JSON headers
}
}
});
})
</script>
型号:
public function getFamilyTreeData()
{
$query = $this->db->get('tab_family');
return $query->result();
}
1条答案
按热度按时间ztigrdn81#
问题是,父id不正确
更改为
现在树出现了