/
JsTree.php
executable file
·77 lines (64 loc) · 2.05 KB
/
JsTree.php
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace letyii\jstree;
use yii\helpers\Html;
use yii\helpers\Json;
class JsTree extends \yii\base\Widget
{
/**
* JsTree items
* @see http://www.jstree.com/docs/json/
* @var array
*/
public $items;
/**
* JsTree options
* @var array
*/
public $clientOptions = [];
/**
* @var string the template for arranging the jstree and the hidden input tag.
*/
// public $template = '{input}{jstree}';
/**
* @var array the HTML attributes for the input tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/**
* @inheritdoc
*/
public function init()
{
JsTreeAsset::register($this->getView());
$this->options['id'] = isset($this->options['id']) ? $this->options['id'] : $this->getId();
parent::init();
}
/**
* Renders the widget.
*/
public function run()
{
$clientOptions = $this->getClientOptions();
$clientOptions = empty($clientOptions) ? '' : Json::encode($clientOptions);
$view = $this->getView();
$view->registerJs("
jQuery('#" . $this->options['id'] . "')
.on('loaded.jstree', function() { jQuery(this).jstree('select_node', jQuery('#" . $this->options['id'] . "').val().split(','), true); })
.on('changed.jstree', function(e, data) { jQuery('#" . $this->options['id'] . "').val(data.selected.join()); })
.jstree($clientOptions);
");
echo Html::tag('div', '', $this->options);
}
/**
* Returns the options for jstree
* @return array
*/
protected function getClientOptions()
{
$options = $this->clientOptions;
if ($this->items !== null)
$options['core']['data'] = $this->items;
$options['core']['check_callback'] = isset($options['core']['check_callback']) ? $options['core']['check_callback'] : true;
return $options;
}
}