CGridView + TreeTable + OrderColumn

Hi All,

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

grid1.jpg

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

grid2.jpg

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.

Thank You!

how can you manage doing that?? i want to use treetable, but i dont know how to, could you share to create treetable with connecting to database?

Hi iceinrain,

I was thinking to use this gridview to manage my database driven menu. I gave up since I found another solution.

Here is the extention to create a gridview as in the screenshots attached.

thanks for replying, i will try it, it was a big help,

i’ll let u know if i can make it

Hi,

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:


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'menu2-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'position'); ?>

		<?php echo $form->textField($model,'position'); ?>

		<?php echo $form->error($model,'position'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'id_parent'); ?>

		<?php echo $form->textField($model,'id_parent'); ?>

		<?php echo $form->error($model,'id_parent'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'title'); ?>

		<?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo $form->error($model,'title'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'url'); ?>

		<?php echo $form->textField($model,'url',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo $form->error($model,'url'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->

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 ::).

it still wont work, i confuse where should i put the code,

could u give me code in the view?

i just change to this ext.QTreeGridView.CQTreeGridView

its not working

or is there any tutorial for this problem?/

Hi,

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',

    );



My admin view looks like this:


<?php

$this->widget('ext.QTreeGridView.MyGridView', array(

    'ajaxUpdate' => false,

    'id'=>'campaign-grid',

    'dataProvider' => $model->search(),

    'filter' => $model,

    'columns' => array(

        'id',

        'id_parent',

        'title',

        'url',

        array(

            'class' => 'CButtonColumn',

        ),

    ),

));

?>

Hope it helps B) .

i got an error

Property "modelclassname.tree" is not defined.

how to create the tree?

this is the instruction:

  • Generate a model, crud. Get rid of references to id, leftkey, rightkey and level in the model and forms.

    • model n crud i created with gii, get rid of that variabel? i dont have that in my model
  • Download YiiExt Nested set behaviour, follow the instructions to extend the model. If you select a single root

    • i download it n put in extension, and i add behavior function
  • manually insert a record with id = 1, level = 1, leftKey = 1, rightKey = 2. In the model to make a public property parentId.

    • in model? or in database? idont have that field in my database
  • Add to sort the search method on a field root (if any) and leftkey. If everything is done according to instructions, then this line:

      &#036; Criteria-&gt; order = &#036; this-&gt; tree-&gt; hasManyRoots
    
    
                         ? &#036; This-&gt; tree-&gt; rootAttribute. ','. &#036; This-&gt; tree-&gt; leftAttribute
    
    
                         : &#036; This-&gt; tree-&gt; leftAttribute;
    
  • Download CQTreeGridView, put in extensions.

  • 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' =&gt; 'Page2', / / ​​class name
    
    
      'AdminAction' =&gt; 'admin' / / action, which is derived QTreeGridView. This will go a redirect to other actions.
    

    );

  • Add generic action in the controller:

    public function actions () {

      return array (
    
    
          'Create' =&gt; 'ext.QTreeGridView.actions.Create',
    
    
          'Update' =&gt; 'ext.QTreeGridView.actions.Update',
    
    
          'Delete' =&gt; 'ext.QTreeGridView.actions.Delete',
    
    
          'MoveNode' =&gt; 'ext.QTreeGridView.actions.MoveNode',
    
    
          'MakeRoot' =&gt; 'ext.QTreeGridView.actions.MakeRoot',
    
    
      );
    

    }

thank you florin, i just try code as same as your code,

i guest i can understand this treetable,

n i hope i can make it in hierarchy model, cause my database model like that, with one to many relationship

Hi,

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>';

       

    }


}



Then in admin view:




$this->widget('ext.MyGridFolder.MyGridView', array(

    'ajaxUpdate' => false,

    //'id'=>'campaign-grid',

    'dataProvider' => $model->search(),

    'filter' => $model,

    'columns' => array(

      

        'id',

        'id_parent',

        'title',

     

        array(

            'class' => 'CButtonColumn',

        ),

    ),

));

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?

i can use an array for that, but javascript treetable, doesn’t work woth bootstrap :’(

Authos says:

public function behaviors() {


    return array(


        'tree'=&gt;array(


            'class'=&gt;'ext.yiiext.behaviors.model.trees.ENestedSetBehavior',


            'hasManyRoots'=&gt;true,


        )


    );


}

Hi

I am Newbie in yii.i use ext.QTreeGridView.CQTreeGridView its work for me .but the order of view is not right.

Order mismatched.all parent category not display under main category.Let me know if any one have a solution and customize this extension.

thanks