JSTree:使用php和mysql创建可扩展树结构

时间:2018-12-08 10:46:20

标签: php mysql ajax codeigniter jstree

我试图在Codeigniter中使用jstree和php和mysql创建可扩展树结构。我指的是this。但是我无法获得树形结构。页面显示为空白。我无法在代码中找出问题。

这是我的代码:

表结构

    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 个答案:

答案 0 :(得分:0)

问题是,父母ID不正确

[Service(IsolatedProcess = true, enabled = true)]
public class TrackService : Service
{
    FusedLocationProviderClient fusedLocationProviderClient;

    public override void OnCreate()
    {
        base.OnCreate(); //Break point set here
    }

    public override StartCommandResult OnStartCommand(Android.Content.Intent
                 intent, StartCommandFlags flags, int startId)
    {
        fusedLocationProviderClient = 
            LocationServices
            .GetFusedLocationProviderClient(this); //Break point set here
        return StartCommandResult.Sticky;
    }

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }
}

更改为

 $data[] = array(
'id' => $res->family_id,
'parent_id' => $res->family_id, // this was the problem
'text' => $res->family_name,
);

现在树出现了