table.blade.php 19 KB

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