table.blade.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 tbl = $('#tbl');
  173. let tableName = tbl.attr('data-table-name');
  174. let tables = JSON.parse(localStorage.getItem('table_'+tableName));
  175. // on first load create tables object
  176. if(!tables) { tables = {}; }
  177. // hide disabled columns
  178. $.each(tables, function (colName, colStatus) {
  179. if(!colStatus) {
  180. $('.checkbox-'+colName).attr('checked', false);
  181. $('.column_'+colName).hide();
  182. }
  183. });
  184. // highlight search text
  185. let searchText = $('.search-form input').val();
  186. if(searchText !== '') {
  187. let innerHTML = tbl.html();
  188. let index = innerHTML.indexOf(searchText);
  189. if (index >= 0) {
  190. innerHTML = innerHTML.substring(0,index) + "<span class='highlight'>" + innerHTML.substring(index,index+searchText.length) + "</span>" + innerHTML.substring(index + searchText.length);
  191. tbl.html(innerHTML);
  192. }
  193. }
  194. $('.table').fadeIn();
  195. $('.toggle-column').on('change', function () {
  196. let columnName = $(this).attr('data-name');
  197. let columnStatus = $(this).is(':checked');
  198. // save column status
  199. tables[columnName] = columnStatus;
  200. localStorage.setItem('table_'+tableName, JSON.stringify(tables));
  201. // show or hide column
  202. if(columnStatus) {
  203. $('.column_'+columnName).show('fast');
  204. } else {
  205. $('.column_'+columnName).hide('fast');
  206. }
  207. });
  208. $('.sort-by-column').on('click', function () {
  209. let columnName = $(this).attr('data-name');
  210. let currentUrl = new URL(document.location.href);
  211. let currentColumnName = currentUrl.searchParams.get('sortBy');
  212. currentUrl.searchParams.set('sortBy', columnName);
  213. if((currentColumnName !== columnName) || (currentUrl.searchParams.has('order'))) {
  214. currentUrl.searchParams.delete('order');
  215. } else {
  216. currentUrl.searchParams.set('order', 'desc');
  217. }
  218. document.location.href = currentUrl.href;
  219. });
  220. $('.accept-filters').on('click', function () {
  221. let filters = $('.filters').serializeArray();
  222. let currentUrl = new URL(document.location.href);
  223. $.each(filters, function (id, filter) {
  224. if(filter.value !== '') {
  225. currentUrl.searchParams.set(filter.name, filter.value);
  226. } else {
  227. currentUrl.searchParams.delete(filter.name);
  228. }
  229. });
  230. currentUrl.searchParams.delete('page');
  231. document.location.href = currentUrl.href;
  232. });
  233. $('.reset-filters').on('click', function () {
  234. let filters = $('.filters').serializeArray();
  235. let currentUrl = new URL(document.location.href);
  236. $.each(filters, function (id, filter) {
  237. currentUrl.searchParams.delete(filter.name);
  238. });
  239. currentUrl.searchParams.delete('page');
  240. document.location.href = currentUrl.href;
  241. });
  242. $('.accept-search').on('click', function () {
  243. let s = $('.search-form input').val();
  244. let currentUrl = new URL(document.location.href);
  245. if(s !== '') {
  246. currentUrl.searchParams.set('s', s);
  247. } else {
  248. currentUrl.searchParams.delete('s');
  249. }
  250. currentUrl.searchParams.delete('page');
  251. document.location.href = currentUrl.href;
  252. });
  253. $('.reset-search').on('click', function () {
  254. let currentUrl = new URL(document.location.href);
  255. currentUrl.searchParams.delete('s');
  256. currentUrl.searchParams.delete('page');
  257. document.location.href = currentUrl.href;
  258. });
  259. </script>
  260. @endpush