Head and feet

The data table supports filling the head and foot blocks as you wish.

  1. $grid->header(function ($collection) {
  2. return 'header';
  3. });
  4. $grid->footer(function ($collection) {
  5. return 'footer';
  6. });

The parameter $collection of the closure function is Illuminate\Support\Collection class instance, which is the current page table data, the following is an example of the use of two different scenarios

Head

  1. $grid->header(function ($collection) use ($grid) {
  2. $query = Model::query();
  3. // Get the table filter where the conditional array is traversed.
  4. $grid->model()->getQueries()->unique()->each(function ($value) use (&$query) {
  5. if (in_array($value['method'], ['paginate', 'get', 'orderBy', 'orderByDesc'], true)) {
  6. return;
  7. }
  8. $query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []);
  9. });
  10. // Identify statistical data
  11. $data = $query->get();
  12. // Custom component
  13. return new Card($data);
  14. });

Component implementation for custom header display

  1. <?php
  2. use Illuminate\Contracts\Support\Renderable;
  3. use Dcat\Admin\Admin;
  4. class Card implements Renderable
  5. {
  6. public static $js = [
  7. 'xxx/js/card.min.js',
  8. ];
  9. public static $css = [
  10. 'xxx/css/card.min.css',
  11. ];
  12. protected $data;
  13. public function __construct($data)
  14. {
  15. $this->data = $data;
  16. }
  17. public function script()
  18. {
  19. return <<<JS
  20. console.log('All JS scripts are loaded.');
  21. $('xxx').card();
  22. JS;
  23. }
  24. public function render()
  25. {
  26. // You can introduce your js or css file here.
  27. Admin::js(static::$js);
  28. Admin::css(static::$css);
  29. // JS code to be executed on the page
  30. // The JS code set by Admin::script will be executed automatically when all JS scripts are loaded.
  31. Admin::script($this->script());
  32. return view('...', ['data' => $this->data])->render();
  33. }
  34. }

Footer

A more common scenario is to display statistics in the footer of a data form, such as adding revenue statistics to the footer of an order form, which can be implemented with the following code.

  1. $grid->footer(function ($collection) use ($grid) {
  2. $query = Model::query();
  3. // Get the table to filter the where condition array for iterations
  4. $grid->model()->getQueries()->unique()->each(function ($value) use (&$query) {
  5. if (in_array($value['method'], ['paginate', 'get', 'orderBy', 'orderByDesc'], true)) {
  6. return;
  7. }
  8. $query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []);
  9. });
  10. // Identifying statistics
  11. $data = $query->get();
  12. return "<div style='padding: 10px;'>gross income : $data</div>";
  13. });

If there is a more complex footer that needs to be displayed, you can also use a view object or wrap it into a class.