子项的Jquery树网格(jqgrid)xml结构

时间:2009-04-29 09:07:43

标签: jquery treegrid jqgrid

任何人都可以告诉我jqgrid使用的子项的XML结构。我已经阅读了文档,并没有清楚地解释如何创建包含子项的树视图。基本上我有一个3列的2列网格。

1 个答案:

答案 0 :(得分:2)

从演示中: http://www.trirand.com/jqgrid35/jqgrid.html

(在版本3.3->树格的新内容中)

它将此ajax调用作为帖子: http://www.trirand.com/jqgrid35/server.php?q=tree

帖子参数如下:

_search: false
n_left: 1
n_level: 0
n_right: 8
nd: 1241000465087
nodeid: 1
page: 1
rows: 20
sidx    
sord: asc

这恰好返回:

<?xml version='1.0' encoding='utf-8'?>
<rows>
<page>1</page>
<total>1</total>
<records>1</records>
<row><cell>1</cell><cell>Cash</cell><cell>100</cell><cell>400.00</cell<cell>250.00</cell><cell>150.00</cell><cell>0</cell><cell>1</cell><cell>8</cell><cell>false</cell><cell>false</cell></row>
<row><cell>5</cell><cell>Bank's</cell><cell>200</cell><cell>1500.00</cell><cell>1000.00</cell><cell>500.00</cell><cell>0</cell><cell>9</cell><cell>14</cell><cell>false</cell><cell>false</cell></row>
<row><cell>8</cell><cell>Fixed asset</cell><cell>300</cell><cell>0.00</cell<cell>1000.00</cell><cell>-1000.00</cell><cell>0</cell><cell>15</cell><cell>16</cell><cell>true</cell><cell>false</cell></row>
</rows>

对于每个子行,它进行另一个ajax调用。第一个“cell”元素指定行号。当在下一行id之前存在间隙时,它知道它有子项,并且将为该行放置一个扩展器。当用户选择扩展器时,它会进行另一次ajax调用,并返回以下内容:

<?xml version='1.0' encoding='utf-8'?>
<rows>
<page>1</page>
<total>1</total>
<records>1</records>
<row><cell>2</cell><cell>Cash 1</cell><cell>1</cell><cell>300.00</cell><cell>200.00</cell><cell>100.00</cell><cell>1</cell><cell>2</cell><cell>5</cell><cell>false</cell><cell>false</cell></row>
<row><cell>4</cell><cell>Cash 2</cell><cell>2</cell><cell>100.00</cell><cell>50.00</cell><cell>50.00</cell><cell>1</cell><cell>6</cell><cell>7</cell><cell>true</cell><cell>false</cell></row>
</rows>

查看源代码,它指定列名称,并期望按顺序返回它们。我很确定你可以写这个不期望特定的顺序,但这就是映射的设置方式。这几乎是ext做事的方式的克隆,这是一个很好的方式,因为他们做得很好。

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源代码示例(经历了这么多麻烦,不妨完成它!):

$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 "<?xml version='1.0' encoding='utf-8'?$et\n";
   echo "<rows>";
echo "<page>1</page>";
echo "<total>1</total>";
echo "<records>1</records>";
// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    echo "<row>";           
    echo "<cell>". $row[account_id]."</cell>";
    echo "<cell>". $row[name]."</cell>";
    echo "<cell>". $row[acc_num]."</cell>";
    echo "<cell>". $row[debit]."</cell>";
    echo "<cell>". $row[credit]."</cell>";
    echo "<cell>". $row[balance]."</cell>";
    echo "<cell>". $row[level]."</cell>";
    echo "<cell>". $row[lft]."</cell>";
    echo "<cell>". $row[rgt]."</cell>";
    if($row[rgt] == $row[lft]+1) $leaf = 'true';else $leaf='false';
    echo "<cell>".$leaf."</cell>";
    echo "<cell>false</cell>";
    echo "</row>";
}
echo "</rows>";