table.blade.php 16 KB

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