问题
I am using Kartik Tree Manager. I am able to add, remove, update nodes. There is more requirement, that is to show/hide nodes on the basis of user access. i.e. when a user is given a specific node(s) then only that particular node(s) with all the child (if any) should be shown.
What I have done so far?
I have created a table user-node
in which I am assigning a node id to a user as shown below
What I want to do
Now I want to show only the specified node with its child node only and hide other nodes to that user
Controller
For now, there are two views in which I am rendering the tree structure but in the future, there shall be more
My front page
public function actionIndex() { if(Yii::$app->user->isGuest){ $this->redirect(Yii::$app->urlManager->createUrl('site/login')); } return $this->render('index'); }
Itself tree-manager node controller
/** * View, create, or update a tree node via ajax * * @return mixed json encoded response */ public function actionManage() { static::checkValidRequest(); $data = static::getPostData(); $nodeTitles = TreeSecurity::getNodeTitles($data); $callback = function () use ($data, $nodeTitles) { $id = ArrayHelper::getValue($data, 'id', null); $parentKey = ArrayHelper::getValue($data, 'parentKey', ''); $parsedData = TreeSecurity::parseManageData($data); $out = $parsedData['out']; $oldHash = $parsedData['oldHash']; $newHash = $parsedData['newHash']; /** * @var Module $module * @var Tree $treeClass * @var Tree $node */ $treeClass = $out['treeClass']; if (!isset($id) || empty($id)) { $node = new $treeClass; $node->initDefaults(); } else { $node = $treeClass::findOne($id); } $module = TreeView::module(); $params = $module->treeStructure + $module->dataStructure + [ 'node' => $node, 'parentKey' => $parentKey, 'treeManageHash' => $newHash, 'treeRemoveHash' => ArrayHelper::getValue($data, 'treeRemoveHash', ''), 'treeMoveHash' => ArrayHelper::getValue($data, 'treeMoveHash', ''), ] + $out; if (!empty($data['nodeViewParams'])) { $params = ArrayHelper::merge($params, unserialize($data['nodeViewParams'])); } if (!empty($module->unsetAjaxBundles)) { $cb = function ($e) use ($module) { foreach ($module->unsetAjaxBundles as $bundle) { unset($e->sender->assetBundles[$bundle]); } }; Event::on(View::class, View::EVENT_AFTER_RENDER, $cb); } TreeSecurity::checkSignature('manage', $oldHash, $newHash); return $this->renderAjax($out['nodeView'], ['params' => $params]); }; return self::process( $callback, Yii::t('kvtree', 'Error while viewing the {node}. Please try again later.', $nodeTitles), null ); }
How can I achieve it? Any help would be highly appreciated.
来源:https://stackoverflow.com/questions/62632209/yii2-show-hide-kartik-treeview-nodes