aadutskevich/yii2-treegrid

There is no license information available for the latest version (v1.0.2) of this package.

Installs: 48

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

v1.0.2 2017-05-22 15:14 UTC

This package is not auto-updated.

Last update: 2024-05-12 01:00:54 UTC


README

This is the jQuery TreeGrid extension for Yii 2. It encapsulates TreeGrid component in terms of Yii widgets, and thus makes using TreeGrid component in Yii applications extremely easy

Based on leandrogehlen/yii2-treegrid.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist aadutskevich/yii2-treegrid "*"

or add

"aadutskevich/yii2-treegrid": "*"

to the require section of your composer.json file.

How to use

Model

use yii\db\ActiveRecord;

/**
 * @property string $description
 * @property integer $parent_id
 */
class Tree extends ActiveRecord 
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'tree';
    }  
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['description'], 'required'],
            [['description'], 'string'],
            [['parent_id'], 'integer']
        ];
    }
}

Controller

use yii\web\Controller;
use Yii;
use yii\data\ActiveDataProvider;

class TreeController extends Controller
{

    /**
     * Lists all Tree models.
     * @return mixed
     */
    public function actionIndex()
    {
        $query = Tree::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider
        ]);
    }

View

use aadutskevich\treegrid\TreeGrid;
  
<?= TreeGrid::widget([
        'dataProvider' => $dataProvider,
        'keyColumnName' => 'id',
        'parentColumnName' => 'parent_id',
        'parentRootValue' => '0', //first parentId value
        'pluginOptions' => [
            'initialState' => 'collapsed',
        ],
        'columns' => [
            'name',
            'id',
            'parent_id',
            ['class' => 'yii\grid\ActionColumn']
        ]     
      ]); ?>