Welcome Guest, Not a member yet? Register   Sign In
jsTree jQuery plugin vs Codeigniter (nodes with children not showing up)
#1

Hey Guys,

I am using jsTree jQuery plugin in a project, I am getting the data correctly to the view, but clicking to the nodes that have children doesn't do anything. Does anybody know what may be wrong? 
[Image: image.png]
Code:
Controller:
<div class="container  well" id="tree">
   <?php
     echo '<ul>';
       $offset = '';
       foreach($users as $user) :
         if($user['department'] != $offset) {
           echo '<li>' . $user['department'] . '</li>';
           $offset = $user['department'];
         }
         echo '<ul><li>' . $user['fullname'] . '</li></ul>';
       endforeach;
     echo '</ul>';
   ?>
</div>

JS File:
$("#tree").jstree();

Thank you very much!
Reply
#2

(This post was last modified: 12-30-2017, 12:36 PM by PaulD.)

From the page (https://www.jstree.com/) the HTML should look like this:
Code:
<ul>
      <li>Root node 1
        <ul>
          <li id="child_node_1">Child node 1</li>
          <li>Child node 2</li>
        </ul>
      </li>
      <li>Root node 2</li>
    </ul>

But you are closing your root node immediately.
PHP Code:
echo '<li>' $user['department'] . '</li>'

So the js cannot find the sub <ul> with your users in it as it is not enclosed in the parent <li>

Hope that makes sense,

Paul.
Reply
#3

Many thanks, Paul!

Fixed!
Reply
#4

(This post was last modified: 12-31-2017, 11:51 PM by XtreemDeveloper.)

You can use this code.

HTML Code:
<ul>
    <li>Business
        <ul>
            <li>Leo Rodriguez</li>
        </ul>
    </li>
    <li>Computer Science
        <ul>
            <li>Harry Jones</li>
            <li>Sabi</li>
        </ul>
    </li>
    </li>
    <li>IT
        <ul>
            <li>Tim Ben</li>
        </ul>
    </li>
    <li>Marketing
        <ul>
            <li>John Doe</li>
        </ul>
    </li>
</ul>

PHP Code:

<ul>
  <?php foreach ($users as $key => $value) { ?>
      <?php if($users[$key]['department'] != $users[$key-1]['department']) { ?>
           <li><?php echo $users[$key]['department']; ?></li>
           <ul>
             <li><?php echo $users[$key]['fullname']; ?></li>
           </ul>
      <?php } else { ?>
 
    <ul>  
       <li>Sabi</li>
     </ul>
  <?php } ?>
  <?php } ?>
</ul>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB