11<?php
22namespace wonail \nestable ;
33
4- use yii \base \InvalidConfigException ;
5- use yii \helpers \ArrayHelper ;
64use yii \helpers \Html ;
75use yii \helpers \Json ;
86use yii \widgets \InputWidget ;
@@ -58,6 +56,11 @@ class Nestable extends InputWidget
5856 */
5957 public $ draggableHandles = false ;
6058
59+ /**
60+ * @var boolean 是否折叠,默认为`false`
61+ */
62+ public $ collapsed = false ;
63+
6164 /**
6265 * @var array widget plugin options.
6366 *
@@ -167,6 +170,11 @@ private function renderItem($item)
167170 } else {
168171 $ options = ['class ' => 'dd-item ' ];
169172 }
173+
174+ if ($ this ->collapsed ) {
175+ Html::addCssClass ($ options , 'dd-collapsed ' );
176+ }
177+
170178 // 设置`data-`格式参数
171179 if (is_string ($ this ->allowParams )) {
172180 $ this ->allowParams = explode (', ' , $ this ->allowParams );
@@ -214,8 +222,6 @@ private function registerPluginAssets()
214222 $ pluginOptions = Json::encode ($ this ->pluginOptions );
215223 $ name = $ this ->hasModel () ? Html::getInputName ($ this ->model , $ this ->attribute ) : $ this ->name ;
216224 $ js = <<<JS
217- $(document).ready(function () {
218-
219225 $('.dd').nestable( {$ pluginOptions }).on('change', function () {
220226 var obj = $(this).parents('.nestable-box'),
221227 nestable = new Array();
@@ -230,8 +236,10 @@ private function registerPluginAssets()
230236
231237 $("[name= \"{$ name }\"]").val(JSON.stringify(nestable));
232238 });
233- });
234239JS ;
240+ if ($ this ->collapsed ) {
241+ $ js .= "\n$('.dd').nestable('collapseAll') " ;
242+ }
235243 $ view ->registerJs ($ js );
236244 }
237245
0 commit comments