使用此答案寻求支持在你的节点视图中,即。 nodeAddlView 位于您的 Treeview 小部件中的变量 nodeAddlView 下,或者位于您的 TreeViewSettings 数组中,其中 nodeAddlView 位于您的 config/main.php 中,您有一个别名,例如。 @app 这是高级应用程序的默认设置,即。未在 common/config/bootstrap.php 下列出,例如。
<?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 下与“别名”设置一起列出:
'aliases'=>[
'@bower'=>'@vendor/bower-asset',
'@npm'=>'@vendor/npm-asset',
],
该别名与路径连接或串联。这个组合位于
模块位于 frontend/config/main.php 中。例如。
'treemanager' => [
'class' => 'kartik\tree\Module',
'treeViewSettings'=> [
'nodeView' => '@kvtree/views/_form',
'nodeAddlViews' => [
1 => '',
2 => '',
3 => '',
4 => '',
5 => '@app/views/krajeeproducttree/product',
]]
],
或在 Treeview::widget 参数中,例如。
<?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'=>'»',
'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 可以简单地通过以下方式检索:
echo "Node id: ". $node->id;
在您创建的视图文件的底部。这是 id:
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实际上没有任何意义,因为如果你刷新数据库即。删除之前的记录,自增将从最新的开始
价值。
如果您指的是构成三个不同级别的 3 个单独表中每一个的 id,那么您必须将这些不同的 id 字段插入到树模型中,即。在 childallowed 字段之后,以便您可以使用不同表之一的实际 id 填充这些字段。这是在您的控制器中完成的,如上面的示例所示。当您单击节点时,只会填充 3 个附加 id 字段之一,即。另外两个将为空,例如。
然后,您可以通过在保存信息的表的 url 中使用此 id 来访问每个节点所需的信息。例如。
<?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 在下面提到了这一点树模型 -> 模型属性
您可以将自定义字段添加到树模型的数据库表中,并在扩展树模型中设置这些属性。但请注意,如果您需要通过表单读取和保存自定义属性数据,则必须正确设置模型属性。这些属性必须在模型规则中标记为安全,否则它们将无法通过 $model->load 方法进行大规模分配进行保存。