Basic use of motion

The Action action class makes it very easy for developers to develop an action that contains a specific function that allows the user to interact with the server very easily.

For example, if there is a button on a page, and user clicks on it to request the server to display the information of the logged in user in a popup window, then this button can be developed by Action.

Example

Let’s start developing a button for viewing logged-in user information.

Creating an Action class with a command

First, you need to create the Action class and run the command

  1. php artisan admin:action

After successful execution, you will see the following message in the command window, asking the developer to select an Action class type, here we type 0.

{tip} An action class of type default can be used anywhere on the page.

  1. Which type of action would you like to make?:
  2. [0] default
  3. [1] grid-batch
  4. [2] grid-row
  5. [3] grid-tool
  6. [4] form-tool
  7. [5] show-tool
  8. [6] tree-tool
  9. > 0 # Enter 0

Next, type in the name of the Action class in PascalCase style.

  1. Please enter a name of action class:
  2. > ShowCurrentAdminUser

The default namespace is App\Admin\Actions, here we just press Enter to skip it.

  1. Please enter the namespace of action class [App\Admin\Actions]:
  2. >

So an Action class is created, and the path of the class just created is app/Admin/Actions/ShowCurrentAdminUser.php.

Use

Modify the Action class as follows

{tip} If you need to pass arguments via a constructor in your action class, be sure to set a default value for all arguments to the constructor!

  1. <?php
  2. namespace App\Admin\Actions;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Widgets\Table;
  5. use Dcat\Admin\Actions\Action;
  6. use Dcat\Admin\Actions\Response;
  7. use Dcat\Admin\Traits\HasPermissions;
  8. use Illuminate\Contracts\Auth\Authenticatable;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Http\Request;
  11. class ShowCurrentAdminUser extends Action
  12. {
  13. /**
  14. * Button Title
  15. *
  16. * @var string
  17. */
  18. protected $title = 'Personal Information';
  19. /**
  20. * @var string
  21. */
  22. protected $modalId = 'show-current-user';
  23. /**
  24. * If you don't need it, please delete it.
  25. *
  26. * @param Request $request
  27. *
  28. * @return Response
  29. */
  30. public function handle(Request $request)
  31. {
  32. // Get the currently logged in user model
  33. $user = Admin::user();
  34. // Here we present the model data in a table
  35. $table = Table::make($user->toArray());
  36. return $this->response()
  37. ->success('Search Success')
  38. ->html($table);
  39. }
  40. /**
  41. * Processes the HTML string of the response and appends it to the popup node.
  42. *
  43. * @return string
  44. */
  45. protected function handleHtmlResponse()
  46. {
  47. return <<<'JS'
  48. function (target, html, data) {
  49. var $modal = $(target.data('target'));
  50. $modal.find('.modal-body').html(html)
  51. $modal.modal('show')
  52. }
  53. JS;
  54. }
  55. /**
  56. * Setting the properties of HTML tags
  57. *
  58. * @return void
  59. */
  60. protected function setupHtmlAttributes()
  61. {
  62. // Add class
  63. $this->addHtmlClass('btn btn-primary');
  64. // ID of the saved popup
  65. $this->setHtmlAttribute('data-target', '#'.$this->modalId);
  66. parent::setupHtmlAttributes();
  67. }
  68. /**
  69. * To set the HTML of the button, here we need to attach the HTML of the popup window.
  70. *
  71. * @return string|void
  72. */
  73. public function html()
  74. {
  75. // Button's html
  76. $html = parent::html();
  77. return <<<HTML
  78. {$html}
  79. <div class="modal fade" id="{$this->modalId}" tabindex="-1" role="dialog">
  80. <div class="modal-dialog modal-lg" role="document">
  81. <div class="modal-content">
  82. <div class="modal-header">
  83. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  84. <h4 class="modal-title">{$this->title()}</h4>
  85. </div>
  86. <div class="modal-body"></div>
  87. </div>
  88. </div>
  89. </div>
  90. HTML;
  91. }
  92. /**
  93. * Confirm the pop-up message, and delete this method if you don't need it.
  94. *
  95. * @return string|void
  96. */
  97. public function confirm()
  98. {
  99. // return ['Confirm?', 'contents'];
  100. }
  101. /**
  102. * Action permission, return false means no permission, if you don't need it, you can delete this method.
  103. *
  104. * @param Model|Authenticatable|HasPermissions|null $user
  105. *
  106. * @return bool
  107. */
  108. protected function authorize($user): bool
  109. {
  110. return true;
  111. }
  112. /**
  113. * This method allows you to set the parameters to be attached to an action request.
  114. *
  115. * @return array
  116. */
  117. protected function parameters()
  118. {
  119. return [];
  120. }
  121. }

After modifying it, you can start using it.

  1. <?php
  2. use App\Admin\Actions\ShowCurrentAdminUser;
  3. class IndexController
  4. {
  5. public function index(Content $content)
  6. {
  7. return $content->body(ShowCurrentAdminUser::make());
  8. }
  9. }

The results are as follows

Basic use of motion - 图1

Properties

Dcat\Admin\Actions\Action Description of Available Class Properties

attribute name type default description
title string TITLE
selectorPrefix public string .admin-action- Css selector for the target element
method string POST Request methods for interaction with the server
event string click Events bound to the target element, default to click events.
disabled bool false If or not to render the action element, set true to not render the action element
usingHandler bool true When this attribute is set to false, no request is made to the server regardless of whether Action contains the handle method or not!

Methods

Description of the Dcat\Admin\Actions\Action class methodology

Create an instance (make)

This method is a static method that instantiates the action class

  1. $action = MyAction::make($param1, $param2...);

Process request (handle)

When this method is included in the Action class, the target element is bound to an event set by the event attribute (default is click). If the event is triggered, a request is made to the server, and the handle method handles and responds to the request.

{tip} The handle method handles and responds to the request. If there is no such method, the target element is not bound to the event.

Respond (response)

Refer to Actions and Form Responses chapter

Set request data (parameters)

This method allows you to set the parameters that should be attached to the request when the action is initiated.

  1. <?php
  2. use Dcat\Admin\Actions\Action;
  3. use Illuminate\Http\Request;
  4. class MyAction extends Action
  5. {
  6. public function handle(Request $request)
  7. {
  8. // Receiving parameters
  9. $key1 = $request->get('key1');
  10. $key2 = $request->get('key2');
  11. return $this->response()->success('success!');
  12. }
  13. public function parameters()
  14. {
  15. return [
  16. 'key1' => 'value1',
  17. 'key2' => 'value2',
  18. ];
  19. }
  20. }

Confirm popups

This method requires a string parameter to be returned.

A confirmation window is added when the return value of this method is not null, and a confirmation box pops up automatically when the event is triggered.

  1. public function confirm()
  2. {
  3. return 'Are you sure you want to remove this line of content?';
  4. }

Show popup title and content

  1. public function confirm()
  2. {
  3. return ['Are you sure you want to delete this line of content?' , 'pop-up content'];
  4. }

JS code that is executed before the request is initiated (actionScript)

The js code preceding the execution of the set action executes the js code set by this method before initiating the request when the button-bound event is triggered, and this method requires a js anonymous function to be returned.

The js anonymous function takes the following three arguments:

  • data object The data object to be passed to the interface.
  • target object Action button’s jQuery object
  • action object Action management object.
  1. protected function actionScript()
  2. {
  3. return <<<JS
  4. function (data, target, action) {
  5. console.log('Before initiating a request', data, target, action);
  6. // return false; Here return false to stop the rest of the operation.
  7. // Changing the primary key value passed to the interface
  8. action.options.key = 123;
  9. }
  10. JS
  11. }

HTML code to handle the server response (handleHtmlResponse)

Processes the HTML code of the server response, which requires a js anonymous function to be returned.

  1. protected function handleHtmlResponse()
  2. {
  3. return <<<'JS'
  4. function (target, html, data) {
  5. // target JQ object of the action button
  6. // html HTML string returned by the interface
  7. // data json object of the complete data returned by the interface.
  8. target.html(html);
  9. }
  10. JS;
  11. }

Permissions (authorize)

This method is used to determine the privileges of the logged in user, the default is true

  1. protected function authorize($user): bool
  2. {
  3. return $user->can('do-action');
  4. }

No Authority Response (failedAuthorization)

This method is used to set the response to an authentication failure and can be overridden if desired

  1. public function failedAuthorization()
  2. {
  3. return $this->response()->error(__('admin.deny'));
  4. }

Hide or show (disable)

Set to show or hide this action

  1. // hide
  2. MyAction::make()->disable();
  3. // show
  4. MyAction::make()->disable(false);

Determine whether or not to show (allowed)

Determine whether the action is allowed to be displayed

  1. if (MyAction::make()->allowed()) {
  2. ...
  3. }

setKey

Setting the Data Primary Key

  1. $id = ...;
  2. MyAction::make()->setKey($id);

Get the primary key value (getKey)

Gets the data primary key, also available in the handle method

  1. <?php
  2. use Dcat\Admin\Actions\Action;
  3. use Illuminate\Http\Request;
  4. class MyAction extends Action
  5. {
  6. public function handle(Request $request)
  7. {
  8. $id = $this->getKey();
  9. ...
  10. return $this->response()->success('msng!');
  11. }
  12. public function title()
  13. {
  14. return "TITLE {$this->key()}";
  15. }
  16. }

Get target element style (getElementClass)

Get the class of the action target element (button)

  1. $class = MyAction::make()->getElementClass();

Get the Css selector for the target element

Get the CSS selector for the action target element (button)

  1. $selector = MyAction::make()->selector();
  2. Admin::script(
  3. <<<JS
  4. $('$selector').click(...);
  5. JS
  6. );

Add style (addHtmlClass)

Append the class of the action target element (button)

  1. MyAction::make()->addHtmlClass('btn btn-primary');
  2. MyAction::make()->addHtmlClass(['btn', 'btn-primary']);

Set the HTML (html) of the target element

This method is used to set the HTML code of the action target element, overriding it if necessary

  1. protected function html()
  2. {
  3. return <<<HTML
  4. <a {$this->formatHtmlAttributes()}>{$this->title()}</a>
  5. HTML;
  6. }

Adding JS code (script)

This method is used to add the JS code before the render method finishes executing

  1. protected function script()
  2. {
  3. return <<<JS
  4. console.log('...')
  5. JS;
  6. }

Set the HTML attribute of the target element (setHtmlAttribute)

Set the HTML tag property of the target element

  1. MyAction::make()->setHtmlAttribute('name', $value);
  2. // batch setting
  3. MyAction::make()->setHtmlAttribute(['name' => $value]);