使用 zend 框架显示树 dijit

Posted

技术标签:

【中文标题】使用 zend 框架显示树 dijit【英文标题】:Display a tree dijit using zend framework 【发布时间】:2010-04-02 12:09:12 【问题描述】:

我正在尝试通过使用带有 zend 框架的 dijit 来显示类别和子类别的树。一直找不到好的例子。这就是我所拥有的:

基本上我得到了以下代码作为我的操作:

class SubcategoriesController extends Zend_Controller_Action
     .....
public function loadtreeAction()


Zend_Dojo::enableView($this->view);
Zend_Layout::getMvcInstance()->disableLayout();

//Creating a sample tree of categories and subcategories
$a["cat1"]["id"] = "id1";
$a["cat1"]["name"] = "Category1";
$a["cat1"]["type"] = "category";

$subcat1 = array("id" => "Subcat1","name" => "Subcategory1" , "type" => "subcategory");
$subcat2 = array("id" => "Subcat12","name" => "Subcategory12" , "type" => "subcategory");
$a["cat1"]["children"] = array($subcat1,$subcat2);

$treeObj = new Zend_Dojo_Data('id', $a);
$treeObj->setLabel('name');
$this->view->tree =  $treeObj->toJson();   

    ....
 

在我看来:

<?php
    $this->dojo()->requireModule('dojo.data.ItemFileReadStore');
    $this->dojo()->requireModule('dijit.Tree');
    $this->dojo()->requireModule('dojo.parser');
?>

<div dojoType="dojo.data.ItemFileReadStore"  url="/Subcategories/loadtree" jsId="store"></div>
<div dojoType="dijit.tree.ForestStoreModel" jsId="treeModel" store="store" rootId="root" rootLabel="List of Categories" childrenAttrs="children" query="type:'category'"></div>
<div dojoType="dijit.Tree" model="treeModel" labelAttrs="ListOfCategories"></div>

它甚至似乎根本没有尝试加载树。

感谢任何帮助

【问题讨论】:

我不是 100% 确定,所以这是一个评论,以免一些纳粹投票反对我...您可能需要在您的视图/布局中添加类似的内容 $this->dojo() ->setLocalPath($this->baseUrl().'/scripts/dojo/dojo.js')->addStyleSheetModule('dijit.themes.tundra'); $this->dojo()->setDjConfig('parseOnLoad', 'true'); $this->dojo()->启用(); echo $this->dojo(); 【参考方案1】:

首先您必须创建单独的操作。一个用于显示,另一个用于加载:

public function indexAction()

    Zend_Dojo::enableView($this->view);
    $this->view->dojo()->setDjConfigOption('parseOnLoad', true);

public function loadtreeAction()

    //Creating a sample tree of categories and subcategories
    $a["cat1"]["id"] = "id1";
    $a["cat1"]["name"] = "Category1";
    $a["cat1"]["type"] = "category";

    $subcat1 = array("id" => "Subcat1","name" => "Subcategory1" , "type" => "subcategory");
    $subcat2 = array("id" => "Subcat12","name" => "Subcategory12" , "type" => "subcategory");
    $a["cat1"]["children"] = array($subcat1,$subcat2);

    $treeObj = new Zend_Dojo_Data('id', $a);
    $treeObj->setLabel('ListOfCategories');
    $this->view->tree =  $treeObj->toJson();

而且你不能忘记

setDjConfigOption('parseOnLoad', true)

在你的views/scripts/index.phtml中(如果你使用布局,一些部分必须放在里面,我让你这样做):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<?php 
$this->dojo()->enable();
$this->dojo()->requireModule('dojo.data.ItemFileReadStore');
$this->dojo()->requireModule('dijit.Tree');
$this->dojo()->requireModule('dojo.parser');
echo $this->dojo();
echo $this->dojo()->addStylesheetModule('dijit.themes.tundra');
?>
</head>
<body class="tundra">
<div id="content">
<div dojoType="dojo.data.ItemFileReadStore"  url="http://194.79.142.38:1080/rbplm/public/home/index/loadtree" jsId="store"></div>
<div dojoType="dijit.tree.ForestStoreModel" jsId="treeModel" store="store" rootId="root" rootLabel="List of Categories" childrenAttrs="children" query="type:'category'"></div>
<div dojoType="dijit.Tree" model="treeModel" labelAttrs="ListOfCategories"></div>   
</div>
</body>
</html>

而在 loadtree.phtmln 中:

     <?php echo $this->tree ?>

【讨论】:

以上是关于使用 zend 框架显示树 dijit的主要内容,如果未能解决你的问题,请参考以下文章

zend 框架显示/处理表单使用相同的动作/视图?

如何使用zend框架2在更改下拉值时在输入字段内显示值

如何使用 Struts2 框架向 Dojo 树提供 JSON 数据

利用 Dijit 组件框架打造丰富的用户界面

有没有办法让文件上传输入匹配 Zend Framework 中的 dijit 主题?

Zend 框架中的错误报告