-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveFormJsTree.php
More file actions
52 lines (45 loc) · 1.41 KB
/
ActiveFormJsTree.php
File metadata and controls
52 lines (45 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Created by PhpStorm.
* User: johnny
* Date: 17-6-16
* Time: 上午12:27
*/
namespace johnnylei\jstree;
use yii\widgets\InputWidget;
use yii\base\Exception;
use yii\helpers\Json;
use yii\helpers\Html;
class ActiveFormJsTree extends InputWidget
{
public $containerId = 'js-tree';
public $options = [];
public $inputOptions = [
'class' => 'form-control',
'style'=>[
'display'=>'none',
],
];
public $js;
public function run()
{
if (!$this->hasModel()) {
throw new Exception('invalid model');
}
if (empty($this->options)) {
throw new Exception('invalid options');
}
JsTreeAsset::register($this->view);
$inputId = $this->inputOptions['id'] = isset($this->inputOptions['id'])?$this->inputOptions['id']:'js-tree-selected';
$initial_js = '
var tree = $("#'.$this->containerId.'").jstree('.Json::encode($this->options).');
var core = tree.data("jstree")
core.element.on("select_node.jstree", function (e, data) {
var input = $("#'.$inputId.'").val(data.node.data);
});';
$this->view->registerJs($initial_js . $this->js);
$this->inputOptions['hidden'] = true;
$input = Html::activeTextInput($this->model, $this->attribute, $this->inputOptions);
return $input.'<div id="'.$this->containerId.'"></div>';
}
}