- Основы
- Кнопка создания
- Кнопка детального просмотра
- Кнопка редактирования
- Кнопка удаления
- Кнопка массового удаления
- Кнопка экспорта
- Кнопка импорта
- Кнопка фильтров
- Кнопки форм
- Кнопки на странице индекса
- Кнопки элемента
- Кнопки в таблице индекса
- Кнопки на странице формы
- Кнопки на странице детального просмотра
Основы
Кнопки отображаются на страницах ресурса: на странице индекса, страницах форм (создание/редактирование) и странице детального просмотра.
Они отвечают за основные действия с элементами и являются компонентами ActionButton
.
В админ-панели MoonShine существует множество методов, которые позволяют переопределить ресурс как отдельную кнопку, так и всю группу.
Более подробная информация о компоненте ActionButton.
Кнопки для создания, просмотра, редактирования, удаления и массового удаления размещены в отдельных классах, чтобы применить к ним все необходимые методы и тем самым устранить дублирование, поскольку эти кнопки также используются в HasMany, BelongsToMany и т.д.
Кнопка создания
Модификация
Метод modifyCreateButton()
позволяет модифицировать кнопку для создания нового элемента.
use MoonShine\ActionButtons\ActionButton;protected function modifyCreateButton(ActionButton $button): ActionButton{return $button->error();}
use MoonShine\ActionButtons\ActionButton;protected function modifyCreateButton(ActionButton $button): ActionButton{return $button->error();}
Переопределение
Метод getCreateButton()
позволяет переопределить кнопку для создания нового элемента.
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\CreateButton;public function getCreateButton(?string $componentName = null, bool $isAsync = false): ActionButton{return CreateButton::for($this,componentName: $componentName,isAsync: $isAsync);}
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\CreateButton;public function getCreateButton(?string $componentName = null, bool $isAsync = false): ActionButton{return CreateButton::for($this,componentName: $componentName,isAsync: $isAsync);}
Кнопка детального просмотра
Модификация
Метод modifyDetailButton()
позволяет модифицировать кнопку детального просмотра элемента.
use MoonShine\ActionButtons\ActionButton;protected function modifyDetailButton(ActionButton $button): ActionButton{return $button->warning();}
use MoonShine\ActionButtons\ActionButton;protected function modifyDetailButton(ActionButton $button): ActionButton{return $button->warning();}
Переопределение
Метод getDetailButton()
позволяет переопределить кнопку детального просмотра элемента.
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\DetailButton;public function getDetailButton(bool $isAsync = false): ActionButton{return DetailButton::for($this,isAsync: $isAsync);}
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\DetailButton;public function getDetailButton(bool $isAsync = false): ActionButton{return DetailButton::for($this,isAsync: $isAsync);}
Кнопка редактирования
Модификация
Метод modifyEditButton()
позволяет модифицировать кнопку редактирования элемента.
use MoonShine\ActionButtons\ActionButton;protected function modifyEditButton(ActionButton $button): ActionButton{return $button->icon('heroicons.pencil-square');}
use MoonShine\ActionButtons\ActionButton;protected function modifyEditButton(ActionButton $button): ActionButton{return $button->icon('heroicons.pencil-square');}
Переопределение
Метод getEditButton()
позволяет переопределить кнопку редактирования элемента.
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\EditButton;public function getEditButton(?string $componentName = null, bool $isAsync = false): ActionButton{return EditButton::for($this,componentName: $componentName,isAsync: $isAsync);}
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\EditButton;public function getEditButton(?string $componentName = null, bool $isAsync = false): ActionButton{return EditButton::for($this,componentName: $componentName,isAsync: $isAsync);}
Кнопка удаления
Модификация
Метод modifyDeleteButton()
позволяет модифицировать кнопку удаления элемента.
use MoonShine\ActionButtons\ActionButton;protected function modifyDeleteButton(ActionButton $button): ActionButton{return $button->icon('heroicons.x-mark');}
use MoonShine\ActionButtons\ActionButton;protected function modifyDeleteButton(ActionButton $button): ActionButton{return $button->icon('heroicons.x-mark');}
Переопределение
Метод getDeleteButton()
позволяет переопределить кнопку удаления элемента.
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\DeleteButton;public function getDeleteButton(?string $componentName = null,string $redirectAfterDelete = '',bool $isAsync = false): ActionButton {return DeleteButton::for($this,componentName: $componentName,redirectAfterDelete: $isAsync ? '' : $redirectAfterDelete,isAsync: $isAsync);}
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\DeleteButton;public function getDeleteButton(?string $componentName = null,string $redirectAfterDelete = '',bool $isAsync = false): ActionButton {return DeleteButton::for($this,componentName: $componentName,redirectAfterDelete: $isAsync ? '' : $redirectAfterDelete,isAsync: $isAsync);}
Кнопка массового удаления
Модификация
Метод modifyMassDeleteButton()
позволяет модифицировать кнопку массового удаления.
use MoonShine\ActionButtons\ActionButton;protected function modifyMassDeleteButton(ActionButton $button): ActionButton{return $button->icon('heroicons.x-mark');}
use MoonShine\ActionButtons\ActionButton;protected function modifyMassDeleteButton(ActionButton $button): ActionButton{return $button->icon('heroicons.x-mark');}
Переопределение
Метод getMassDeleteButton()
позволяет переопределить кнопку массового удаления.
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\MassDeleteButton;public function getMassDeleteButton(?string $componentName = null,string $redirectAfterDelete = '',bool $isAsync = false): ActionButton {return MassDeleteButton::for($this,componentName: $componentName,redirectAfterDelete: $isAsync ? '' : $redirectAfterDelete,isAsync: $isAsync);}
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\MassDeleteButton;public function getMassDeleteButton(?string $componentName = null,string $redirectAfterDelete = '',bool $isAsync = false): ActionButton {return MassDeleteButton::for($this,componentName: $componentName,redirectAfterDelete: $isAsync ? '' : $redirectAfterDelete,isAsync: $isAsync);}
Кнопка экспорта
Модификация
Метод modifyExportButton()
позволяет модифицировать кнопку экспорта.
use MoonShine\ActionButtons\ActionButton;protected function modifyExportButton(ActionButton $button): ActionButton{return $button->secondary();}
use MoonShine\ActionButtons\ActionButton;protected function modifyExportButton(ActionButton $button): ActionButton{return $button->secondary();}
Переопределение
Метод getExportButton()
позволяет переопределить кнопку экспорта.
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\ExportButton;public function getExportButton(): ActionButton{return ExportButton::for($this, export: $this->export());}
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\ExportButton;public function getExportButton(): ActionButton{return ExportButton::for($this, export: $this->export());}
Кнопка импорта
Модификация
Метод modifyImportButton()
позволяет модифицировать кнопку импорта.
use MoonShine\ActionButtons\ActionButton;protected function modifyImportButton(ActionButton $button): ActionButton{return $button->error();}
use MoonShine\ActionButtons\ActionButton;protected function modifyImportButton(ActionButton $button): ActionButton{return $button->error();}
Переопределение
Метод getImportButton()
позволяет переопределить кнопку импорта.
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\ImportButton;public function getImportButton(): ActionButton{return ImportButton::for($this, import: $this->import());}
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\ImportButton;public function getImportButton(): ActionButton{return ImportButton::for($this, import: $this->import());}
Кнопка фильтров
Модификация
Метод modifyFiltersButton()
позволяет модифицировать кнопку фильтров.
use MoonShine\ActionButtons\ActionButton;protected function modifyFiltersButton(ActionButton $button): ActionButton{return $button->error();}
use MoonShine\ActionButtons\ActionButton;protected function modifyFiltersButton(ActionButton $button): ActionButton{return $button->error();}
Переопределение
Метод getFiltersButton()
позволяет переопределить кнопку фильтров.
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\FiltersButton;public function getFiltersButton(): ActionButton{return FiltersButton::for($this);}
use MoonShine\ActionButtons\ActionButton;use MoonShine\Buttons\FiltersButton;public function getFiltersButton(): ActionButton{return FiltersButton::for($this);}
Кнопки форм
Метод getFormBuilderButtons()
позволяет добавить дополнительные кнопки в форму создания или редактирования.
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function getFormBuilderButtons(): array{return [ActionButton::make('Back', fn() => $this->indexPageUrl())->customAttributes(['class' => 'btn-lg'])];}//...}
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function getFormBuilderButtons(): array{return [ActionButton::make('Back', fn() => $this->indexPageUrl())->customAttributes(['class' => 'btn-lg'])];}//...}
Кнопки на странице индекса
По умолчанию на странице индекса ресурса модели есть только кнопка создания.
Метод actions()
позволяет добавить дополнительные кнопки.
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Enums\JsEvent;use MoonShine\Resources\ModelResource;use MoonShine\Support\AlpineJs;class PostResource extends ModelResource{//...public function actions(): array{return [ActionButton::make('Refresh', '#')->dispatchEvent(AlpineJs::event(JsEvent::TABLE_UPDATED, 'index-table'))];}//...}
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Enums\JsEvent;use MoonShine\Resources\ModelResource;use MoonShine\Support\AlpineJs;class PostResource extends ModelResource{//...public function actions(): array{return [ActionButton::make('Refresh', '#')->dispatchEvent(AlpineJs::event(JsEvent::TABLE_UPDATED, 'index-table'))];}//...}
Кнопки элемента
Метод buttons()
позволяет указать дополнительные кнопки, которые будут отображаться в таблице индекса, в формах создания и редактирования, а также на детальной странице, если они не переопределены для страниц соответствующими методами indexButton()
, formButtons()
и detailButtons()
.
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function buttons(): array{return [ActionButton::make('Link', '/endpoint')];}//...}
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function buttons(): array{return [ActionButton::make('Link', '/endpoint')];}//...}
Кнопки в таблице индекса
Для добавления кнопок в таблицу индекса используйте метод indexButtons()
.
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function indexButtons(): array{return [ActionButton::make('Link',fn(Model $item) => '/endpoint?id=' . $item->getKey())];}//...}
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function indexButtons(): array{return [ActionButton::make('Link',fn(Model $item) => '/endpoint?id=' . $item->getKey())];}//...}
Пример создания пользовательских кнопок для таблицы индекса в разделе Рецепты
Для массовых действий с элементами необходимо добавить метод bulk()
public function indexButtons(): array{return [ActionButton::make('Link', '/endpoint')->bulk()];}
public function indexButtons(): array{return [ActionButton::make('Link', '/endpoint')->bulk()];}
Переопределение группы
Если вы хотите полностью изменить все кнопки элементов в таблице индекса, то необходимо переопределить метод getIndexItemButtons()
в ресурсе.
namespace MoonShine\Resources;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function getIndexItemButtons(): array{return [...$this->getIndexButtons(),$this->getDetailButton(isAsync: $this->isAsync()),$this->getEditButton(isAsync: $this->isAsync()),$this->getDeleteButton(redirectAfterDelete: $this->redirectAfterDelete(),isAsync: $this->isAsync()),$this->getMassDeleteButton(redirectAfterDelete: $this->redirectAfterDelete(),isAsync: $this->isAsync()),];}//...}
namespace MoonShine\Resources;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function getIndexItemButtons(): array{return [...$this->getIndexButtons(),$this->getDetailButton(isAsync: $this->isAsync()),$this->getEditButton(isAsync: $this->isAsync()),$this->getDeleteButton(redirectAfterDelete: $this->redirectAfterDelete(),isAsync: $this->isAsync()),$this->getMassDeleteButton(redirectAfterDelete: $this->redirectAfterDelete(),isAsync: $this->isAsync()),];}//...}
Кнопки на странице формы
Чтобы добавить кнопки на страницу с формой, используйте метод formButtons()
.
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function formButtons(): array{return [ActionButton::make('Link')->method('updateSomething')];}//...}
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function formButtons(): array{return [ActionButton::make('Link')->method('updateSomething')];}//...}
Переопределение группы
Если вы хотите полностью изменить все кнопки элемента на странице формы, то необходимо переопределить метод getFormItemButtons()
в ресурсе.
namespace MoonShine\Resources;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function getFormItemButtons(): array{return [...$this->getFormButtons(),$this->getDetailButton(),$this->getDeleteButton(redirectAfterDelete: $this->redirectAfterDelete(),isAsync: false),];}//...}
namespace MoonShine\Resources;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function getFormItemButtons(): array{return [...$this->getFormButtons(),$this->getDetailButton(),$this->getDeleteButton(redirectAfterDelete: $this->redirectAfterDelete(),isAsync: false),];}//...}
Кнопки на странице детального просмотра
Чтобы добавить кнопки на страницу детального просмотра, используйте метод detailButtons()
.
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function detailButtons(): array{return [ActionButton::make('Link', '/endpoint')];}//...}
namespace MoonShine\Resources;use MoonShine\ActionButtons\ActionButton;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function detailButtons(): array{return [ActionButton::make('Link', '/endpoint')];}//...}
Переопределение группы
Если вы хотите полностью изменить все кнопки элемента на странице детального просмотра, то необходимо переопределить метод getDetailItemButtons()
в ресурсе.
namespace MoonShine\Resources;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function getDetailItemButtons(): array{return [...$this->getDetailButtons(),$this->getEditButton(isAsync: $this->isAsync(),),$this->getDeleteButton(redirectAfterDelete: $this->redirectAfterDelete(),isAsync: false),];}//...}
namespace MoonShine\Resources;use MoonShine\Resources\ModelResource;class PostResource extends ModelResource{//...public function getDetailItemButtons(): array{return [...$this->getDetailButtons(),$this->getEditButton(isAsync: $this->isAsync(),),$this->getDeleteButton(redirectAfterDelete: $this->redirectAfterDelete(),isAsync: false),];}//...}