EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: kavvson on September 30, 2014, 12:22:00 PM



Title: Node tree php
Post by: kavvson on September 30, 2014, 12:22:00 PM
I use
Code:
SELECT * FROM customers
to get my names

example output

Code:
[{"id": 1,"name": "Marcin","surname": "A"}, {"id": 2,"name": "Piotr","surname": "B"}, {"id": 3,"name": "Kamil","surname": "C"}]

and the customers could have a child [ but doesn't have to ]

My query

Code:
select listy.number,listy.id,listy.created,listy.type from listy where forwho = $id

example ouput

Code:
[{"number": "122232222222222222222222","id": 1,"created": "2014-09-12 14:51:00","type": 1}, {"number": "412333333333333333333333","id": 2,"created": "2014-09-03 15:15:00","type": 1}, {"number": "423233333333333333333333","id": 3,"created": "2014-09-03 16:08:00","type": 1}]

What Iam willing to get is,

[root]
        - [Marcin A]
           = 122232222222222222222222
           = 412333333333333333333333
           = 423233333333333333333333
        - [Piotr B]
           = No entries
[/root]

I was basing on the example on : http://www.jeasyui.com/tutorial/tree/tree2.php but I can not achieve this somehow. All I need is to load data onclick on the [Name] cause the child can have ~100 entries

My php code

Code:
$result = array();
$rs = mysql_query("SELECT name, surname,id FROM customers");
while($row = mysql_fetch_array($rs)){
    $node = array();
    $node['id'] = $row['id'];
    $node['text'] = $row['name'].' '.$row['surname'];
    $node['state'] = has_child($row['id']) ? 'closed' : 'open';
    array_push($result,$node);
}



echo json_encode($result);



function has_child($id){

$rs = mysql_query("select listy.number,listy.id,listy.created,listy.type from listy where forwho = $id");
    $row = mysql_fetch_array($rs);
    return $row[0] > 0 ? true : false;
}

generated :

Code:
[{"id":"1","text":"Marcin B","state":"open"}.........

It doesn't displays in the tree even,

I use

Code:
<ul id="tt" class="easyui-tree"
        url="tree.php">
</ul>

and everything is properly directed and included for sure cause on a static .json file from other examples it shows up. It might be wrong with my code maybe.

Thanks for help


Title: Re: Node tree php
Post by: stworthy on October 01, 2014, 02:48:36 AM
Please refer the code below:
Code:
<ul id="tt" class="easyui-tree" url="test.php">
</ul>
The 'test.php' code:
Code:
<?php

$id 
= isset($_POST['id']) ? intval($_POST['id']) : 0;

include 
'conn.php';

$result = array();
if (
$id 0){
$rs mysql_query("select * from listy where forwho=$id");
while($row mysql_fetch_array($rs)){
$node = array();
$node['id'] = $id.'_'.$row['id'];
$node['text'] = $row['number'];
$result[] = $node;
}
} else {
$rs mysql_query("select name,surname,id from customers");
while($row mysql_fetch_array($rs)){
$node = array();
$node['id'] = $row['id'];
$node['text'] = $row['name'].' '.$row['surname'];
$node['state'] = has_child($row['id']) ? 'closed' 'open';
$result[] = $node;
}
}
echo 
json_encode($result);

function 
has_child($id){
$rs mysql_query("select count(*) from listy where forwho=$id");
$row mysql_fetch_array($rs);
return $row[0] > true false;
}

?>



Title: Re: Node tree php
Post by: kavvson on October 01, 2014, 04:05:29 AM
Well done, does the formater allows to do a <a href="...?=number">number</a>? how can I accomplish it ( only for child ) there is only 1 level of each root


Title: Re: Node tree php
Post by: stworthy on October 01, 2014, 05:54:59 AM
Yes, the 'formatter' function can be used to format the node text. Please refer to the code below:
Code:
<ul id="tt" class="easyui-tree" data-options="
url:'test.php',
formatter:function(node){
if (some condition){
return '<a href=\'...\'>' + node.text + '</a>';
} else {
return node.text;
}
}
"></ul>