yii无限极分类

	/**
	 * 获取菜单Tree
	 *
	 * @return multitype:
	 */
	public function getMenuAllList() {
		$resArr = $this->getTreeChilds ( 0 );
		$arr = array ();
		$arr [0] = "作为一级菜单";
		foreach ( $resArr as $rs ) {
			$id = $rs ['id'];
			$text = $rs ['text'];
			$arr [$id] = $text;
		}
		return $arr;
	    //return $resArr = CHtml::listData ( $arr, 'id', 'text');
	}
	public function getTreeChilds($parentid) {
		$icon = array (
				'├─ ',
				'├─ ',
				'└─ ' 
		);
		$rs = Menu::model ()->findAll ( "parentid=:parentid", array (
				'parentid' => $parentid 
		) );
		$returnArr = array ();
		if (count ( $rs ) > 0) {
			
			for($i = 0; $i < count ( $rs ); $i ++) {
				if ($parentid > 0) {
					

					$strnbsp = str_repeat ( '    ', $rs [$i] ['level'] );
					
					if (count($rs)==1) {
						$strnbsp .= $icon [2];
					}else{
						if ($i == 0) {
							$strnbsp .= $icon [0];
						} elseif ($i == count ( $rs ) - 1) {
							$strnbsp .= $icon [2];
						} else {
							$strnbsp .= $icon [1];
						}
					}
					
					$returnArr [Menu::$ii] ['id'] = $rs [$i] ['id'];
					$returnArr [Menu::$ii] ['text'] = $strnbsp. $rs [$i] ['text'];
				} else {
					$returnArr [Menu::$ii] ['id'] = $rs [$i] ['id'];
					$returnArr [Menu::$ii] ['text'] = $rs [$i] ['text'];
				}
				$childArr = $this->getTreeChilds ( $rs [$i] ['id'] );
				if (count ( $childArr ) > 0) {
					foreach ( $childArr as $child ) {
						array_push ( $returnArr, $child );
					}
				}
				Menu::$ii ++;
			}
		}
		return $returnArr;
	}
	public function beforeSave() {
		if (parent::beforeSave ()) {
			if ($this->isNewRecord) {
				$this->create_time = time ();
			}
			// 更改菜单层次
			if ($this->parentid == 0) {
				$this->level = 0;
			} else {
				$this->level = $this->parentid;
			}
			
			return true;
		} else {
			return false;
		}
	}

  

<div class="row">
		<?php echo $form->labelEx($model,'parentid'); ?>
		<?php echo $form->dropDownList($model,'parentid',Menu::model()->getMenuAllList(),array('encode'=>false)); ?>
		<?php echo $form->error($model,'parentid'); ?>
	</div>

db:

 * @property integer $id

 * @property integer $parentid

 * @property string $text

 * @property string $alias_name

 * @property string $icon_cls

 * @property integer $issort

 * @property string $href

 * @property string $level

 * @property integer $create_time

  

posted @ 2014-03-18 15:02  haiwei.sun  阅读(1069)  评论(2编辑  收藏  举报
返回顶部