Yii Framework Forum: Show Parent And Children In Cgridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Show Parent And Children In Cgridview

#1 User is offline   Paul Verheul 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 07-January 14

Posted 29 April 2014 - 05:43 AM

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?
0

#2 User is offline   nawin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 15-March 14
  • Location:இந்தியா, தமிழ்நாடு (India,TamilNadu)

Posted 29 April 2014 - 06:28 AM

In your relation:

'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'=>'yiicms-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'title',
'content',
//'parent_title',
array('name'=>'parent_title',
'value'=> 'Yiicms::getParent($data->parent_title)'),
array(
'class'=>'CButtonColumn',
),
),
));

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users