CTreeView的URL带参数没有起作用

描述的比较长请看下去,第一种情况生成的html代码和第二种不一致的,说明参数无效。

list.php这个View嵌入了CTreeView的代码:

<?php

$this->widget('CTreeView',array('url'=>array('listTreeData','root'=>$root),'animated'=>'fast','htmlOptions'=>array('class'=>'treeview-red')));

?>

运行的TypeController的代码如下:

	public function actionList(){
	if(isset($_GET&#91;&#039;root&#039;&#93;)){


		$root=$_GET&#91;&#039;root&#039;&#93;;


	}else{


		$root=&#039;source&#039;;


	}


	$this-&gt;renderPartial(&#039;list&#039;,array(&#039;root&#039;=&gt;$root,),array(),true);


}code&#93;

生成的list.php生成的html如下:[code]<ul class="treeview-red" id="yw0">

</ul><script type="text/javascript">

/<![CDATA[/

jQuery(document).ready(function() {

jQuery("#yw0").treeview({'url':'/slp_oa/index.php?r=type/listTreeData&root=9','animated':'fast'});

});

/]]>/

</script>

第一种情况,运行http://localhost/slp_oa/?r=type/list&root=9生成如下html

生成如下代码,已经过滤了html代码:

Quote

地区TypeArea1是添子类 修改 移动 删除

文件分类TypeArticle1是添子类 修改 移动 删除

公文分类TypeDocument1是添子类 修改 移动 删除

文件分类TypeFile1是添子类 修改 移动 删除

档案密级分类TypeFileSecret1是添子类 修改 移动 删除

文件紧急程度TypeFileUrgency1是添子类 修改 移动 删除

用工类型TypeLabor1是添子类 修改 移动 删除

在listTreeData中的代码:

	public function actionListTreeData(){


		if(!isset($_GET['root'])){


			$parentId='source';


		}else{


			$parentId=$_GET['root'];


		}


		$dataTree=array();


		$userList=array();


		if($parentId=='source'){


			$typeList=Type::model()->with('children')->findAll('parent_id is null');


		}else{


			$typeList=Type::model()->with('children')->findAll('parent_id ='.$parentId);


		}


		if(is_array($typeList)){


			foreach($typeList as $type){


				$dataTree[]=$this->formatData($type);


			}


		}


		echo CTreeView::saveDataAsJson($dataTree);


	}


	protected function formatData($type){


		$text='<div class="x-column-tree">'.


		'<div class="x-tree-col" style="width:200px;">'.$type['name'].'</div>'.


		'<div class="x-tree-col" style="width:130px;">'.$type['code'].'</div>'.


		'<div class="x-tree-col" style="width:30px;">'.$type['priority'].'</div>'.


		'<div class="x-tree-col" style="width:60px;">'.($type['leaf']==true?'是':'否').'</div>'.


		'<div class="x-tree-col" style="width:120px;"><a href="?r=type/create">添子类</a>';


		if($type['remove']){


			$text=$text.'&nbsp;<a href="?r=type/update">修改</a>';


			$text=$text.'&nbsp;<a href="?r=type/move">移动</a>';


			$text=$text.'&nbsp;<a href="?r=type/delete">删除</a>';


		}


		$text=$text.'</div></div>';


		return array('text'=>$text,'id'=>$type['id'],'hasChildren'=>false);


	}

第二种情况直接运行jruery里面的url:http://localhost/slp_oa/index.php?r=type/listTreeData&root=9生成如下html:

Quote

[{"text":"

\u4e2a\u4eba\u6d88\u606f</div>

TypeMessage</div>

1</div>

\u662f</div>

\u6dfb\u5b50\u7c7b</a> \u4fee\u6539</a> \u79fb\u52a8</a> \u5220\u9664</a></div></div>","id":"15","hasChildren":false},{"text":"

\u5de5\u4f5c\u6d41</div>

TypeMessage</div>

1</div>

\u662f</div>

\u6dfb\u5b50\u7c7b</a> \u4fee\u6539</a> \u79fb\u52a8</a> \u5220\u9664</a></div></div>","id":"59","hasChildren":false},{"text":"

\u5185\u90e8\u901a\u77e5</div>

TypeMessage</div>

1</div>

\u662f</div>

\u6dfb\u5b50\u7c7b</a> \u4fee\u6539</a> \u79fb\u52a8</a> \u5220\u9664</a></div></div>","id":"72","hasChildren":false},{"text":"

\u516c\u5171\u901a\u544a</div>

TypeMessage</div>

1</div>

\u662f</div>

\u6dfb\u5b50\u7c7b</a> \u4fee\u6539</a> \u79fb\u52a8</a> \u5220\u9664</a></div></div>","id":"73","hasChildren":false}]

可以从TypeMessage这个字眼看出代码不一致。不知道原因出在哪里?

大概知道情况是如果用嵌入jquery的方式的话,参数无效。直接调用url参数有效。

你是指第一种方式里的GET参数无效,而第二种则有效?

你能对比一下两种方式里的javascript代码有什么区别么?

文字太多可能写的有点难理解。我是这样来判断的。

先测试返回的树是否想要的,因为嵌入了javascript代码看不到生成的数据,只能看页面表现。发现不是我想要的数据,因为他没有根据参数root=9去数据库找,而是当作没有传递root的结果。

第二种测试是用CTreeView生成的jquery的url来访问。发现这个是正确的json代码。说明参数起作用了。

第一种无效,第二种有效。

这个比较奇怪,你能打开firebug看看第一种方式下ajax实际提交的URL是什么吗?

发现多了一个参数 &root=source。

我在找怎样让他开始的root是我提供的某个id。

搞定了。谢谢qiang

能解释一下怎么搞定的么?

我的方法不是屏蔽&root=source这个参数,而是换个名字来传递。

我换了一个参数parentId来传递初始root。在取树的数据的时候进行判断,如果root的值是source,则把parentId赋值给它。