Skip to content

Commit

Permalink
tree and filter attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
nighthtr committed Nov 26, 2016
1 parent 4a5dc43 commit 28f1a1e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
32 changes: 24 additions & 8 deletions src/ChildrenAction.php
Expand Up @@ -9,20 +9,36 @@
class ChildrenAction extends Action
{
public $modelName;
public $filterAttribute;

public function run($id = 1)
public function run($id = null)
{
Yii::$app->response->format = Response::FORMAT_JSON;

$models = $this->modelName::findOne($id)->children(1)->all();
if ($this->modelName === null) {
throw new \yii\base\InvalidConfigException("No 'modelName' supplied on action initialization.");
}

$children = [];
foreach ($models as $key => $model) {
$children[] = [
'key' => $model->id,
'title' => $model->name,
'lazy' => !$model->isLeaf(),
];

$query = $this->modelName::find();

$query->where($id ? ['id' => $id] : ['depth' => 0]);

if ($this->filterAttribute) {
$query->andWhere([$this->filterAttribute => Yii::$app->request->get($this->filterAttribute) ? Yii::$app->request->get($this->filterAttribute) : null]);
}

if (($parent = $query->one()) !== null) {
$models = $parent->children(1)->all();

foreach ($models as $key => $model) {
$children[] = [
'key' => $model->id,
'title' => $model->name,
'lazy' => !$model->isLeaf(),
];
}
}

return $children;
Expand Down
9 changes: 6 additions & 3 deletions src/NestedSetsGrid.php
Expand Up @@ -13,12 +13,15 @@
class NestedSetsGrid extends \yii\base\Widget
{
public $model;
public $filterAttribute;
public $actions = [];

public $options = [
'class' => 'table table-condensed table-hover table-striped fancytree-fade-expander',
];
public $actions = [];
public $pluginEvents = [];

public $pluginOptions = [];
public $pluginEvents = [];

private $_defaultPluginOptions = [
'extensions' => ['dnd', 'edit', 'glyph', 'table'],
Expand Down Expand Up @@ -55,7 +58,7 @@ public function init()
}

if (!isset($this->actions['children'])) {
$this->actions['children'] = Url::to(['children']);
$this->actions['children'] = Url::to(['children', $this->filterAttribute => Yii::$app->request->get($this->filterAttribute)]);
}

if (!isset($this->actions['nodeMove'])) {
Expand Down
5 changes: 5 additions & 0 deletions src/NodeMoveAction.php
Expand Up @@ -9,6 +9,7 @@
class NodeMoveAction extends Action
{
public $modelName;
public $treeAttribute;

public function run($id, $target, $action)
{
Expand All @@ -21,6 +22,10 @@ public function run($id, $target, $action)
$model = $this->modelName::findOne($id);
$target = $this->modelName::findOne($target);

if ($this->treeAttribute && ($model->$this->treeAttribute != $target->$this->treeAttribute)) {
return ['status' => false];
}

switch ($action) {
case 'over':
return ['status' => $model->appendTo($target)];
Expand Down

0 comments on commit 28f1a1e

Please sign in to comment.