Skip to content

Commit 9b9a0be

Browse files
committed
add events
1 parent 7aa25c9 commit 9b9a0be

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,21 @@ Usage
2828
Once the extension is installed, simply use it in your code by :
2929

3030
```php
31-
<?= \lesha724\bootstraptree\TreeView::widget(); ?>```
31+
<?= \lesha724\bootstraptree\TreeView::widget([
32+
'htmlOptions'=>[
33+
'id'=>'treeview-tabs'
34+
],
35+
'options'=>[
36+
'data'=>$items,
37+
'enableLinks'=>true,
38+
'showTags'=>true,
39+
'levels'=>3
40+
],
41+
'events'=>[
42+
'onNodeSelected'=>'function(event, data) {
43+
// Your logic goes here
44+
alert(data.href);
45+
}'
46+
]
47+
]); ?>
48+
```

TreeView.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace lesha724\bootstraptree;
44
use yii\helpers\Html;
55
use yii\helpers\Json;
6+
use yii\web\JsExpression;
67
use yii\web\View;
78

89
/**
@@ -20,6 +21,11 @@ class TreeView extends \yii\base\Widget
2021
*/
2122
public $options=[];
2223

24+
/**
25+
* @var array additional options that can be passed to the constructor of the treeview js object.
26+
*/
27+
public $events=[];
28+
2329
protected $_id;
2430

2531
public function init(){
@@ -38,12 +44,26 @@ public function run()
3844
$view = $this->getView();
3945
TreeViewAsset::register($view);
4046

41-
$options=$this->options===[]?'{}' : Json::encode($this->options);
47+
$options = $this->_getEventsOptions();
48+
$options=$options===[]?'{}' : Json::encode($options);
4249
$view->registerJs('$("#' . $this->_id . '").treeview( ' .$options .')', View::POS_READY);
4350

4451
return $this->_runWidget();
4552
}
4653

54+
/**
55+
* @return array the javascript options
56+
*/
57+
protected function _getEventsOptions()
58+
{
59+
$options=$this->options;
60+
foreach($this->events as $key=>$event)
61+
{
62+
$options[$key]=$_function = new JsExpression($event);
63+
}
64+
return $options;
65+
}
66+
4767
/**
4868
* Вывод виджета
4969
* @return string html widget

0 commit comments

Comments
 (0)