我在 Yii 框架中使用了 CTreeView,因为我想显示用户列表。
所有用户都显示在我的树中,但我想将 css 添加到我正在读取节点信息的节点。
例如:如果我转到 user/view/[id],所有用户都会显示树,而 [id] 的用户会突出显示
怎么办呢?
控制器:
public function actionAjaxFillTree()
{
// accept only AJAX request (comment this when debugging)
if (!Yii::app()->request->isAjaxRequest) {
exit();
}
// parse the user input
$parentId = Yii::app()->user->id;
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.username AS text, m2.id IS NOT NULL AS hasChildren "
. "FROM psh_users AS m1 LEFT JOIN psh_users AS m2 ON m1.id=m2.parent_id "
. "WHERE m1.parent_id <=> $parentId "
. "GROUP BY m1.id ORDER BY m1.username ASC"
)->queryAll();
echo str_replace(
'"hasChildren":"0"',
'"hasChildren":false',
CTreeView::saveDataAsJson($children)
);
}
视图:
<?php
$this->widget(
'CTreeView',
array('url' => array('ajaxFillTree'),
)
);
?>
</div>
我可以突出显示相关页面的节点:
public function actionAjaxFillTree() {
// accept only AJAX request (comment this when debugging)
if (!Yii::app()->request->isAjaxRequest) {
exit();
}
$node = $_GET[id];
// parse the user input
$parentId = Yii::app()->user->id;
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.username AS text, m2.id IS NOT NULL AS hasChildren "
. "FROM psh_users AS m1 LEFT JOIN psh_users AS m2 ON m1.id=m2.parent_id "
. "WHERE m1.parent_id <=> $parentId "
. "GROUP BY m1.id ORDER BY m1.username ASC"
)->queryAll();
$treedata = array();
foreach ($children as $child) {
$options = ($child['id'] == $node)? array('href' => '#', 'id' => $child['id'], 'class' => 'treenode selected_node'):
array('href' => '#', 'id' => $child['id'], 'class' => 'treenode');
$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**)
);
}