-
-
Save BcTpe4HbIu/4fbea5884ca84a8890ee to your computer and use it in GitHub Desktop.
Select options as a tree #196
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//location: /Modules/categories/ | |
Admin::model('Modules\Categories\Entities\Category')->title('Categories')->display(function () | |
{ | |
$display = AdminDisplay::tree(); | |
$display->value('name'); | |
// configure display | |
return $display; | |
})->createAndEdit(function () | |
{ | |
$form = AdminForm::form(); | |
$parent_categories = Modules\Categories\Entities\Category::getNestedList('name', 'id', '-'); | |
$form->items([ | |
FormItem::text('name', 'Name'), | |
FormItem::categoryselect('parent_id', 'Parent ID') | |
->options($parent_categories) | |
]); | |
return $form; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\FormItems; | |
//location: /app/FormItems/ | |
use SleepingOwl\Admin\AssetManager\AssetManager; | |
use SleepingOwl\Admin\Repository\BaseRepository; | |
use SleepingOwl\Admin\FormItems\NamedFormItem; | |
class CategorySelect extends NamedFormItem | |
{ | |
protected $view = 'select'; | |
protected $model; | |
protected $display = 'title'; | |
protected $options = []; | |
protected $nullable = false; | |
public function initialize() | |
{ | |
parent::initialize(); | |
AssetManager::addStyle('admin::default/css/formitems/select/chosen.css'); | |
AssetManager::addScript('admin::default/js/formitems/select/chosen.jquery.min.js'); | |
AssetManager::addScript('admin::default/js/formitems/select/init.js'); | |
} | |
public function model($model = null) | |
{ | |
if (is_null($model)) | |
{ | |
return $this->model; | |
} | |
$this->model = $model; | |
return $this; | |
} | |
public function display($display = null) | |
{ | |
if (is_null($display)) | |
{ | |
return $this->display; | |
} | |
$this->display = $display; | |
return $this; | |
} | |
public function options($options = null) | |
{ | |
if (is_null($options)) | |
{ | |
if ( ! is_null($this->model()) && ! is_null($this->display())) | |
{ | |
$this->loadOptions(); | |
} | |
$options = $this->options; | |
//asort($options); | |
return $options; | |
} | |
$this->options = $options; | |
return $this; | |
} | |
protected function loadOptions() | |
{ | |
$repository = new BaseRepository($this->model()); | |
$key = $repository->model()->getKeyName(); | |
$options = $repository->query()->get()->lists($this->display(), $key); | |
$this->options($options); | |
} | |
public function getParams() | |
{ | |
return parent::getParams() + [ | |
'options' => $this->options(), | |
'nullable' => $this->isNullable(), | |
]; | |
} | |
public function enum($values) | |
{ | |
return $this->options(array_combine($values, $values)); | |
} | |
public function nullable($nullable = true) | |
{ | |
$this->nullable = $nullable; | |
return $this; | |
} | |
public function isNullable() | |
{ | |
return $this->nullable; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment