• 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

      

  • 相关阅读:
    线程安全及Python中的GIL
    八个我最常用的AS3开源类库(转)
    Mac 10.6与Win7 文件共享
    linux下erlang之OTP编译安装
    位图和矢量图区别
    让你的AIR程序脱离AIR环境运行
    outlook 0x80040201 错误
    NOSQL
    RLE压缩及优化图片压缩
    改正6种常见HTML5错误用法
  • 原文地址:https://www.cnblogs.com/haiwei_sun/p/3607782.html
Copyright © 2020-2023  润新知