kartik-v / yii2-tree-manager

An advanced tree management module using nested sets for Yii 2.
http://demos.krajee.com/tree-manager
Other
150 stars 108 forks source link

[bug] nodeActions adding are not the same. #102

Closed bubifengyun closed 8 years ago

bubifengyun commented 8 years ago

Do as your documents, I install TreeView, and copy all your yii2-tree-manager as a module in my ./frontend/modules. I also modify namespace to frontend/modules/tree. Everything works well, EXCEPT that when I configure nodeActions NODE_MANAGE will load forever.

My Usage is

<?= TreeView::widget([
    'query'             => $query,
    'headingOptions'    => ['label' => '部别'],
//    'nodeView'          => '@frontend/views/personinfo/_nodegridview',
    'nodeActions' => [
        Module::NODE_MANAGE => Url::to(['/personinfo/manage']),
        Module::NODE_SAVE => Url::to(['/personinfo/manage']),
    ],
    'isAdmin'           => false,
    'rootOptions'       => ['label' => '您可以查看的部别'],
    'displayValue'      => $see_unit,
    '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,
        TreeView::BTN_SEPARATOR => false,
    ],
]) ?>

from xdebug, I find that

It seems that Both of them are in the same array, but their configure properties are not the same.

bubifengyun commented 8 years ago

sorry, this time i paste here in Chinese, I will translate it into English in later days.

一、功能设想

yii2做的网站,一边是树形结构treeview,一边是表格列表gridview,利用kartik的插件,这两个都比较容易搭建起来。选择左边的treeview,根据不同的选择项,girdview显示对应节点的信息。这个功能具有较好的使用价值。

二、参考资料

三、完成步骤

总共花了两周时间才完成,中间查了很多资料,不一一记录了。由于时间较长,可能部分内容缺失,如果无法工作,欢迎在下面留言。总体流程是如下:安装tree manager的treeview,=》对treeview显示内容进行改动,=》把显示内容更改为gridview,=》修复gridview出现的各种问题。

1、安装tree manager 和 girdview

composer require kartik-v/yii2-tree-manager "@dev"
composer require kartik-v/yii2-grid "@dev"

2、数据库的导入

找到./vendor/kartik-v/yii2-tree-manager/schema/tree.sql文件,由于本人是xampp开发,使用phpmyadmin, 进入到工作用的db_lhpg,点击导入按钮,选择该文件导入即可。导入后,可以根据自己需要在tbl_tree添加部分项,或者修改部分备注。 最好不要删除原有的项。我把添加好的tbl_tree更名为tbl_unit了,没有做任何添加。

3、创建模型Model

可以使用gii自动生成工具,Model命名空间为common/models。修改./common/models/Unit.php文件(对应你生成的文件),修改内容如下。

namespace common\models;

use Yii;

class Tree extends \kartik\tree\models\Tree
{
/*略*/
}

4、创建Module

这部分内容比较多。

'modules' => [
   'treemanager' =>  [
        'class' => '\frontend\modules\tree\Module',
    ]
]

5、使用TreeView插件

假设现有一个Personinfo,对应有PersoninfoController的动作

    /**
     * Every User take control of Out in special unit.
     * @return mixed
     */
    public function actionOut()
    {
        $see_unit = Yii::$app->user->identity->see_unit;

        // add children and itself.
        $query = Unit::findOne($see_unit)
            ->children()
            ->orWhere(['id' => $see_unit])
            ->addOrderBy('root, lft');

        $current_unit = Yii::$app->session->get('current_unit', 0);
        if ($current_unit === 0){
            $current_unit = $see_unit;
        }

        return $this->render('out', [
            'query' => $query,
            'current_unit' => $current_unit,
        ]);
    }

解说:

下面说渲染的页面./frontend/views/personinfo/out.php,代码如下:

<?= TreeView::widget([
    'query'             => $query,
    'headingOptions'    => ['label' => '部别'],
    'nodeView'          => '@frontend/views/personinfo/_nodegridview',
    'nodeActions' => [
        Module::NODE_MANAGE => Url::to(['/treemanager/node/my-manage']),
    ],
    'isAdmin'           => false,
    'rootOptions'       => ['label' => '您可以查看的部别'],
    'displayValue'      => $current_unit,
    '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,
        TreeView::BTN_SEPARATOR => false,
    ],
]) ?>

解说:

Module::NODE_MANAGE => Url::to(['/treemanager/node/my-manage'])中的my-manage的解说, 在 ./frontend/modules/tree/controllers/NodeController.php文件中, 该动作对应于actionMyManage函数。内容如下。

    /**
     * View a tree node via ajax
     *
     * @return redirect to `/personinfo/out`
     */
    public function actionMyManage()
    {
        extract(static::getPostData());
        if (isset($id) && !empty($id)) {
            Yii::$app->session->set('current_unit', $id);
        }
        return $this->redirect(['/personinfo/out']);
    }

解说:

下面把./frontend/views/personinfo/_nodegridview.php的内容贴出来。

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;
use kartik\grid\GridView;
use kartik\tree\TreeView;
use common\models\Personinfo;
use common\models\PersoninfoSearch;
use common\models\Unit;

/* @var $this yii\web\View */
/* @var $model common\models\Personinfo */
/* @var $form yii\widgets\ActiveForm */

Pjax::begin();
// run every key=>value as variant in PHP.
extract($params);

$children = Unit::findOne($node->id)->children()->all();
$selfAndChildrenID = [$node->id];
foreach($children as $child){
    $selfAndChildrenID[] = $child->id;
}

$searchModel = new PersoninfoSearch();
$searchModel->unit_code = $selfAndChildrenID; 
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

echo GridView::widget([
    'id' => 'admin-gridview-id',
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        [
            'class' => 'kartik\grid\SerialColumn'
        ],
        'name',
        'is_married',
        'can_home_weekend',
        'mil_rank',
        [
            'class' => 'kartik\grid\ActionColumn',
            'template' => '{view}{update}',
        ],
    ],
    'containerOptions' => ['style'=>'overflow: auto'], // only set when $responsive = false
    'toolbar' =>  [
        '{export}',
        '{toggleData}'
    ],
    'pjax' => true,
    'bordered' => true,
    'striped' => false,
    'condensed' => false,
    'responsive' => true,
    'hover' => true,
 //   'floatHeader' => true,
//    'floatHeaderOptions' => ['scrollingTop' => $scrollingTop],
    'showPageSummary' => true,
    'panel' => [
        'type' => GridView::TYPE_PRIMARY
    ],
]);
Pjax::end();

解说:

调试发现,每次点击gridview的排序,都没有任何东西放在post变量里,这个问题需要好好解决。

这样就实现了一边是树形结构TreeView,一边是表格GridView,且GirdView可以调节改动。可以根据自己的需要,排序搜索GridView的内容。

四、值得完善之处

在花费两周的时间才解决这个问题时,设想了很多方案,比如把原作者的这个模块给肢解了,各自放到frontend里。 当然最好的方案是把这个模块留在./vendor文件夹里,不需要对他做任何改动。只是修改 Module::NODE_MANAGE => Url::to(['/frontend/personinfo/out']),,然后配置一下Controller里的ActionOut函数。 由于缺乏时间对此进行更深的研究,先留存在这里。

五、贴一张图来过过瘾。

输入图片说明

六、源代码

网站正在紧锣密鼓的开发中,后期会开源。