Checkbox.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Admin;
  4. use Illuminate\Contracts\Support\Arrayable;
  5. class Checkbox extends AbstractDisplayer
  6. {
  7. public function display($options = [])
  8. {
  9. if ($options instanceof \Closure) {
  10. $options = $options->call($this, $this->row);
  11. }
  12. $checkboxes = '';
  13. $name = $this->column->getName();
  14. if (is_string($this->value)) {
  15. $this->value = explode(',', $this->value);
  16. }
  17. if ($this->value instanceof Arrayable) {
  18. $this->value = $this->value->toArray();
  19. }
  20. foreach ($options as $value => $label) {
  21. $checked = in_array($value, $this->value) ? 'checked' : '';
  22. $checkboxes .= <<<EOT
  23. <div class="vs-checkbox-con vs-checkbox-primary" style="margin-bottom: 4px">
  24. <input type="checkbox" name="grid-checkbox-{$name}[]" value="{$value}" $checked >
  25. <span class="vs-checkbox vs-checkbox-sm"><span class="vs-checkbox--check"><i class="vs-icon feather icon-check"></i></span></span>
  26. <span class="">{$label}</span>
  27. </div>
  28. EOT;
  29. }
  30. Admin::script($this->script());
  31. return <<<EOT
  32. <form class="form-group {$this->getElementClass()}" style="text-align:left;" data-key="{$this->getKey()}">
  33. $checkboxes
  34. <button type="submit" class="btn btn-primary btn-sm pull-left">
  35. <i class="feather icon-save"></i>&nbsp;{$this->trans('save')}
  36. </button>
  37. <button type="reset" class="btn btn-white btn-sm pull-left" style="margin-left:5px;">
  38. <i class="feather icon-trash"></i>&nbsp;{$this->trans('reset')}
  39. </button>
  40. </form>
  41. EOT;
  42. }
  43. protected function getElementClass()
  44. {
  45. return 'grid-checkbox-'.$this->column->getName();
  46. }
  47. protected function script()
  48. {
  49. return <<<JS
  50. (function () {
  51. var f;
  52. $('form.{$this->getElementClass()}').off('submit').on('submit', function () {
  53. var values = $(this).find('input:checkbox:checked').map(function (_, el) {
  54. return $(el).val();
  55. }).get(), btn = $(this).find('[type="submit"]');
  56. if (f) return;
  57. f = 1;
  58. btn.buttonLoading();
  59. var data = {
  60. {$this->column->getName()}: values,
  61. _token: Dcat.token,
  62. _method: 'PUT'
  63. };
  64. $.ajax({
  65. url: "{$this->resource()}/" + $(this).data('key'),
  66. type: "POST",
  67. contentType: 'application/json;charset=utf-8',
  68. data: JSON.stringify(data),
  69. success: function (data) {
  70. btn.buttonLoading(false);
  71. f = 0;
  72. Dcat.success(data.message);
  73. },
  74. error: function (a, b, c) {
  75. btn.buttonLoading(false);
  76. f = 0;
  77. Dcat.handleAjaxError(a, b, c);
  78. },
  79. });
  80. return false;
  81. });
  82. })();
  83. JS;
  84. }
  85. }