table.blade.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 }}">
  8. <thead>
  9. <tr>
  10. @foreach($header as $headerName => $headerTitle)
  11. <th scope="col" class="column_{{ $headerName }} hrader_{{ $headerName }}">{{ $headerTitle }}</th>
  12. @endforeach
  13. </tr>
  14. </thead>
  15. <tbody>
  16. @foreach($products as $product)
  17. <tr>
  18. @foreach($header as $headerName => $headerTitle)
  19. <td class="column_{{$headerName}}">{!! $product->$headerName !!}</td>
  20. @endforeach
  21. </tr>
  22. @endforeach
  23. </tbody>
  24. </table>
  25. </div>
  26. <!-- Модальное окно настроек таблицы -->
  27. <div class="modal fade" id="table_{{ $id }}_modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  28. <div class="modal-dialog modal-fullscreen-sm-down">
  29. <div class="modal-content">
  30. <div class="modal-header">
  31. <h1 class="modal-title fs-5" id="exampleModalLabel">Выбор отображаемых колонок</h1>
  32. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Закрыть"></button>
  33. </div>
  34. <div class="modal-body">
  35. @foreach($header as $headerName => $headerTitle)
  36. <div>
  37. <label class="me-3"><input type="checkbox" checked="checked" data-name="{{ $headerName }}" class="toggle-column checkbox-{{ $headerName }}"> {{ $headerTitle }}</label>
  38. </div>
  39. @endforeach
  40. </div>
  41. <div class="modal-footer">
  42. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. @push('scripts')
  48. <script type="module">
  49. // on page load set column visible
  50. let tableName = $('#tbl').attr('data-table-name');
  51. let tables = JSON.parse(localStorage.getItem('table_'+tableName));
  52. // on first load create tables object
  53. if(!tables) {
  54. tables = {};
  55. }
  56. $.each(tables, function (colName, colStatus) {
  57. if(!colStatus) {
  58. $('.checkbox-'+colName).attr('checked', false);
  59. $('.column_'+colName).hide();
  60. }
  61. });
  62. $('.toggle-column').on('change', function () {
  63. let columnName = $(this).attr('data-name');
  64. let columnStatus = $(this).is(':checked');
  65. // save column status
  66. tables[columnName] = columnStatus;
  67. localStorage.setItem('table_'+tableName, JSON.stringify(tables));
  68. // show or hide column
  69. if(columnStatus) {
  70. $('.column_'+columnName).show('fast');
  71. } else {
  72. $('.column_'+columnName).hide('fast');
  73. }
  74. });
  75. $('#table-settings').on('click', function () {
  76. });
  77. </script>
  78. @endpush