table.blade.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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"
  4. data-bs-target="#table_{{ $id }}_modal_search">
  5. <i class="bi bi-search"></i>
  6. </button>
  7. <button type="button" class="btn btn-sm text-white " data-bs-toggle="modal"
  8. data-bs-target="#table_{{ $id }}_modal_filters">
  9. <i class="bi bi-funnel-fill"></i>
  10. </button>
  11. <button type="button" class="btn btn-sm text-white " data-bs-toggle="modal"
  12. data-bs-target="#table_{{ $id }}_modal_settings">
  13. <i class="bi bi-gear-fill"></i>
  14. </button>
  15. </div>
  16. <table class="table" id="tbl" data-table-name="{{ $id }}" style="display: none;">
  17. <thead>
  18. <tr>
  19. @foreach($header as $headerName => $headerTitle)
  20. <th scope="col" class="bg-primary-subtle column_{{ $headerName }}">
  21. <span class="cursor-pointer sort-by-column" data-name="{{ $headerName }}">
  22. {{ $headerTitle }}
  23. @if(str_starts_with($headerName, $sortBy))
  24. @if($orderBy === 'asc')
  25. <i class="bi bi-arrow-up"></i>
  26. @else
  27. <i class="bi bi-arrow-down"></i>
  28. @endif
  29. @endif
  30. @if(isset(request()->filters[$headerName]) ||
  31. isset(request()->filters[str_replace('_txt', '', $headerName) . '_from']) ||
  32. isset(request()->filters[str_replace('_txt', '', $headerName) . '_to'])
  33. )
  34. <i class="bi bi-funnel"></i>
  35. @endif
  36. </span>
  37. </th>
  38. @endforeach
  39. </tr>
  40. </thead>
  41. <tbody>
  42. @foreach($strings as $string)
  43. <tr>
  44. @foreach($header as $headerName => $headerTitle)
  45. <td class="column_{{$headerName}}"
  46. @if(isset($routeName) && !str_contains($headerName, 'image')) onclick="location.href='{{ route($routeName, $string->id) }}'" @endif>
  47. @if(str_contains($headerName, '-'))
  48. @php
  49. list($rel, $field) = explode('-', $headerName);
  50. @endphp
  51. @if(isset($string->$rel->$field))
  52. @if(str_ends_with($field, '_id'))
  53. @php
  54. $relation = \Illuminate\Support\Str::camel(str_replace('_id', '', $field));
  55. @endphp
  56. {!! $string->$rel->$relation?->name; !!}
  57. @else
  58. @if(str_contains($field, 'image') && $string->$rel->$field)
  59. <a href="{{ $string->$rel->$field }}" data-toggle="lightbox" data-gallery="photos" data-size="fullscreen">
  60. <img src="{{ $string->$rel->$field }}" alt="" class="img-thumbnail maf-img">
  61. </a>
  62. @else
  63. {!! $string->$rel->$field !!}
  64. @endif
  65. @endif
  66. @else
  67. <ul class="small mb-0 list-group list-group-flush bg-secondary">
  68. @foreach($string->$rel ?? [] as $item)
  69. <li class="list-group-item py-0 bg-body-secondary">
  70. {!! $item->$field !!}
  71. </li>
  72. @endforeach
  73. </ul>
  74. @endif
  75. @elseif(str_ends_with($headerName, '_id'))
  76. @php
  77. $relation = \Illuminate\Support\Str::camel(str_replace('_id', '', $headerName));
  78. @endphp
  79. {!! $string->$relation?->name; !!}
  80. @elseif(str_ends_with($headerName, '_date') && ($string->$headerName))
  81. {{ \App\Helpers\DateHelper::getHumanDate($string->$headerName, true) }}
  82. @elseif(str_contains($headerName, 'image') && $string->$headerName)
  83. <a href="{{ $string->$headerName }}" data-toggle="lightbox" data-gallery="photos" data-size="fullscreen">
  84. <img src="{{ $string->$headerName }}" alt="" class="img-thumbnail maf-img">
  85. </a>
  86. @else
  87. {!! $string->$headerName !!}
  88. @endif
  89. </td>
  90. @endforeach
  91. </tr>
  92. @endforeach
  93. </tbody>
  94. </table>
  95. </div>
  96. <!-- Модальное окно настроек таблицы -->
  97. <div class="modal fade" id="table_{{ $id }}_modal_settings" tabindex="-1" aria-labelledby="exampleModalLabel"
  98. aria-hidden="true">
  99. <div class="modal-dialog modal-fullscreen-sm-down">
  100. <div class="modal-content">
  101. <div class="modal-header">
  102. <h1 class="modal-title fs-5" id="exampleModalLabel">Выбор отображаемых колонок</h1>
  103. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  104. </div>
  105. <div class="modal-body">
  106. @foreach($header as $headerName => $headerTitle)
  107. <div>
  108. <label class="me-3"><input type="checkbox" checked="checked" data-name="{{ $headerName }}"
  109. class="toggle-column checkbox-{{ $headerName }}"> {{ $headerTitle }}
  110. </label>
  111. </div>
  112. @endforeach
  113. </div>
  114. <div class="modal-footer">
  115. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
  116. </div>
  117. </div>
  118. </div>
  119. </div>
  120. <!-- Модальное окно фильтров -->
  121. <div class="modal fade" id="table_{{ $id }}_modal_filters" tabindex="-1" aria-labelledby="exampleModalLabel"
  122. aria-hidden="true">
  123. <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
  124. <div class="modal-content">
  125. <div class="modal-header">
  126. <h1 class="modal-title fs-5" id="exampleModalLabel">Фильтры по колонкам таблицы</h1>
  127. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  128. </div>
  129. <div class="modal-body">
  130. <form class="filters">
  131. @if(isset($filters) && is_array($filters))
  132. @foreach($filters as $filterName => $filter)
  133. @php $filter['values'] = ['' => ''] + $filter['values'] @endphp
  134. @include('partials.select', [
  135. 'name' => 'filters[' . $filterName . ']',
  136. 'title' => $filter['title'],
  137. 'options' => $filter['values'],
  138. 'value' => request()->filters[$filterName] ?? '',
  139. ])
  140. @endforeach
  141. @endif
  142. @if(isset($ranges) && is_array($ranges))
  143. @foreach($ranges as $rangeName => $range)
  144. @include('partials.input', [
  145. 'name' => 'filters[' . $rangeName . '_from]',
  146. 'type' => 'number',
  147. 'title' => $range['title'] . ' с:',
  148. 'min' => $range['min'],
  149. 'max' => $range['max'],
  150. 'value' => request()->filters[$rangeName . '_from'] ?? '', // $range['min']
  151. ])
  152. @include('partials.input', [
  153. 'name' => 'filters[' . $rangeName . '_to]',
  154. 'type' => 'number',
  155. 'title' => ' по:',
  156. 'min' => $range['min'],
  157. 'max' => $range['max'],
  158. 'value' => request()->filters[$rangeName . '_to'] ?? '', // $range['max']
  159. ])
  160. @endforeach
  161. @endif
  162. @if(isset($dates) && is_array($dates))
  163. @foreach($dates as $rangeName => $range)
  164. @include('partials.input', [
  165. 'name' => 'filters[' . $rangeName . '_from]',
  166. 'type' => 'date',
  167. 'title' => $range['title'] . ' с:',
  168. 'min' => $range['min'],
  169. 'max' => $range['max'],
  170. 'value' => request()->filters[$rangeName . '_from'] ?? '',
  171. ])
  172. @include('partials.input', [
  173. 'name' => 'filters[' . $rangeName . '_to]',
  174. 'type' => 'date',
  175. 'title' => $range['title'] . ' по:',
  176. 'min' => $range['min'],
  177. 'max' => $range['max'],
  178. 'value' => request()->filters[$rangeName . '_to'] ?? '',
  179. ])
  180. @endforeach
  181. @endif
  182. </form>
  183. </div>
  184. <div class="modal-footer">
  185. <button type="button" class="btn btn-primary accept-filters" data-bs-dismiss="modal">Применить</button>
  186. <button type="button" class="btn btn-outline-secondary reset-filters" data-bs-dismiss="modal">Сбросить
  187. </button>
  188. </div>
  189. </div>
  190. </div>
  191. </div>
  192. <!-- Модальное окно поиска -->
  193. <div class="modal fade" id="table_{{ $id }}_modal_search" tabindex="-1" aria-labelledby="exampleModalLabel"
  194. aria-hidden="true">
  195. <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
  196. <div class="modal-content">
  197. <div class="modal-header">
  198. <h1 class="modal-title fs-5" id="exampleModalLabel">Поиск</h1>
  199. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  200. </div>
  201. <div class="modal-body">
  202. <div>
  203. Поиск ведётся по следующим колонкам:
  204. <span class="fst-italic">
  205. @foreach($searchFields as $searchField)
  206. {{ $header[$searchField] }}
  207. @if(!$loop->last)
  208. ,
  209. @endif
  210. @endforeach
  211. </span>
  212. </div>
  213. <form class="search-form">
  214. @include('partials.input', [
  215. 'name' => 's',
  216. 'title' => 'Поиск',
  217. 'placeholder' => 'что ищем?',
  218. 'value' => request()->s ?? '',
  219. ])
  220. </form>
  221. </div>
  222. <div class="modal-footer">
  223. <button type="button" class="btn btn-primary accept-search" data-bs-dismiss="modal">Найти</button>
  224. <button type="button" class="btn btn-outline-secondary reset-search" data-bs-dismiss="modal">Сбросить
  225. </button>
  226. </div>
  227. </div>
  228. </div>
  229. </div>
  230. @push('scripts')
  231. <script type="module">
  232. // on page load set column visible
  233. let tbl = $('#tbl');
  234. let tableName = tbl.attr('data-table-name');
  235. let tables = JSON.parse(localStorage.getItem('table_' + tableName));
  236. // on first load create tables object
  237. if (!tables) {
  238. tables = {};
  239. }
  240. // hide disabled columns
  241. $.each(tables, function (colName, colStatus) {
  242. if (!colStatus) {
  243. $('.checkbox-' + colName).attr('checked', false);
  244. $('.column_' + colName).hide();
  245. }
  246. });
  247. // highlight search text
  248. let searchText = $('.search-form input').val();
  249. if (searchText !== '') {
  250. let innerHTML = tbl.html();
  251. let index = innerHTML.indexOf(searchText);
  252. if (index >= 0) {
  253. innerHTML = innerHTML.substring(0, index) + "<span class='highlight'>" + innerHTML.substring(index, index + searchText.length) + "</span>" + innerHTML.substring(index + searchText.length);
  254. tbl.html(innerHTML);
  255. }
  256. }
  257. $('.table').fadeIn();
  258. $('.toggle-column').on('change', function () {
  259. let columnName = $(this).attr('data-name');
  260. let columnStatus = $(this).is(':checked');
  261. // save column status
  262. tables[columnName] = columnStatus;
  263. localStorage.setItem('table_' + tableName, JSON.stringify(tables));
  264. // show or hide column
  265. if (columnStatus) {
  266. $('.column_' + columnName).show('fast');
  267. } else {
  268. $('.column_' + columnName).hide('fast');
  269. }
  270. });
  271. $('.sort-by-column').on('click', function () {
  272. let columnName = $(this).attr('data-name');
  273. let currentUrl = new URL(document.location.href);
  274. let currentColumnName = currentUrl.searchParams.get('sortBy');
  275. currentUrl.searchParams.set('sortBy', columnName);
  276. if ((currentColumnName !== columnName) || (currentUrl.searchParams.has('order'))) {
  277. currentUrl.searchParams.delete('order');
  278. } else {
  279. currentUrl.searchParams.set('order', 'desc');
  280. }
  281. document.location.href = currentUrl.href;
  282. });
  283. $('.accept-filters').on('click', function () {
  284. let filters = $('.filters').serializeArray();
  285. let currentUrl = new URL(document.location.href);
  286. $.each(filters, function (id, filter) {
  287. if (filter.value !== '') {
  288. currentUrl.searchParams.set(filter.name, filter.value);
  289. } else {
  290. currentUrl.searchParams.delete(filter.name);
  291. }
  292. });
  293. currentUrl.searchParams.delete('page');
  294. document.location.href = currentUrl.href;
  295. });
  296. $('.reset-filters').on('click', function () {
  297. let filters = $('.filters').serializeArray();
  298. let currentUrl = new URL(document.location.href);
  299. $.each(filters, function (id, filter) {
  300. currentUrl.searchParams.delete(filter.name);
  301. });
  302. currentUrl.searchParams.delete('page');
  303. document.location.href = currentUrl.href;
  304. });
  305. $('.accept-search').on('click', function () {
  306. let s = $('.search-form input').val();
  307. let currentUrl = new URL(document.location.href);
  308. if (s !== '') {
  309. currentUrl.searchParams.set('s', s);
  310. } else {
  311. currentUrl.searchParams.delete('s');
  312. }
  313. currentUrl.searchParams.delete('page');
  314. document.location.href = currentUrl.href;
  315. });
  316. $('.reset-search').on('click', function () {
  317. let currentUrl = new URL(document.location.href);
  318. currentUrl.searchParams.delete('s');
  319. currentUrl.searchParams.delete('page');
  320. document.location.href = currentUrl.href;
  321. });
  322. </script>
  323. @endpush