Views and custom pages

View

In the Dcat Admin we can use the admin_view function to render the view, this function draws on the design ideas of vue, you can write HTML, CSS and JS code in the same template file, so that the code is more clear and more concise and easy to read layered code, such as

  1. <div class="my-class">...</div>
  2. <style>
  3. .my-class {
  4. color: blue;
  5. }
  6. </style>
  7. <script require="@test1,@test2" init=".my-class">
  8. $this.css({background: 'red'})
  9. </script>

Render the view in php

  1. public function index(Content $content)
  2. {
  3. return $content->body(admin_view('...'));
  4. }

Explanation of Example

The code in the above example is actually equivalent to the following code

  1. <div class="my-class">...</div>
  1. public function index(Content $content)
  2. {
  3. admin_require_assets(['@test1', '@test2']);
  4. admin_style('.my-class {
  5. color: blue;
  6. }');
  7. admin_script(
  8. <<<JS
  9. Dcat.init('.my-class', function (\$this, id) {
  10. \$this.css({background: 'red'})
  11. });
  12. JS
  13. );
  14. return $content->body(view('...'));
  15. }

Obviously, using admin_view to render the view will make your code much easier to read. For Dcat.init and the use of the init and require attributes in the script tag, please refer to the documentation [Static Resources] (assets.md) and [Dynamic Listening Element Generation (init)] (js.md #init) chapters.

Custom pages

Building a custom page in Dcat Admin is very simple, you can refer to the following two examples

Example 1

{tip} Dcat Admin is a single page application and the loaded JS script will only be executed once, so the initialization operation cannot be placed directly in the JS script, it should be loaded using the Admin::script method.

  1. <?php
  2. use Illuminate\Contracts\Support\Renderable;
  3. use Dcat\Admin\Admin;
  4. class MyPage implements Renderable
  5. {
  6. // Define the static resources needed for the page, which will be loaded here on demand.
  7. public static $js = [
  8. // The js script cannot contain the initialization operation directly, otherwise the page refresh is invalid.
  9. 'xxx/js/page.min.js',
  10. ];
  11. public static $css = [
  12. 'xxx/css/page.min.css',
  13. ];
  14. public function script()
  15. {
  16. return <<<JS
  17. console.log('All JS scripts are loaded');
  18. // The initialization operation is written here.
  19. JS;
  20. }
  21. public function render()
  22. {
  23. // This is where you can bring in your js or css files.
  24. Admin::js(static::$js);
  25. Admin::css(static::$css);
  26. // JS code to be executed on the page
  27. // The JS code set by Admin::script will be executed automatically when all JS scripts are loaded.
  28. Admin::script($this->script());
  29. return view('admin.pages.my-page')->render();
  30. }
  31. }

View admin.pages.my-page, make sure the view code does not contain tags such as <body> and <html>.

  1. <div>
  2. <h3>自定义页面演示</h3>
  3. </div>
  4. <script>
  5. Dcat.ready(function () {
  6. // js代码也可以放在模板里面
  7. console.log('所有JS脚本都加载完了!!!');
  8. });
  9. </script>

Usage

  1. public function index(Content $content)
  2. {
  3. return $content->body(new MyPage());
  4. }

Example 2

The dashboard page /admin in the background can also be seen as a custom page, the code implementation is as follows

  1. public function index(Content $content)
  2. {
  3. return $content
  4. ->header('Dashboard')
  5. ->description('Description...')
  6. ->body(function (Row $row) {
  7. $row->column(6, function (Column $column) {
  8. $column->row(Dashboard::title());
  9. $column->row(new Examples\Tickets());
  10. });
  11. $row->column(6, function (Column $column) {
  12. $column->row(function (Row $row) {
  13. $row->column(6, new Examples\NewUsers());
  14. $row->column(6, new Examples\NewDevices());
  15. });
  16. $column->row(new Examples\Sessions());
  17. $column->row(new Examples\ProductOrders());
  18. });
  19. });
  20. }