table.blade.php 17 KB

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