Show Parent And Children In Cgridview

In a table showing all ‘projects’, I have projects with and without parents. I’d like to show:

  • parent projects (aka projects with no parentId)

  • Under each parent project the related child projects

So, for instance:

  • project, not parent

  • project, parent

    • child

    • child

    • child

  • project, parent

    • child
  • project, not parent

I came across the GroupGridView extension, but don’t know if that can help me… Anyone?

[size="5"]In your relation[/size]:

‘parent’ => array(self::BELONGS_TO, ‘Yiicms’, ‘parent_title’, ‘condition’ => ‘t.parent_title = 0’),

					'children' => array(self::HAS_MANY, 'Yiicms', 'parent_title'),


					'childCount' => array(self::STAT, 'Yiicms', 'parent_title'),

:

:

public static function getParent($id)

{


	$title = self::model()->findByPk($id);


	//echo '<pre>';


	//print_r($title); exit;


	return $title['title'];


}

And your controller:

public function actionAjaxFillTree()

{


    // accept only AJAX request (comment this when debugging)


    if (!Yii::app()->request->isAjaxRequest) {


        exit();


    }


    // parse the user input


    $parentId = "NULL";


    if (isset($_GET['root']) && $_GET['root'] !== 'source') {


        $parentId = (int) $_GET['root'];


    }


    // read the data (this could be in a model)


    $children = Yii::app()->db->createCommand(


        "SELECT m1.id, m1.title AS text, m2.id IS NOT NULL AS hasChildren "


        . "FROM tree AS m1 LEFT JOIN tree AS m2 ON m1.id=m2.parent_id "


        . "WHERE m1.parent_id <=> $parent_title "


        . "GROUP BY m1.id ORDER BY m1.name ASC"


    )->queryAll();


    echo str_replace(


        '"hasChildren":"0"',


        '"hasChildren":false',


        CTreeView::saveDataAsJson($children)


    );


}

in your admin page:

<?php $this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=&gt;'yiicms-grid',


'dataProvider'=&gt;&#036;model-&gt;search(),


'filter'=&gt;&#036;model,


'columns'=&gt;array(


	'id',


	'title',


	'content',


	//'parent_title',


	array('name'=&gt;'parent_title',


			'value'=&gt; 'Yiicms::getParent(&#036;data-&gt;parent_title)'),


	array(


		'class'=&gt;'CButtonColumn',


	),


),

));

i don’t know what you r expectin but try this becoz this is my training project i got good result