table.blade.php 14 KB

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