table.blade.php 14 KB

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