jeffy2009520 2012-03-06 13:14:08 4498次浏览 15条回复 0 0 0
<?php
class SHtml extends CHtml
{
	/**
	 * 
	 * @param unknown_type $models      array   数据
	 * @param unknown_type $condition	array   条件字段  valueField=>id,textField=>name,parentValue=>pid
	 * @param unknown_type $listData		array   中间传输数据
	 * @param unknown_type $num			int     前缀循环次数
	 */
	public static function listTreeData($models,$condition,$listData=array(),$num=0)
	{
		$data = array();
		$num++;
		foreach($models as $model)
		{
			if($num > 0){
				$value=self::value($model,$condition['valueField']);
				$p = '|';
				$n = abs($num)-1;
				$pre = '--';
				for($i = 0;$i < $n;$i++){
					$pre .= $pre;
				}
				$text=$p.$pre.self::value($model,$condition['textField']);
			}else{
				$value=self::value($model,$condition['valueField']);
				$text=self::value($model,$condition['textField']);
			}
			$listData[$value]=$text;
			$parent_id=self::value($model,$condition['parentValue']);
			$val = bumen::model()->findAll(array("select"=>"{$condition['valueField']},{$condition['textField']},{$condition['parentValue']}","condition"=>"{$condition['parentValue']}=".$value));
			if(is_array($val) && count($val) != 0){
				$data = self::listTreeData($val,$condition,$listData,$num);
				foreach($data as $key=>$val){
					$listData[$key]=$val;
				}
			}
		}
		return $listData;
	}
}

调用的时候:

<?php echo $form->labelEx($model,'p_bumen'); ?>
<?php echo $form->dropDownList($model,'p_bumen',SHtml::listTreeData(bumen::model()->findAll(array("select"=>"id,bumen,p_bumen","condition"=>"p_bumen=0")),array('valueField'=>'id','textField'=>'bumen','parentValue'=>'p_bumen')),array(
    'empty' => '- 请选择部门 -',
    'options' => array(
))); ?>
<?php echo $form->error($model,'p_bumen'); ?>

显示效果: [attach]658[/attach] 希望对大家有帮助~

您需要登录后才可以回复。登录 | 立即注册