Yii2 Show/Hide kartik treeview nodes

女生的网名这么多〃 提交于 2020-07-01 06:09:27

问题


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

  1. My front page

    public function actionIndex()
    {
       if(Yii::$app->user->isGuest){
        $this->redirect(Yii::$app->urlManager->createUrl('site/login'));
        }
       return $this->render('index');
    }
    
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!