table.blade.php 18 KB

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