PHP code example of myzero1 / yii2-ztree

1. Go to this page and download the library: Download myzero1/yii2-ztree library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

myzero1 / yii2-ztree example snippets


function actionAjaxnode()
 {
     $db = new Query;
     $cat = $db->from('product_category')->orderBy('id asc')->all();
     for($v=1;$v<count($cat);$v++){
         $pid = $cat[$v]['parent_id'];
         $cat[$v]['pId'] = $pid;
     }
     return json_encode($cat);
 }

	<?=\mallka\ztree\Ztree::widget([
       'eleId'=>'treeDemo222',
       'selValEleId'=>"alltypeid",
       'data'=>[           //没有dat_ajax,改为以下格式数组
           ['id'=>1, 'pId'=>0, 'name'=>'目录1'],
           ['id'=>2, 'pId'=>1, 'name'=>'目录2'],
           ['id'=>3, 'pId'=>1, 'name'=>'目录3'],
           ['id'=>4, 'pId'=>1, 'name'=>'目录4'],
           ['id'=>5, 'pId'=>2, 'name'=>'目录5'],
           ['id'=>6, 'pId'=>3, 'name'=>'目录6']
       ],
       'radio'=>true,
       'selVal'=>[956,957],
        'readOnly'=>true,
	])

	<?=\mallka\ztree\Ztree::widget([
       'eleId'=>'treeDemo222',
       'selValEleId'=>"alltypeid",
       'data'=>Model::find()->where(xxxx)->all(),
       'radio'=>true,
       'selVal'=>[956,957],
       'readOnly'=>true,
   ])

<?= $form->field($model, 'kind_value')->widget(
    \mallka\ztree\Ztree::class,
    [
    
               'data'=>\yii\helpers\Url::to(['product-category/ajaxnode'],true),
               'data_ajax'=>true,
               'checkbox'=>true,
               //'readOnly'=>true,
    ]
)