# TreeGrid树型列表 **Repository Path**: fakis/yii2-treegrid ## Basic Information - **Project Name**: TreeGrid树型列表 - **Description**: 树型列表,扩展自Yii2 GridView的小部件 - **Primary Language**: PHP - **License**: BSD-3-Clause - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-07-01 - **Last Updated**: 2022-07-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # jQuery TreeGrid Extension for Yii 2 This is the [jQuery TreeGrid](https://github.com/maxazan/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 [![Yii2](https://img.shields.io/badge/Powered_by-Yii_Framework-green.svg?style=flat)](http://www.yiiframework.com/) ## Installation The preferred way to install this extension is through [composer](http://getcomposer.org/download/). Either run ``` php composer.phar require --prefer-dist fakis/yii2-treegrid "*" ``` or add ``` "fakis/yii2-treegrid": "*" ``` to the require section of your `composer.json` file. ## Usage **Model** ```php 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** ```php 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, 'pagination' => false ]); return $this->render('index', [ 'dataProvider' => $dataProvider ]); } ``` **View** ```php use fakis\treegrid\TreeGrid; $dataProvider, 'keyColumnName' => 'id', 'parentColumnName' => 'parent_id', 'parentRootValue' => '0', //first parentId value 'pluginOptions' => [ 'initialState' => 'collapsed', ], 'columns' => [ 'name', 'id', 'parent_id', ['class' => 'yii\grid\ActionColumn'] ] ]); ?> ``` ## Adding resources When is necessary to add other resource files, then should be used the [Dependency Injection](http://www.yiiframework.com/doc-2.0/guide-concept-di-container.html#registering-dependencies) concept. To use the `saveState` option it's necessary to add `jquery.cookie.js`. ```php //config/web.php $config = [ 'id' => 'my-app', 'components' => [ ... ] ... ] Yii::$container->set('fakis\treegrid\TreeGridAsset',[ 'js' => [ 'js/jquery.cookie.js', 'js/jquery.treegrid.min.js', ] ]); return $config; ```