Skip to content

Commit b9e4cca

Browse files
committed
添加[[collapsed]]参数,用于是否允许折叠
1 parent 1c3f1b1 commit b9e4cca

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Nestable.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
namespace wonail\nestable;
33

4-
use yii\base\InvalidConfigException;
5-
use yii\helpers\ArrayHelper;
64
use yii\helpers\Html;
75
use yii\helpers\Json;
86
use 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-
});
234239
JS;
240+
if ($this->collapsed) {
241+
$js .= "\n$('.dd').nestable('collapseAll')";
242+
}
235243
$view->registerJs($js);
236244
}
237245

0 commit comments

Comments
 (0)