This example shows the tree grid feature of jqGrid. Currently jqGrid support only a Nested Set Model.
The Adjacency List Model will be supported soon. All you need is to set treeGrid option to true,
and to specify which column is expandable. Of course the data from the server should be ajusted in appropriate way.
HTML
...
Java Scrpt code
...
jQuery("#treegrid").jqGrid({
url: 'server.php?q=tree',
treedatatype: "xml",
mtype: "POST",
colNames:["id","Account","Acc Num", "Debit", "Credit","Balance"],
colModel:[
{name:'id',index:'id', width:1,hidden:true,key:true},
{name:'name',index:'name', width:180},
{name:'num',index:'acc_num', width:80, align:"center"},
{name:'debit',index:'debit', width:80, align:"right"},
{name:'credit',index:'credit', width:80,align:"right"},
{name:'balance',index:'balance', width:80,align:"right"}
],
height:'auto',
pager : jQuery("#ptreegrid"),
imgpath: gridimgpath,
treeGrid: true,
ExpandColumn : 'name',
caption: "Treegrid example"
});
PHP code
$node = (integer)$_REQUEST["nodeid"];
// detect if here we post the data from allready loaded tree
// we can make here other checks
if( $node >0) {
$n_lft = (integer)$_REQUEST["n_left"];
$n_rgt = (integer)$_REQUEST["n_right"];
$n_lvl = (integer)$_REQUEST["n_level"];
$n_lvl = $n_lvl+1;
$SQL = "SELECT account_id, name, acc_num, debit, credit, balance, level, lft, rgt FROM accounts WHERE lft > ".$n_lft." AND rgt < ".$n_rgt." AND level = ".$n_lvl." ORDER BY lft";
} else {
// initial grid
$SQL = "SELECT account_id, name, acc_num, debit, credit, balance, level, lft, rgt FROM accounts WHERE level=0 ORDER BY lft";
}
$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
header("Content-type: text/xml;charset=utf-8");
}
$et = ">";
echo "";
echo "1";
echo "1";
echo "1";
// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo "";
echo "". $row[account_id]." | ";
echo "". $row[name]." | ";
echo "". $row[acc_num]." | ";
echo "". $row[debit]." | ";
echo "". $row[credit]." | ";
echo "". $row[balance]." | ";
echo "". $row[level]." | ";
echo "". $row[lft]." | ";
echo "". $row[rgt]." | ";
if($row[rgt] == $row[lft]+1) $leaf = 'true';else $leaf='false';
echo "".$leaf." | ";
echo "false | ";
echo "
";
}
echo "";