ExportButton.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Grid;
  5. use Illuminate\Contracts\Support\Renderable;
  6. class ExportButton implements Renderable
  7. {
  8. /**
  9. * @var Grid
  10. */
  11. protected $grid;
  12. /**
  13. * Create a new Export button instance.
  14. *
  15. * @param Grid $grid
  16. */
  17. public function __construct(Grid $grid)
  18. {
  19. $this->grid = $grid;
  20. }
  21. /**
  22. * Set up script for export button.
  23. */
  24. protected function setUpScripts()
  25. {
  26. $script = <<<JS
  27. $('.{$this->grid->getExportSelectedName()}').on('click', function (e) {
  28. e.preventDefault();
  29. var rows = Dcat.grid.selected('{$this->grid->getName()}').join(',');
  30. if (! rows) {
  31. return false;
  32. }
  33. var href = $(this).attr('href').replace('__rows__', rows);
  34. location.href = href;
  35. });
  36. JS;
  37. Admin::script($script);
  38. }
  39. /**
  40. * @return string|void
  41. */
  42. protected function renderExportAll()
  43. {
  44. if (! $this->grid->exporter()->option('show_export_all')) {
  45. return;
  46. }
  47. $all = trans('admin.all');
  48. return "<li class='dropdown-item'><a href=\"{$this->grid->exportUrl('all')}\" target=\"_blank\">{$all}</a></li>";
  49. }
  50. /**
  51. * @return string
  52. */
  53. protected function renderExportCurrentPage()
  54. {
  55. if (! $this->grid->exporter()->option('show_export_current_page')) {
  56. return;
  57. }
  58. $page = $this->grid->model()->getCurrentPage() ?: 1;
  59. $currentPage = trans('admin.current_page');
  60. return "<li class='dropdown-item'><a href=\"{$this->grid->exportUrl('page', $page)}\" target=\"_blank\">{$currentPage}</a></li>";
  61. }
  62. /**
  63. * @return string|void
  64. */
  65. protected function renderExportSelectedRows()
  66. {
  67. if (
  68. ! $this->grid->option('show_row_selector')
  69. || ! $this->grid->exporter()->option('show_export_selected_rows')
  70. ) {
  71. return;
  72. }
  73. $selectedRows = trans('admin.selected_rows');
  74. return "<li class='dropdown-item'><a href=\"{$this->grid->exportUrl('selected', '__rows__')}\" target=\"_blank\" class='{$this->grid->getExportSelectedName()}'>{$selectedRows}</a></li>";
  75. }
  76. /**
  77. * Render Export button.
  78. *
  79. * @return string
  80. */
  81. public function render()
  82. {
  83. $this->setUpScripts();
  84. $export = trans('admin.export');
  85. return <<<EOT
  86. <div class="btn-group dropdown" style="margin-right:3px">
  87. <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
  88. <i class="feather icon-download"></i>
  89. <span class="d-none d-sm-inline">&nbsp;{$export}&nbsp;</span>
  90. <span class="caret"></span>
  91. <span class="sr-only"></span>
  92. </button>
  93. <ul class="dropdown-menu" role="menu">
  94. {$this->renderExportAll()}
  95. {$this->renderExportCurrentPage()}
  96. {$this->renderExportSelectedRows()}
  97. </ul>
  98. </div>
  99. EOT;
  100. }
  101. }