Closed amun1303 closed 5 years ago
Have any one can help me?
hi! I am edit \vendor\encore\laravel-admin\src\Tree.php \vendor\encore\laravel-admin\views\tree.blade.php
Thanks @il-aeeeee I tried. But I don't want to change the vendor source code. I need something like the method $form->disableAction().
I agree, I would like also a way to only manage the parent/child relation (parent_id) with drag-n-drop, but not ordering. Actually I would like the ordering to be alphabetical, like same as title, in my case name
field... it would have been great to be able to use $tree->disableOrdering
and $tree->disableDelete
. After all there is already $tree->disableCreate()
...
Probably the easiest way is to just put a order field and ignore it when using it in your frontend. If you can live with it.. but I don't like that.
@amun1303 In case you choose to the hard route (I may also..):
vendor/encore/laravel-admin/views/tree/tree.blade.php
and vendor/encore/laravel-admin/views/tree/branch.blade.php
in your resources folder.vendor/encore/laravel-admin/src/Tree.php
vendor/encore/laravel-admin/src/Traits/ModelTree.php
is using heavily ordering queries. It may be possible to override some methods directly on the model or extend the trait.Lastly, even if there are a few bummers like these ..really love this useful package.
@z-song Do you have a plan for this?
I did something to achieve just enough for what I needed, I created a trait that will replace certain functions in order to disable (ignore) ordering, but still allow parent change:
app/Admin/Traits/ModelTree.php
<?php
namespace App\Admin\Traits;
use Illuminate\Support\Facades\DB;
trait ModelTree
{
/**
* @var boolean
*/
protected $disableOrdering = true;
/**
* Get all elements.
*
* @return mixed
*/
public function allNodes()
{
$orderColumn = DB::getQueryGrammar()->wrap($this->disableOrdering ? $this->titleColumn : $this->orderColumn);
$byOrder = $orderColumn.' = 0,'.$orderColumn;
$self = new static();
if ($this->queryCallback instanceof \Closure) {
$self = call_user_func($this->queryCallback, $self);
}
return $self->orderByRaw($byOrder)->get()->toArray();
}
/**
* Save tree order from a tree like array.
*
* @param array $tree
* @param int $parentId
*/
public static function saveOrder($tree = [], $parentId = 0)
{
if (empty(static::$branchOrder)) {
static::setBranchOrder($tree);
}
foreach ($tree as $branch) {
$node = static::find($branch['id']);
$node->{$node->getParentColumn()} = $parentId;
if (!$node->disableOrdering) {
$node->{$node->getOrderColumn()} = static::$branchOrder[$branch['id']];
}
$node->save();
if (isset($branch['children'])) {
static::saveOrder($branch['children'], $branch['id']);
}
}
}
}
app/Models/Category.php
:
<?php
namespace App\Models;
use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use App\Admin\Traits\ModelTree as AppModelTree;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use ModelTree, AppModelTree, AdminBuilder {
AppModelTree::allNodes insteadof ModelTree;
AppModelTree::saveOrder insteadof ModelTree;
}
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setParentColumn('parent_id'); // same as default
$this->setOrderColumn('order'); // not needed anymore
$this->setTitleColumn('name'); // my specific field name
}
}
You could also enable ordering by defining protected $disableOrdering = false;
. And also if you don't want to use a trait, you can override the classes on the model directly in case its the only model that needs ordering disabled.
So what happens here is that the title column will be used for ordering. You can still try to change ordering when dragging, but when saving it will only remember the parent change. This also means that you don't need to have a order
field in your table anymore. The ->select
also works well.
..for disabling the delete, you know what to do ;-)
vendor/encore/laravel-admin/views/tree/branch.blade.php
@z-song
Check parts where I use $this->disableOrdering
Thanks @isometriq I will try. But now I have to change from Tree model to Grid model for my business first.
How can I disable editing the tree please?. I need only to display it.
How to remove Delete button, Edit button and Disable Order function on tree view?