This is my first post and from the start I want to greet all the members of the Yii forum. I’ve been playing for a week with TreeTable + OrderColumn extensions trying to make a gridview similar to Joomla for my adjacent menu. My CGridView looks like in the screenshot below:
2788
All works fine so far except the ordering column which moves the parents node independently, so if I move the parent node, the children doesnt change the position together with parent node. See the screenshot:
2789
I tried to add some relations in the Menu model but didn’t help me so far.
If anyone could point me in the right direction it will be much appreciated.
As you can see the extension is using nested set behavior but I tried to use it with hierarchical structure (check this tutorial) and the grid view will look like this:
<?php
Yii::import('zii.widgets.grid.CGridView');
class MyGridView extends CGridView {
/**
* @var string the base script URL for all treeTable view resources (e.g. javascript, CSS file, images).
* Defaults to null, meaning using the integrated grid view resources (which are published as assets).
*/
public $baseTreeTableUrl;
/**
* @var string the base script URL for jQuery ui draggable and droppable.
* Defaults to null, meaning using the integrated grid view resources (which are published as assets).
*/
/**
* Initializes the tree grid view.
*/
public function init() {
parent::init();
if ($this->baseTreeTableUrl === null)
$this->baseTreeTableUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.QTreeGridView.treeTable'));
}
/**
* Registers necessary client scripts.
*/
public function registerClientScript() {
parent::registerClientScript();
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($this->baseTreeTableUrl . '/javascripts/jquery.treeTable.js', CClientScript::POS_END);
$cs->registerCssFile($this->baseTreeTableUrl . '/stylesheets/jquery.treeTable.css');
$cs->registerScript('treeTable', '
$(document).ready(function() {
$("#' . $this->getId() . ' .items").treeTable();
$(".items input:checkbox").change(function() {
checkChildNodes($(this).closest("tr"), !!$(this).attr("checked"));
});
function checkChildNodes($node, checked) {
$(".items .child-of-" + $node.attr("id")).each(function() {
var $checkbox = $(this).find("input:checkbox");
if (checked)
$checkbox.attr("checked", "checked");
else
$checkbox.removeAttr("checked");
checkChildNodes($(this), checked);
});
}
});
');
}
/**
* Renders the data items for the grid view.
*/
public function renderItems() {
if (Yii::app()->user->hasFlash('CQTeeGridView')) {
print '<div style="background-color:#ffeeee;padding:7px;border:2px solid #cc0000;">' . Yii::app()->user->getFlash("CQTeeGridView") . '</div>';
}
parent::renderItems();
}
public function renderTableRow($row) {
$model = $this->dataProvider->data[$row];
$parentClass = $model->id_parent ? 'child-of-' . $model->id_parent . ' ' : '';
echo '<tr style="display:none;" class="before" id="before-' . $model->getPrimaryKey() . '"><td style="padding:0;"><div style="height:3px;"></div></td></tr>';
if ($this->rowCssClassExpression !== null) {
echo '<tr id="' . $model->getPrimaryKey() . '" class="' . $parentClass . $this->evaluateExpression($this->rowCssClassExpression, array('row' => $row, 'data' => $model)) . '">';
} else if (is_array($this->rowCssClass) && ($n = count($this->rowCssClass)) > 0)
echo '<tr id="' . $model->getPrimaryKey() . '" class="' . $parentClass . $this->rowCssClass[$row % $n] . '">';
else
echo '<tr id="' . $model->getPrimaryKey() . '" class="' . $parentClass . '">';
foreach ($this->columns as $column) {
$column->renderDataCell($row);
}
echo "</tr>\n";
echo '<tr style="display:none;" class="after" id="after-' . $model->getPrimaryKey() . '"><td style="padding:0;"><div style="height:3px;"></div></td></tr>';
}
}
Now let’s suppose we have an update form like this:
Now the only problem is how to create a dependent relation for the parents and children, so for example if we move the parent node, the children will move togheter with its parent in the grid view .
Here is the page for the extention, it is Russian but you can open with chrome and translate to English. On this page you will find the instalation tutorial. This extention will work with nested set behavior extention.
My controller looks like this:
class Menu2Controller extends Controller {
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout = '//layouts/column2';
public $MyGridView = array(
'modelClassName' => 'Menu2',
);
View in admin instead of the widget [i] zii.widgets.grid.CGridView [/ i] use [i] ext.QTreeGridView.CQTreeGridView [/ i], add the option ‘ajaxUpdate’ => false.
Add to the controller:
public $CQtreeGreedView = array (
'ModelClassName' => 'Page2', / / class name
'AdminAction' => 'admin' / / action, which is derived QTreeGridView. This will go a redirect to other actions.
I’m glad you figured out, I was also using in hierarchy model, but as I mentioned previously I couldn’t create any relation between parent rows and children rows. For example if I move the children, these don’t move together with its parent, and the grid becomes a mess. So to simplify your work your grid should look like this:
<?php
Yii::import('zii.widgets.grid.CGridView');
class MyGridView extends CGridView
{
/**
* Initializes the tree grid view.
*/
public function init()
{
parent::init();
if($this->baseTreeTableUrl===null)
$this->baseTreeTableUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.MyGridFolder.treeTable'));
}
/**
* Registers necessary client scripts.
*/
public function registerClientScript()
{
parent::registerClientScript();
$cs=Yii::app()->getClientScript();
$cs->registerScriptFile($this->baseTreeTableUrl.'/javascripts/jquery.treeTable.js',CClientScript::POS_END);
$cs->registerCssFile($this->baseTreeTableUrl.'/stylesheets/jquery.treeTable.css');
$cs->registerScript('treeTable', '
$(document).ready(function() {
$("#'.$this->getId().' .items").treeTable();
function checkChildNodes($node, checked) {
$(".items .child-of-" + $node.attr("id")).each(function() {
var $checkbox = $(this).find("input:checkbox");
if (checked)
$checkbox.attr("checked", "checked");
else
$checkbox.removeAttr("checked");
checkChildNodes($(this), checked);
});
}
});
');
}
/**
* Renders the data items for the grid view.
*/
public function renderItems() {
if(Yii::app()->user->hasFlash('MyGridView')) {
print '<div style="background-color:#ffeeee;padding:7px;border:2px solid #cc0000;">'. Yii::app()->user->getFlash("MyGridView") . '</div>';
}
parent::renderItems();
}
public function renderTableRow($row)
{
$model=$this->dataProvider->data[$row];
$parentClass = $model->id_parent
?'child-of-'.$model->id_parent.' '
:'';
echo '<tr style="display:none;" class="before" id="before-'.$model->getPrimaryKey().'"><td style="padding:0;"><div style="height:3px;"></div></td></tr>';
if($this->rowCssClassExpression!==null)
{
echo '<tr id="'.$model->getPrimaryKey().'" class="'.$parentClass.$this->evaluateExpression($this->rowCssClassExpression,array('row'=>$row,'data'=>$model)).'">';
}
else if(is_array($this->rowCssClass) && ($n=count($this->rowCssClass))>0)
echo '<tr id="'.$model->getPrimaryKey().'" class="'.$parentClass.$this->rowCssClass[$row%$n].'">';
else
echo '<tr id="'.$model->getPrimaryKey().'" class="'.$parentClass.'">';
foreach($this->columns as $column) {
$column->renderDataCell($row);
}
echo "</tr>\n";
echo '<tr style="display:none;" class="after" id="after-'.$model->getPrimaryKey().'"><td style="padding:0;"><div style="height:3px;"></div></td></tr>';
}
}
florin, could you help me, how to get those model->search(), but i have 3 model to combine, where the attribute has a different name, what do you think about my problem?