table.blade.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <div class="table-responsive">
  2. <div class="py-2 bg-primary rounded-start" style="position: absolute; right: 0">
  3. <button type="button" class="btn btn-sm btn-primary " data-bs-toggle="modal" data-bs-target="#table_{{ $id }}_modal">
  4. <i class="bi bi-gear-fill"></i>
  5. </button>
  6. </div>
  7. <table class="table" id="tbl" data-table-name="{{ $id }}" style="display: none;">
  8. <thead>
  9. <tr>
  10. @foreach($header as $headerName => $headerTitle)
  11. <th scope="col" class="bg-primary-subtle column_{{ $headerName }} hrader_{{ $headerName }}">
  12. <span class="cursor-pointer sort-by-column" data-name="{{ $headerName }}">
  13. {{ $headerTitle }}
  14. @if(str_starts_with($headerName, $sortBy))
  15. @if($orderBy === 'asc')
  16. <i class="bi bi-arrow-up"></i>
  17. @else
  18. <i class="bi bi-arrow-down"></i>
  19. @endif
  20. @endif
  21. </span>
  22. </th>
  23. @endforeach
  24. </tr>
  25. </thead>
  26. <tbody>
  27. @foreach($products as $product)
  28. <tr>
  29. @foreach($header as $headerName => $headerTitle)
  30. <td class="column_{{$headerName}}">{!! $product->$headerName !!}</td>
  31. @endforeach
  32. </tr>
  33. @endforeach
  34. </tbody>
  35. </table>
  36. </div>
  37. <!-- Модальное окно настроек таблицы -->
  38. <div class="modal fade" id="table_{{ $id }}_modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  39. <div class="modal-dialog modal-fullscreen-sm-down">
  40. <div class="modal-content">
  41. <div class="modal-header">
  42. <h1 class="modal-title fs-5" id="exampleModalLabel">Выбор отображаемых колонок</h1>
  43. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  44. </div>
  45. <div class="modal-body">
  46. @foreach($header as $headerName => $headerTitle)
  47. <div>
  48. <label class="me-3"><input type="checkbox" checked="checked" data-name="{{ $headerName }}" class="toggle-column checkbox-{{ $headerName }}"> {{ $headerTitle }}</label>
  49. </div>
  50. @endforeach
  51. </div>
  52. <div class="modal-footer">
  53. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. @push('scripts')
  59. <script type="module">
  60. // on page load set column visible
  61. let tableName = $('#tbl').attr('data-table-name');
  62. let tables = JSON.parse(localStorage.getItem('table_'+tableName));
  63. // on first load create tables object
  64. if(!tables) { tables = {}; }
  65. $.each(tables, function (colName, colStatus) {
  66. if(!colStatus) {
  67. $('.checkbox-'+colName).attr('checked', false);
  68. $('.column_'+colName).hide();
  69. }
  70. });
  71. $('.table').fadeIn();
  72. $('.toggle-column').on('change', function () {
  73. let columnName = $(this).attr('data-name');
  74. let columnStatus = $(this).is(':checked');
  75. // save column status
  76. tables[columnName] = columnStatus;
  77. localStorage.setItem('table_'+tableName, JSON.stringify(tables));
  78. // show or hide column
  79. if(columnStatus) {
  80. $('.column_'+columnName).show('fast');
  81. } else {
  82. $('.column_'+columnName).hide('fast');
  83. }
  84. });
  85. $('.sort-by-column').on('click', function () {
  86. let columnName = $(this).attr('data-name');
  87. let currentUrl = new URL(document.location.href);
  88. let currentColumnName = currentUrl.searchParams.get('sortBy');
  89. currentUrl.searchParams.set('sortBy', columnName);
  90. if((currentColumnName !== columnName) || (currentUrl.searchParams.has('order'))) {
  91. currentUrl.searchParams.delete('order');
  92. } else {
  93. currentUrl.searchParams.set('order', 'desc');
  94. }
  95. document.location.href = currentUrl.href;
  96. });
  97. </script>
  98. @endpush