viewtopic.php?f=9&t=804&start=20
Не совсем понимаю как это использовать(во вьюшке):
Код: Выделить всё
$this->widget('zii.widgets.CMenu', array(
'items'=>$root->getCMenuArray(array(
'returnrootnode'=>true,
)),
));
Код: Выделить всё
Fatal error: Call to a member function getCMenuArray() on a non-object in protected/modules/backend/views/cat/index.php on line 34
в модели:
Код: Выделить всё
public function behaviors(){
return array(
'TreeBehavior' => array(
'class' => 'application.extensions.nestedset.TreeBehavior',
'_idCol' => 'id',
'_lftCol' => 'lft',
'_rgtCol' => 'rgt',
'_lvlCol' => 'level',
),
'TreeViewTreebehavior' => array(
'class' => 'application.extensions.nestedset.TreeViewTreebehavior',
),
'ArrayTreeBehavior' => array(
'class' => 'application.extensions.nestedset.ArrayTreeBehavior',
),
);
}
Код: Выделить всё
// создаем массив, который будем кормить CDropdownList и CListBox
$data = CHtml::listData($model->findAll(array('order'=>'lft')), 'id', 'nameExt'); // Здесь переопределяем поле name на nameExt. Ниже описано зачем.
$model = Cat::model()->findByPK(1); // Здесь вместо Categories меняем на свою модель
$tree = $model->getTreeViewData(false);
$this->render('index',array(
'data'=>$data,
'root'=>$tree,
));
Код: Выделить всё
class ArrayTreeBehavior extends TreeBehavior {
public function getCMenuArray ($param = array()) {
if (isset($param['rootaslink'])) {
$rootaslink = $param['rootaslink'];
} else {
$rootaslink = false;
}
if (isset($param['returnrootnode'])) {
$returnrootnode = $param['returnrootnode'];
} else {
$returnrootnode = false;
}
if (isset($param['keyfield'])) {
$keyfield = $param['keyfield'];
} else {
$keyfield = '_id';
}
// Fetch the flat tree
$rawtree = $this->getTree($returnrootnode);
// Init variables needed for the array conversion
$tree = array();
$node =& $tree;
$position = array();
$lastitem = '';
$depth = $this->getLevelValue();
foreach($rawtree as $rawitem)
{
// If its a deeper item, then make it subitems of the current item
if ($rawitem->getLevelValue() > $depth)
{
$position[] =& $node; //$lastitem;
$depth = $rawitem->getLevelValue();
$node =& $node[$lastitem]['items'];
}
// If its less deep item, then return to a level up
else
{
while ($rawitem->getLevelValue() < $depth)
{
end($position);
$node =& $position[key($position)];
array_pop($position);
$depth = $node[key($node)]['node']->getLevelValue();
}
}
// Add the item to the final array
$node[$rawitem->$keyfield]['node'] = $rawitem;
if(!$rawitem->hasChildNodes() || $rootaslink)
$node[$rawitem->$keyfield]['url'] = array($rawitem->owner->_url);
$node[$rawitem->$keyfield]['label'] = $rawitem->owner->_name;
// save the last items' name
$lastitem = $rawitem->$keyfield;
}
// we don't care about the root node
if (!$returnrootnode)
{
reset($tree);
$tree = $tree[key($tree)]['items'];
//array_shift($tree);
}
return $tree;
}
}