File tree Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Original file line number Diff line number Diff line change 2828Once 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+ ```
Original file line number Diff line number Diff line change 33namespace lesha724 \bootstraptree ;
44use yii \helpers \Html ;
55use yii \helpers \Json ;
6+ use yii \web \JsExpression ;
67use 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
You can’t perform that action at this time.
0 commit comments