table.blade.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <div class="table-responsive">
  2. <div class="table-buttons py-2 bg-primary rounded-start d-flex flex-column ">
  3. <button type="button" class="btn btn-sm text-white" data-bs-toggle="modal" data-bs-target="#table_{{ $id }}_modal_search">
  4. <i class="bi bi-search"></i>
  5. </button>
  6. <button type="button" class="btn btn-sm text-white " data-bs-toggle="modal" data-bs-target="#table_{{ $id }}_modal_filters">
  7. <i class="bi bi-funnel-fill"></i>
  8. </button>
  9. <button type="button" class="btn btn-sm text-white " data-bs-toggle="modal" data-bs-target="#table_{{ $id }}_modal_settings">
  10. <i class="bi bi-gear-fill"></i>
  11. </button>
  12. </div>
  13. <table class="table" id="tbl" data-table-name="{{ $id }}" style="display: none;">
  14. <thead>
  15. <tr>
  16. @foreach($header as $headerName => $headerTitle)
  17. <th scope="col" class="bg-primary-subtle column_{{ $headerName }}">
  18. <span class="cursor-pointer sort-by-column" data-name="{{ $headerName }}">
  19. {{ $headerTitle }}
  20. @if(str_starts_with($headerName, $sortBy))
  21. @if($orderBy === 'asc')
  22. <i class="bi bi-arrow-up"></i>
  23. @else
  24. <i class="bi bi-arrow-down"></i>
  25. @endif
  26. @endif
  27. @if(isset(request()->filters[$headerName]) ||
  28. isset(request()->filters[str_replace('_txt', '', $headerName) . '_from']) ||
  29. isset(request()->filters[str_replace('_txt', '', $headerName) . '_to'])
  30. )
  31. <i class="bi bi-funnel"></i>
  32. @endif
  33. </span>
  34. </th>
  35. @endforeach
  36. </tr>
  37. </thead>
  38. <tbody>
  39. @foreach($products as $product)
  40. <tr>
  41. @foreach($header as $headerName => $headerTitle)
  42. <td class="column_{{$headerName}}">{!! $product->$headerName !!}</td>
  43. @endforeach
  44. </tr>
  45. @endforeach
  46. </tbody>
  47. </table>
  48. </div>
  49. <!-- Модальное окно настроек таблицы -->
  50. <div class="modal fade" id="table_{{ $id }}_modal_settings" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  51. <div class="modal-dialog modal-fullscreen-sm-down">
  52. <div class="modal-content">
  53. <div class="modal-header">
  54. <h1 class="modal-title fs-5" id="exampleModalLabel">Выбор отображаемых колонок</h1>
  55. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  56. </div>
  57. <div class="modal-body">
  58. @foreach($header as $headerName => $headerTitle)
  59. <div>
  60. <label class="me-3"><input type="checkbox" checked="checked" data-name="{{ $headerName }}" class="toggle-column checkbox-{{ $headerName }}"> {{ $headerTitle }}</label>
  61. </div>
  62. @endforeach
  63. </div>
  64. <div class="modal-footer">
  65. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. <!-- Модальное окно фильтров -->
  71. <div class="modal fade" id="table_{{ $id }}_modal_filters" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  72. <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
  73. <div class="modal-content">
  74. <div class="modal-header">
  75. <h1 class="modal-title fs-5" id="exampleModalLabel">Фильтры по колонкам таблицы</h1>
  76. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  77. </div>
  78. <div class="modal-body">
  79. <form class="filters">
  80. @foreach($filters as $filterName => $filter)
  81. @php array_unshift($filter['values'], '') @endphp
  82. @include('partials.select', [
  83. 'name' => 'filters[' . $filterName . ']',
  84. 'title' => $filter['title'],
  85. 'options' => $filter['values'],
  86. 'value' => request()->filters[$filterName] ?? '',
  87. 'key_as_val' => true
  88. ])
  89. @endforeach
  90. @foreach($ranges as $rangeName => $range)
  91. @include('partials.input', [
  92. 'name' => 'filters[' . $rangeName . '_from]',
  93. 'type' => 'number',
  94. 'title' => $range['title'] . ' с:',
  95. 'min' => $range['min'],
  96. 'max' => $range['max'],
  97. 'value' => request()->filters[$rangeName . '_from'] ?? '', // $range['min']
  98. ])
  99. @include('partials.input', [
  100. 'name' => 'filters[' . $rangeName . '_to]',
  101. 'type' => 'number',
  102. 'title' => ' по:',
  103. 'min' => $range['min'],
  104. 'max' => $range['max'],
  105. 'value' => request()->filters[$rangeName . '_to'] ?? '', // $range['max']
  106. ])
  107. @endforeach
  108. @foreach($dates as $rangeName => $range)
  109. @include('partials.input', [
  110. 'name' => 'filters[' . $rangeName . '_from]',
  111. 'type' => 'date',
  112. 'title' => $range['title'] . ' с:',
  113. 'min' => $range['min'],
  114. 'max' => $range['max'],
  115. 'value' => request()->filters[$rangeName . '_from'] ?? '',
  116. ])
  117. @include('partials.input', [
  118. 'name' => 'filters[' . $rangeName . '_to]',
  119. 'type' => 'date',
  120. 'title' => $range['title'] . ' по:',
  121. 'min' => $range['min'],
  122. 'max' => $range['max'],
  123. 'value' => request()->filters[$rangeName . '_to'] ?? '',
  124. ])
  125. @endforeach
  126. </form>
  127. </div>
  128. <div class="modal-footer">
  129. <button type="button" class="btn btn-primary accept-filters" data-bs-dismiss="modal">Применить</button>
  130. <button type="button" class="btn btn-outline-secondary reset-filters" data-bs-dismiss="modal">Сбросить</button>
  131. </div>
  132. </div>
  133. </div>
  134. </div>
  135. <!-- Модальное окно поиска -->
  136. <div class="modal fade" id="table_{{ $id }}_modal_search" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  137. <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
  138. <div class="modal-content">
  139. <div class="modal-header">
  140. <h1 class="modal-title fs-5" id="exampleModalLabel">Поиск</h1>
  141. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  142. </div>
  143. <div class="modal-body">
  144. <div>
  145. Поиск ведётся по следующим колонкам:
  146. <span class="fst-italic">
  147. @foreach($searchFields as $searchField)
  148. {{ $header[$searchField] }}
  149. @if(!$loop->last),@endif
  150. @endforeach
  151. </span>
  152. </div>
  153. <form class="search-form">
  154. @include('partials.input', [
  155. 'name' => 's',
  156. 'title' => 'Поиск',
  157. 'placeholder' => 'что ищем?',
  158. 'value' => request()->s ?? '',
  159. ])
  160. </form>
  161. </div>
  162. <div class="modal-footer">
  163. <button type="button" class="btn btn-primary accept-search" data-bs-dismiss="modal">Найти</button>
  164. <button type="button" class="btn btn-outline-secondary reset-search" data-bs-dismiss="modal">Сбросить</button>
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. @push('scripts')
  170. <script type="module">
  171. // on page load set column visible
  172. let tableName = $('#tbl').attr('data-table-name');
  173. let tables = JSON.parse(localStorage.getItem('table_'+tableName));
  174. // on first load create tables object
  175. if(!tables) { tables = {}; }
  176. $.each(tables, function (colName, colStatus) {
  177. if(!colStatus) {
  178. $('.checkbox-'+colName).attr('checked', false);
  179. $('.column_'+colName).hide();
  180. }
  181. });
  182. $('.table').fadeIn();
  183. $('.toggle-column').on('change', function () {
  184. let columnName = $(this).attr('data-name');
  185. let columnStatus = $(this).is(':checked');
  186. // save column status
  187. tables[columnName] = columnStatus;
  188. localStorage.setItem('table_'+tableName, JSON.stringify(tables));
  189. // show or hide column
  190. if(columnStatus) {
  191. $('.column_'+columnName).show('fast');
  192. } else {
  193. $('.column_'+columnName).hide('fast');
  194. }
  195. });
  196. $('.sort-by-column').on('click', function () {
  197. let columnName = $(this).attr('data-name');
  198. let currentUrl = new URL(document.location.href);
  199. let currentColumnName = currentUrl.searchParams.get('sortBy');
  200. currentUrl.searchParams.set('sortBy', columnName);
  201. if((currentColumnName !== columnName) || (currentUrl.searchParams.has('order'))) {
  202. currentUrl.searchParams.delete('order');
  203. } else {
  204. currentUrl.searchParams.set('order', 'desc');
  205. }
  206. document.location.href = currentUrl.href;
  207. });
  208. $('.accept-filters').on('click', function () {
  209. let filters = $('.filters').serializeArray();
  210. let currentUrl = new URL(document.location.href);
  211. $.each(filters, function (id, filter) {
  212. if(filter.value !== '') {
  213. currentUrl.searchParams.set(filter.name, filter.value);
  214. } else {
  215. currentUrl.searchParams.delete(filter.name);
  216. }
  217. });
  218. currentUrl.searchParams.delete('page');
  219. document.location.href = currentUrl.href;
  220. });
  221. $('.reset-filters').on('click', function () {
  222. let filters = $('.filters').serializeArray();
  223. let currentUrl = new URL(document.location.href);
  224. $.each(filters, function (id, filter) {
  225. currentUrl.searchParams.delete(filter.name);
  226. });
  227. currentUrl.searchParams.delete('page');
  228. document.location.href = currentUrl.href;
  229. });
  230. $('.accept-search').on('click', function () {
  231. let s = $('.search-form input').val();
  232. let currentUrl = new URL(document.location.href);
  233. if(s !== '') {
  234. currentUrl.searchParams.set('s', s);
  235. } else {
  236. currentUrl.searchParams.delete('s');
  237. }
  238. currentUrl.searchParams.delete('page');
  239. document.location.href = currentUrl.href;
  240. });
  241. $('.reset-search').on('click', function () {
  242. let currentUrl = new URL(document.location.href);
  243. currentUrl.searchParams.delete('s');
  244. currentUrl.searchParams.delete('page');
  245. document.location.href = currentUrl.href;
  246. });
  247. </script>
  248. @endpush