Hi,
My admin.php view page has two div parts, the left is CTeView, and the right side is CGridView.
The CTreeView is filled with nodes. I want to make each node to be a link, so everytime i click one node, the right side CGridView will update it’s related database table regarding to the clicked node.
Here is my Controller class php file: "ProjectController.php"
class ProjectController extends Controller
{
//$this->layout = false;
/**
* @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 $treeNodeText;
public function actionAdmin()
{
$model=new Project('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Project']))
$model->attributes=$_GET['Project'];
$table = 'jiashu';
$count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM '.$table)->queryScalar();
$sql='SELECT * FROM '.$table;
$dataProvider=new CSqlDataProvider($sql, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'id', 'name', 'description',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
$this->render('admin',array('dataProvider' => $dataProvider,'model'=>$model,));
}
public function actionajaxFillTree(){
if (!Yii::app()->request->isAjaxRequest) {
exit();
}
$parentId = 0;
if (isset($_GET['root']) && $_GET['root'] !== 'source') {
$parentId = (int) $_GET['root'];
}
$treeNodeText = $parentId;
$req = Yii::app()->db->createCommand(
"SELECT m1.id, m1.name AS text, m2.id IS NOT NULL AS hasChildren "
. "FROM tbl_tree AS m1 LEFT JOIN tbl_tree AS m2 ON m1.id=m2.parent_id "
. "WHERE m1.parent_id <=> $parentId "
. "GROUP BY m1.id ORDER BY m1.name ASC"
);
$children = $req->queryAll();
$treedata=array();
foreach($children as $child){
$options=array('href'=>'#','id'=>$child['id'],'class'=>'treenode');
//Create tag 'a', and put $options contents in to a.
$nodeText = CHtml::openTag('a', $options);
$nodeText.= $child['text'];
$nodeText.= CHtml::closeTag('a')."\n";
$child['text'] = $nodeText;
$treedata[]=$child;
}
echo str_replace(
'"hasChildren":"0"',
'"hasChildren":false',
CTreeView::saveDataAsJson($treedata)
);
//$this->render('admin',array('dataProvider' => $dataProvider,));
exit();
}
Here is my view php file: admin.php
<?php
$this->widget(
'CTreeView',
array('url' => array('/project/ajaxFillTree'),
)
);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
)
);
?>
I can successfully create a treeview with all link node. But i am confused of how to click one of the node, then the $dataprovider chages as well, and the CGridView is updated as well.
Thank you very much for your help!