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

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

问题描述

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

`commonmodelsMdcNode::find()->addOrderBy('root, lft'),'headingOptions' =>['标签' =>'根'],'rootOptions' =>['label'=''Root'],'topRootAsHeading' =>true,//这将覆盖headingOptions//'displayValue' =>1,//初始显示值'isAdmin' =>错误的,'fontAwesome' =>真的,//'nodeView' =>'',//显示=>none 删除详细信息下的 iconType 等设置'iconEditSettings'=>['show'=>'none'],'工具栏' =>[TreeView::BTN_REFRESH =>错误的,TreeView::BTN_CREATE =>错误的,TreeView::BTN_CREATE_ROOT =>错误的,树视图::BTN_REMOVE =>错误的,TreeView::BTN_SEPARATOR =>错误的,树视图::BTN_MOVE_UP =>错误的,TreeView::BTN_MOVE_DOWN =>错误的,TreeView::BTN_MOVE_LEFT =>错误的,TreeView::BTN_MOVE_RIGHT =>错误的,],'showIDAttribute' =>错误的,'showTooltips' =>错误的,'showNameAttribute' =>错误的,'软删除' =>错误的,'缓存设置' =>['启用缓存' =>真的],//删除下面的详细信息将删除视图 1 - 5 的第二列.第 5 部分用于呈现额外的数据.稍后参见 frontendconfigmain.php.'mainTemplate'=>'

<div class="col-sm-3">{包装}

<div class="col-sm-9">{细节}

','nodeAddlViews' =>[模块::VIEW_PART_1 =>'',模块::VIEW_PART_2 =>'',模块::VIEW_PART_3 =>'',模块::VIEW_PART_4 =>'',模块::VIEW_PART_5 =>'@backend/views/api/index',]]);?>`

控制器

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

查看

<p>您可以通过修改来更改此页面的内容文件<代码><?= __FILE__;?></代码>.</p>

界面

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

解决方案

然后,您可以通过在指向保存信息的表的 url 中使用此 id 来访问每个节点所需的信息.例如.

<div class="krajeeproducttree-product"><br><?php如果($node->product_id>0){echo Html::a('<h4>查看房屋详情:'.$node->name.'</h4>',Url::toRoute(['/product/view','id'=>;$node->product_id]));}如果($node->productsubcategory_id>0){echo Html::a('<h4>查看街道详情:'.$node->name.'</h4>',Url::toRoute(['/productsubcategory/view','id'=>;$node->productsubcategory_id]));}如果($node->productcategory_id>0){echo Html::a('<h4>查看邮政编码详情:'.$node->name.'</h4>',Url::toRoute(['/productcategory/view','id'=>;$node->productcategory_id]));}回声节点ID:".$node->id;?><br>

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

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

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' => commonmodelsMdcNode::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 frontendconfigmain.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',
        ]

    ]);
    ?>`

Controller

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

View

<?PHP
  /* @var $this yiiwebView */
  ?>
  

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

UI

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');

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

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

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

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

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>'                       
    ]); 
?>

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;

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;

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.

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.

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 yiihelpersUrl;
   use yiihelpersHtml;
?>
<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 mentions this under Tree Model -> Model Attributes

You can add custom fields to the database table for the Tree model, and set these attributes in your extended Tree model. However NOTE that you must set the model attributes correctly if you need to read & save the custom attribute data via the form. These attributes must be marked as safe in your model rules, else they will not be available for saving via $model->load method for massive assignment.

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

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆