Hi Macinville,
I’m quite new to YII and started using MTreeView to display/manage a collection of folders and files.
My two models are properly built and relationed (giix did a great job…) and displaying them using MTree was a joke.
I have problems putting ajax links tough, hope you could advice.
In my tree-filling controller’s method I wrote:
public function actionXLSFolderFill(){
Yii::import('application.extensions.MTreeView.MTreeView');
if (!Yii::app()->request->isAjaxRequest) {
exit();
}
$parentId = "NULL";
// default query select all folders
$sql = "SELECT m1.id, "
. "m1.name AS text, "
. "m2.id IS NOT NULL AS hasChildren, "
. "'#' AS url, "
. "'' AS icon, "
. "m1.name AS tooltip, "
. "'' AS options "
. "FROM yi3_xlsfolder AS m1 LEFT JOIN yi3_xlsfile AS m2 ON m1.id=m2.xlsfolder_id "
. "GROUP BY m1.id ORDER BY m1.name ASC";
if (isset($_GET['root']) && $_GET['root'] !== 'source') {
// 'branch' query selects all files belonging to a specific folder
$parentId = (int) $_GET['root'];
$sql = "SELECT m1.id, "
. "m1.name AS text, "
. "0 AS hasChildren, "
. "'#' AS url, "
. "'' AS icon, "
. "m1.name AS tooltip, "
. "'' AS options "
. "FROM yi3_xlsfile AS m1 "
. "WHERE m1.xlsfolder_id = $parentId "
. "GROUP BY m1.id ORDER BY m1.name ASC";
}
$req = Yii::app()->db->createCommand($sql);
$children = $req->queryAll();
MTreeView::$_template_ = "{icon} {text}";
MTreeView::$_assetsPath_ = Yii::app()->request->baseUrl.'/images/';
echo str_replace(
'"hasChildren":"0"',
'"hasChildren":false',
MTreeView::saveDataAsJson($children)
);
exit();
}
}
Then, in my view, I have:
<?php
$this->widget('application.extensions.MTreeView.MTreeView',
array('url'=>array('XLSFolderFill'),
'animated'=>'fast',
'persist'=>'cookie',
'htmlOptions'=>array(
'class'=>'treeview-famfamfam',
),
// <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />
'template'=>'{icon} {text}',
'ajaxOptions'=>array('update'=>'#XLSDetails')
)
);
?>
</div>
<div id="XLSDetails"> </div>
Like this, eveything works as expectd. Now, I would like to populate the XLSDetails div with folder or file data, depending on which tree node was clicked, with data taken from respective ‘_view’ partials.
Not to mention populating target div with data from two different models, I couldn’t find a working way to populate the div at all!
I’ve tried adding a ‘fields’ array (as per the example) but it didn’t work.
I’ve tried adding code to my controller’s method (as per http://www.yiiframework.com/wiki/215/adding-class-and-links-to-ctreeview-node/) but it didn’t work either.
What would you (or anybody else with more experience than me) suggest?
Thanks for your great extension (it did teach me different things!).
Cheers,
rash*