如何为在kartik树视图中单击的每个节点传递节点ID? [英] How to pass node id for each node clicked in kartik treeview?

查看:111
本文介绍了如何为在kartik树视图中单击的每个节点传递节点ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个节点.我想在每个节点单击上显示不同的视图.为此,我必须传递节点ID.下面是我尝试解释总场景的代码

I have multiple nodes. I want to show different views on each node click. For this, I have to pass the node id. Below is my code in which I have tried to explain the total scenario

`<?=
    TreeView::widget([
        'query' => \common\models\MdcNode::find()->addOrderBy('root, lft'),
        'headingOptions' => ['label' => 'Root'],
        'rootOptions' => ['label'=>'<span class="text-primary">Root</span>'],
        'topRootAsHeading' => true, // this will override the headingOptions
        //'displayValue' => 1,        // initial display value
        'isAdmin' => false,
        'fontAwesome' => true,
        //'nodeView' => '',
        //show => none removes the iconType etc setting under details
        'iconEditSettings'=>['show'=>'none'],

        'toolbar' =>
            [
            TreeView::BTN_REFRESH => false,
            TreeView::BTN_CREATE => false,
            TreeView::BTN_CREATE_ROOT => false,
            TreeView::BTN_REMOVE => false,
            TreeView::BTN_SEPARATOR => false,
            TreeView::BTN_MOVE_UP => false,
            TreeView::BTN_MOVE_DOWN => false,
            TreeView::BTN_MOVE_LEFT => false,
            TreeView::BTN_MOVE_RIGHT => false,
        ],

        'showIDAttribute' => false,
        'showTooltips' => false,

        'showNameAttribute' => false,
        'softDelete' => false,
        'cacheSettings' => ['enableCache' => true],
        //removing the detail below removes the second column of view(s) 1 - 5. Section 5 is being used to render the extra data. See frontend\config\main.php later.
        'mainTemplate'=>'<div class="row">
                        <div class="col-sm-3">
                            {wrapper}
                        </div>
                        <div class="col-sm-9">
                            {detail}
                        </div>
                     </div>',
        'nodeAddlViews' => [
            Module::VIEW_PART_1 => '',
            Module::VIEW_PART_2 => '',
            Module::VIEW_PART_3 => '',
            Module::VIEW_PART_4 => '',
            Module::VIEW_PART_5 => '@backend/views/api/index',
        ]

    ]);
    ?>`

控制器

public function actionIndex() { echo "{Index}"; }

查看

<?PHP
  /* @var $this yii\web\View */
  ?>
  

  <p>
    You may change the content of this page by modifying
    the file <code><?= __FILE__; ?></code>.
  </p>

用户界面

当我单击每个节点时,如何将每个节点ID传递给视图? 任何帮助将不胜感激.

How can I pass each node id to my view when I click each node? Any help would be highly appreciated.

推荐答案

Using this answer for support in your node view ie. nodeAddlView located in your Treeview widget under the variable nodeAddlView or in your TreeViewSettings array with nodeAddlView located in your config/main.php you have an alias eg. @app which is the default for the advanced app and ie. not listed under common/config/bootstrap.php eg.

<?php
Yii::setAlias('@common', dirname(__DIR__));
Yii::setAlias('@frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('@bower', dirname(dirname(__DIR__)) . '/vendor/bower-asset');
Yii::setAlias('@migrations', dirname(dirname(__DIR__)) . '/frontend/migrations');
Yii::setAlias('@console', dirname(dirname(__DIR__)) . '/console');

,此处未在frontend/config/main.php下列出别名"设置:

and is not listed with the 'aliases' setting under frontend/config/main.php here:

'aliases'=>[
      '@bower'=>'@vendor/bower-asset',
      '@npm'=>'@vendor/npm-asset',
    ],

此别名与路径连接或串联.这个组合坐在 在frontend/config/main.php中的模块.例如.

This alias is joined or concatenated with a path. This combination sits under Module in the frontend/config/main.php. eg.

'treemanager' =>  [
        'class' => 'kartik\tree\Module',
        'treeViewSettings'=> [
            'nodeView' => '@kvtree/views/_form',    
            'nodeAddlViews' => [
                1 => '',
                2 => '',
                3 => '',
                4 => '',
                5 => '@app/views/krajeeproducttree/product',
        ]]    
       ],

或在Treeview :: widget参数中.

or in the Treeview::widget parameters eg.

<?php  
    echo TreeView::widget([
        // single query fetch to render the tree
        'query' => KrajeeProductTree::find()->addOrderBy('root, lft'),
        'nodeView' => '@kvtree/views/_form',    
        'nodeAddlViews' => [
            1 => '',
            2 => '',
            3 => '',
            4 => '',
            5 => '@app/views/krajeeproducttree/product',
         ],  
        'headingOptions' => ['label' => 'Categories'],
        'fontAwesome' => true,      // optional
        'isAdmin' => false,         // optional (toggle to enable admin mode)
        'displayValue' => 1,        // initial display value
        'softDelete' => true,       // defaults to true
        'cacheSettings' => [        
            'enableCache' => true   // defaults to true
        ],
        'hideTopRoot'=>true,
        'treeOptions' => ['style' => 'height:1000px width:900px' ],
        //more detail can be added to the node here eg. $node->id but
        //for security better adding it to the bottom of your
        // view file listed here 
        // eg.   5 => '@app/views/krajeeproducttree/product', 
        // since it automatically carries and is passed the $node variable.
        'nodeLabel' => function($node) {
                             return $node->name . " ". $node->id;
                       },
        //disable the toolbar completely                       
        'toolbar'           => [
                TreeView::BTN_REFRESH => false,
                TreeView::BTN_CREATE => false,
                TreeView::BTN_CREATE_ROOT => false,
                TreeView::BTN_REMOVE => false,
                TreeView::BTN_SEPARATOR => false,
                TreeView::BTN_MOVE_UP => false,
                TreeView::BTN_MOVE_DOWN => false,
                TreeView::BTN_MOVE_LEFT => false,
                TreeView::BTN_MOVE_RIGHT => false,
            ],                 
        'showIDAttribute' => false,
        'showTooltips' => false,
        'showNameAttribute' => false,
        'softDelete' => false,
        'cacheSettings' => ['enableCache' => true],
        //show => none removes the iconType etc setting under details                       
        'iconEditSettings'=>['show'=>'none'],
        //
        'showFormButtons'=>false,
        'breadcrumbs'=>[//'depth'=>null,
                        'glue'=>'&raquo;',
                        'activeCss'=>'kv-crumb-active',
                        'untitled'=>'Untitled'
                       ],
        //removing header below removes the search button and header                       
        //'wrapperTemplate'=>'{header}{tree}{footer}',         
        'wrapperTemplate'=>'{tree}',
        //removing the detail below removes the second column of view(s) 1 - 5
        'mainTemplate'=>'<div class="row">
                            <div class="col-sm-3">
                                {wrapper}
                            </div>
                            <div class="col-sm-9">
                                {detail}
                            </div>
                         </div>'                       
    ]); 
?>

实际节点的ID,或者如果我理解正确的话,可以通过以下方式简单地检索扩展的TreeView模型的自动增量ID:

The id of the actual node, or if I understand you correctly, the autoincrement id of the extended TreeView model can be retrieved simply by:

echo "Node id: ". $node->id;

在创建的视图文件底部的

.这是ID中的ID:

at the bottom of your created view file. This is the id in:

CREATE TABLE tbl_product (
    id            INT(11)      NOT NULL AUTO_INCREMENT PRIMARY KEY,
    root          INT(11)               DEFAULT NULL,
    lft           INT(11)      NOT NULL,
    rgt           INT(11)      NOT NULL,
    lvl           SMALLINT(5)  NOT NULL,
    name          VARCHAR(60)  NOT NULL,
    icon          VARCHAR(255)          DEFAULT NULL,
    icon_type     TINYINT(1)   NOT NULL DEFAULT '1',
    active        TINYINT(1)   NOT NULL DEFAULT TRUE,
    selected      TINYINT(1)   NOT NULL DEFAULT FALSE,
    disabled      TINYINT(1)   NOT NULL DEFAULT FALSE,
    readonly      TINYINT(1)   NOT NULL DEFAULT FALSE,
    visible       TINYINT(1)   NOT NULL DEFAULT TRUE,
    collapsed     TINYINT(1)   NOT NULL DEFAULT FALSE,
    movable_u     TINYINT(1)   NOT NULL DEFAULT TRUE,
    movable_d     TINYINT(1)   NOT NULL DEFAULT TRUE,
    movable_l     TINYINT(1)   NOT NULL DEFAULT TRUE,
    movable_r     TINYINT(1)   NOT NULL DEFAULT TRUE,
    removable     TINYINT(1)   NOT NULL DEFAULT TRUE,
    removable_all TINYINT(1)   NOT NULL DEFAULT FALSE,
    child_allowed TINYINT(1)   NOT NULL DEFAULT FALSE,
    KEY tbl_product_NK1 (root),
    KEY tbl_product_NK2 (lft),
    KEY tbl_product_NK3 (rgt),
    KEY tbl_product_NK4 (lvl),
    KEY tbl_product_NK5 (active)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1;

但是该ID并没有任何意义,因为如果刷新数据库即.删除以前的记录,自动递增将从最新开始 值.

But this id does not really have any significance since if you refresh the database ie. delete previous records the autoincrement will start from the latest value.

如果您要引用构成三个不同级别的3个单独表中每个表的ID,则必须在其中将这些不同的ID字段插入树模型中.在允许孩子的字段之后输入,以便您可以使用不同表之一中的实际ID填充这些字段.如上面的上一个示例所示,这是在您的控制器中完成的.当您单击节点时,将仅填充3个其他id字段之一.其他两个将为空,例如.

If you are referring to the id of each of the 3 separate tables that make up the three different levels then this is where you will have to insert these different id field's into the tree model ie. after childallowed field so that you can populate these fields with the actual id from one of the different tables. This is done in your controller as shown in the previous example above. When you click on a node, only one of the 3 additional id fields will be populated ie. the other two will be null eg.

然后,您可以通过在包含该信息的表的URL中使用此ID来访问每个节点所需的信息.例如.

You can then access the information that you need for each node by using this id in a url to the table that holds the information. eg.

<?php
   use Yii;
   use yii\helpers\Url;
   use yii\helpers\Html;
?>
<div class="krajeeproducttree-product">
    <br>
        <?php
          if ($node->product_id > 0){
            echo Html::a('<h4>View House Details: ' .$node->name. '</h4>',Url::toRoute(['/product/view','id'=>$node->product_id]));
          }         
          if ($node->productsubcategory_id > 0){
            echo Html::a('<h4>View Street Details: ' .$node->name. '</h4>',Url::toRoute(['/productsubcategory/view','id'=>$node->productsubcategory_id]));
          }         
          if ($node->productcategory_id > 0){
            echo Html::a('<h4>View Postcode Details: ' .$node->name. '</h4>',Url::toRoute(['/productcategory/view','id'=>$node->productcategory_id]));
          }
          echo "Node id: ". $node->id;           
        ?>
    <br>
</div>

Krajee在树模型->中提到了这一点.模型属性

您可以将自定义字段添加到Tree模型的数据库表中,并在扩展Tree模型中设置这些属性.但是请注意,如果需要阅读& ;,则必须正确设置模型属性.通过表单保存自定义属性数据.这些属性必须在模型规则中标记为安全,否则将无法通过$ model-> load方法保存以进行大量分配.

这篇关于如何为在kartik树视图中单击的每个节点传递节点ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆