slatiusa / yii2-nestable
使用jquery.nestable插件实现的Yii 2.0嵌套集行为。
v1.2.1
2016-10-08 12:21 UTC
Requires
This package is not auto-updated.
Last update: 2024-09-14 17:21:25 UTC
README
使用jquery.nestable插件实现的Yii 2.0嵌套集行为。
- jquery.nestable插件: http://dbushell.github.io/Nestable/
- Yii 2的嵌套集行为: https://github.com/creocoder/yii2-nested-sets
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
$ php composer.phar require slatiusa/yii2-nestable "dev-master"
或者在您的 composer.json
文件的 require
部分添加以下内容:
"slatiusa/yii2-nestable": "dev-master"
to the require section of your composer.json file.
使用方法
请确保您已正确将NestedSetsBehavior (creocoder/yii2-nested-sets)附加到您的模型上。然后通过附加提供的操作将节点移动处理程序添加到您的控制器中;
use slatiusa\nestable\Nestable;
class yourClass extends Controller
{
public function actions() {
return [
'nodeMove' => [
'class' => 'slatiusa\nestable\NodeMoveAction',
'modelName' => TreeModel::className(),
],
];
}
然后在视图中渲染小部件;
echo Nestable::widget([
'type' => Nestable::TYPE_WITH_HANDLE,
'query' => TreeModel::find()->where([ top of tree ]),
'modelOptions' => [
'name' => 'name'
],
'pluginEvents' => [
'change' => 'function(e) {}',
],
'pluginOptions' => [
'maxDepth' => 7,
],
]);
您可以从 query
中提供一个ActiveQuery对象,从中构建一个树。您也可以提供一个项目列表;
...
'items' => [
['content' => 'Item # 1', 'id' => 1],
['content' => 'Item # 2', 'id' => 2],
['content' => 'Item # 3', 'id' => 3],
['content' => 'Item # 4 with children', 'id' => 4, 'children' => [
['content' => 'Item # 4.1', 'id' => 5],
['content' => 'Item # 4.2', 'id' => 6],
['content' => 'Item # 4.3', 'id' => 7],
]],
],
modelOptions['name']
应包含一个属性名,该属性名将用于列表中的项目命名。您还可以提供一个未命名的 function($model)
来构建您自己的内容字符串。
提供 pluginEvents['change']
并附上一些JavaScript代码以捕获由jquery.nestable插件触发的事件。 pluginOptions
接受原始jquery.nestable插件的所有选项。