table.blade.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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($strings as $string)
  40. <tr>
  41. @foreach($header as $headerName => $headerTitle)
  42. <td class="column_{{$headerName}}" @isset($routeName) onclick="location.href='{{ route($routeName, $string->id) }}'" @endisset>
  43. @if(str_ends_with($headerName, '_id'))
  44. @php
  45. $relation = \Illuminate\Support\Str::camel(str_replace('_id', '', $headerName));
  46. @endphp
  47. {!! $string->$relation?->name; !!}
  48. @elseif(str_ends_with($headerName, '_date'))
  49. {{ \App\Helpers\DateHelper::getHumanDate($string->$headerName) }}
  50. @else
  51. {!! $string->$headerName !!}
  52. @endif
  53. </td>
  54. @endforeach
  55. </tr>
  56. @endforeach
  57. </tbody>
  58. </table>
  59. </div>
  60. <!-- Модальное окно настроек таблицы -->
  61. <div class="modal fade" id="table_{{ $id }}_modal_settings" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  62. <div class="modal-dialog modal-fullscreen-sm-down">
  63. <div class="modal-content">
  64. <div class="modal-header">
  65. <h1 class="modal-title fs-5" id="exampleModalLabel">Выбор отображаемых колонок</h1>
  66. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  67. </div>
  68. <div class="modal-body">
  69. @foreach($header as $headerName => $headerTitle)
  70. <div>
  71. <label class="me-3"><input type="checkbox" checked="checked" data-name="{{ $headerName }}" class="toggle-column checkbox-{{ $headerName }}"> {{ $headerTitle }}</label>
  72. </div>
  73. @endforeach
  74. </div>
  75. <div class="modal-footer">
  76. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. <!-- Модальное окно фильтров -->
  82. <div class="modal fade" id="table_{{ $id }}_modal_filters" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  83. <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
  84. <div class="modal-content">
  85. <div class="modal-header">
  86. <h1 class="modal-title fs-5" id="exampleModalLabel">Фильтры по колонкам таблицы</h1>
  87. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  88. </div>
  89. <div class="modal-body">
  90. <form class="filters">
  91. @foreach($filters as $filterName => $filter)
  92. @php $filter['values'] = ['' => ''] + $filter['values'] @endphp
  93. @include('partials.select', [
  94. 'name' => 'filters[' . $filterName . ']',
  95. 'title' => $filter['title'],
  96. 'options' => $filter['values'],
  97. 'value' => request()->filters[$filterName] ?? '',
  98. ])
  99. @endforeach
  100. @foreach($ranges as $rangeName => $range)
  101. @include('partials.input', [
  102. 'name' => 'filters[' . $rangeName . '_from]',
  103. 'type' => 'number',
  104. 'title' => $range['title'] . ' с:',
  105. 'min' => $range['min'],
  106. 'max' => $range['max'],
  107. 'value' => request()->filters[$rangeName . '_from'] ?? '', // $range['min']
  108. ])
  109. @include('partials.input', [
  110. 'name' => 'filters[' . $rangeName . '_to]',
  111. 'type' => 'number',
  112. 'title' => ' по:',
  113. 'min' => $range['min'],
  114. 'max' => $range['max'],
  115. 'value' => request()->filters[$rangeName . '_to'] ?? '', // $range['max']
  116. ])
  117. @endforeach
  118. @foreach($dates as $rangeName => $range)
  119. @include('partials.input', [
  120. 'name' => 'filters[' . $rangeName . '_from]',
  121. 'type' => 'date',
  122. 'title' => $range['title'] . ' с:',
  123. 'min' => $range['min'],
  124. 'max' => $range['max'],
  125. 'value' => request()->filters[$rangeName . '_from'] ?? '',
  126. ])
  127. @include('partials.input', [
  128. 'name' => 'filters[' . $rangeName . '_to]',
  129. 'type' => 'date',
  130. 'title' => $range['title'] . ' по:',
  131. 'min' => $range['min'],
  132. 'max' => $range['max'],
  133. 'value' => request()->filters[$rangeName . '_to'] ?? '',
  134. ])
  135. @endforeach
  136. </form>
  137. </div>
  138. <div class="modal-footer">
  139. <button type="button" class="btn btn-primary accept-filters" data-bs-dismiss="modal">Применить</button>
  140. <button type="button" class="btn btn-outline-secondary reset-filters" data-bs-dismiss="modal">Сбросить</button>
  141. </div>
  142. </div>
  143. </div>
  144. </div>
  145. <!-- Модальное окно поиска -->
  146. <div class="modal fade" id="table_{{ $id }}_modal_search" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  147. <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
  148. <div class="modal-content">
  149. <div class="modal-header">
  150. <h1 class="modal-title fs-5" id="exampleModalLabel">Поиск</h1>
  151. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  152. </div>
  153. <div class="modal-body">
  154. <div>
  155. Поиск ведётся по следующим колонкам:
  156. <span class="fst-italic">
  157. @foreach($searchFields as $searchField)
  158. {{ $header[$searchField] }}
  159. @if(!$loop->last),@endif
  160. @endforeach
  161. </span>
  162. </div>
  163. <form class="search-form">
  164. @include('partials.input', [
  165. 'name' => 's',
  166. 'title' => 'Поиск',
  167. 'placeholder' => 'что ищем?',
  168. 'value' => request()->s ?? '',
  169. ])
  170. </form>
  171. </div>
  172. <div class="modal-footer">
  173. <button type="button" class="btn btn-primary accept-search" data-bs-dismiss="modal">Найти</button>
  174. <button type="button" class="btn btn-outline-secondary reset-search" data-bs-dismiss="modal">Сбросить</button>
  175. </div>
  176. </div>
  177. </div>
  178. </div>
  179. @push('scripts')
  180. <script type="module">
  181. // on page load set column visible
  182. let tbl = $('#tbl');
  183. let tableName = tbl.attr('data-table-name');
  184. let tables = JSON.parse(localStorage.getItem('table_'+tableName));
  185. // on first load create tables object
  186. if(!tables) { tables = {}; }
  187. // hide disabled columns
  188. $.each(tables, function (colName, colStatus) {
  189. if(!colStatus) {
  190. $('.checkbox-'+colName).attr('checked', false);
  191. $('.column_'+colName).hide();
  192. }
  193. });
  194. // highlight search text
  195. let searchText = $('.search-form input').val();
  196. if(searchText !== '') {
  197. let innerHTML = tbl.html();
  198. let index = innerHTML.indexOf(searchText);
  199. if (index >= 0) {
  200. innerHTML = innerHTML.substring(0,index) + "<span class='highlight'>" + innerHTML.substring(index,index+searchText.length) + "</span>" + innerHTML.substring(index + searchText.length);
  201. tbl.html(innerHTML);
  202. }
  203. }
  204. $('.table').fadeIn();
  205. $('.toggle-column').on('change', function () {
  206. let columnName = $(this).attr('data-name');
  207. let columnStatus = $(this).is(':checked');
  208. // save column status
  209. tables[columnName] = columnStatus;
  210. localStorage.setItem('table_'+tableName, JSON.stringify(tables));
  211. // show or hide column
  212. if(columnStatus) {
  213. $('.column_'+columnName).show('fast');
  214. } else {
  215. $('.column_'+columnName).hide('fast');
  216. }
  217. });
  218. $('.sort-by-column').on('click', function () {
  219. let columnName = $(this).attr('data-name');
  220. let currentUrl = new URL(document.location.href);
  221. let currentColumnName = currentUrl.searchParams.get('sortBy');
  222. currentUrl.searchParams.set('sortBy', columnName);
  223. if((currentColumnName !== columnName) || (currentUrl.searchParams.has('order'))) {
  224. currentUrl.searchParams.delete('order');
  225. } else {
  226. currentUrl.searchParams.set('order', 'desc');
  227. }
  228. document.location.href = currentUrl.href;
  229. });
  230. $('.accept-filters').on('click', function () {
  231. let filters = $('.filters').serializeArray();
  232. let currentUrl = new URL(document.location.href);
  233. $.each(filters, function (id, filter) {
  234. if(filter.value !== '') {
  235. currentUrl.searchParams.set(filter.name, filter.value);
  236. } else {
  237. currentUrl.searchParams.delete(filter.name);
  238. }
  239. });
  240. currentUrl.searchParams.delete('page');
  241. document.location.href = currentUrl.href;
  242. });
  243. $('.reset-filters').on('click', function () {
  244. let filters = $('.filters').serializeArray();
  245. let currentUrl = new URL(document.location.href);
  246. $.each(filters, function (id, filter) {
  247. currentUrl.searchParams.delete(filter.name);
  248. });
  249. currentUrl.searchParams.delete('page');
  250. document.location.href = currentUrl.href;
  251. });
  252. $('.accept-search').on('click', function () {
  253. let s = $('.search-form input').val();
  254. let currentUrl = new URL(document.location.href);
  255. if(s !== '') {
  256. currentUrl.searchParams.set('s', s);
  257. } else {
  258. currentUrl.searchParams.delete('s');
  259. }
  260. currentUrl.searchParams.delete('page');
  261. document.location.href = currentUrl.href;
  262. });
  263. $('.reset-search').on('click', function () {
  264. let currentUrl = new URL(document.location.href);
  265. currentUrl.searchParams.delete('s');
  266. currentUrl.searchParams.delete('page');
  267. document.location.href = currentUrl.href;
  268. });
  269. </script>
  270. @endpush