QuickSearch.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Admin;
  4. use Illuminate\Support\Arr;
  5. class QuickSearch extends AbstractTool
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $view = 'admin::grid.quick-search';
  11. /**
  12. * @var string
  13. */
  14. protected $placeholder = null;
  15. /**
  16. * @var string
  17. */
  18. protected $queryName = '__search__';
  19. /**
  20. * @var string
  21. */
  22. protected $id;
  23. /**
  24. * @var int rem
  25. */
  26. protected $width = 29;
  27. public function __construct($key = null, $title = null)
  28. {
  29. parent::__construct($key, $title);
  30. }
  31. /**
  32. * @param string|null $name
  33. *
  34. * @return $this
  35. */
  36. public function setQueryName(?string $name)
  37. {
  38. $this->id = 'grid-quick-search-'.$name;
  39. $this->queryName = $name;
  40. return $this;
  41. }
  42. /**
  43. * @return string
  44. */
  45. public function queryName()
  46. {
  47. return $this->queryName;
  48. }
  49. /**
  50. * @param int $width
  51. *
  52. * @return $this
  53. */
  54. public function width(int $width)
  55. {
  56. $this->width = $width;
  57. return $this;
  58. }
  59. /**
  60. * Set placeholder.
  61. *
  62. * @param string $text
  63. *
  64. * @return $this
  65. */
  66. public function placeholder(?string $text = '')
  67. {
  68. $this->placeholder = $text;
  69. return $this;
  70. }
  71. /**
  72. * @return string
  73. */
  74. public function value()
  75. {
  76. return trim(request($this->queryName));
  77. }
  78. /**
  79. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  80. */
  81. public function render()
  82. {
  83. $request = request();
  84. $query = $request->query();
  85. $this->setupScript();
  86. Arr::forget($query, [
  87. $this->queryName,
  88. $this->parent->model()->getPageName(),
  89. '_pjax',
  90. ]);
  91. $vars = [
  92. 'action' => $request->url().'?'.http_build_query($query),
  93. 'key' => $this->queryName,
  94. 'value' => $this->value(),
  95. 'placeholder' => $this->placeholder ?: trans('admin.search'),
  96. 'width' => $this->width,
  97. 'id' => $this->id,
  98. ];
  99. return view($this->view, $vars);
  100. }
  101. protected function setupScript()
  102. {
  103. $script = <<<'JS'
  104. (function () {
  105. var show = function () {
  106. var t = $(this),
  107. clear = t.parent().find('.quick-search-clear');
  108. if (t.val()) {
  109. clear.css({color: '#333'});
  110. } else {
  111. clear.css({color: '#fff'});
  112. }
  113. return false;
  114. };
  115. var request = LA.debounce(function (input) {
  116. $(input).parents('form').submit()
  117. }, 500);
  118. var $input = $('input.quick-search-input');
  119. $input.on('focus', show).on('keyup', function () {
  120. show.apply(this);
  121. request(this);
  122. });
  123. var val = $input.val();
  124. val !== '' && $input.val('').focus().val(val);
  125. $('.quick-search-clear').click(function () {
  126. $(this).parent().find('.quick-search-input').val('');
  127. $(this).closest('form').submit();
  128. });
  129. })()
  130. JS;
  131. Admin::script($script);
  132. }
  133. }