Skip to content

Commit 4fc63f2

Browse files
committed
Fixed sorting table
Bug was when beta mode is enabled
1 parent 98d16cc commit 4fc63f2

File tree

1 file changed

+47
-47
lines changed

1 file changed

+47
-47
lines changed

resources/views/panel/users.blade.php

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<a href="{{ url('') }}/admin/users/admin">Admin</a>
4141

4242
<div class="row"><div class="table-responsive">
43-
<table class="table table-stripped">
43+
<table id="sortable" class="table table-stripped">
4444
<thead>
4545
<tr>
4646
<th id="cs" scope="col" data-sort="id" data-order="asc">{{__('messages.ID')}}</th>
@@ -171,52 +171,52 @@
171171
</div>
172172

173173
@push('sidebar-scripts')
174-
<script defer>
175-
const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
176-
177-
const comparer = (idx, asc) => (a, b) =>
178-
((v1, v2) =>
179-
v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
180-
)(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
181-
182-
document.addEventListener("DOMContentLoaded", function() {
183-
// Find the table and its headers
184-
const table = document.querySelector('table');
185-
const headers = table.querySelectorAll('th');
186-
187-
// Add caret icon to initial header element
188-
const initialHeader = table.querySelector('[data-order]');
189-
initialHeader.innerHTML = `${initialHeader.innerText} <i class="bi bi-caret-down-fill"></i>`;
190-
191-
// Attach click event listener to all headers
192-
headers.forEach(th => th.addEventListener('click', function() {
193-
// Get the clicked header's index, sort order, and sortable attribute
194-
const thIndex = Array.from(th.parentNode.children).indexOf(th);
195-
const isAscending = this.asc = !this.asc;
196-
const isSortable = th.getAttribute('data-sortable') !== 'false';
197-
198-
// If the column is not sortable, do nothing
199-
if (!isSortable) {
200-
return;
201-
}
202-
203-
// Remove caret icon and active class from all headers
204-
headers.forEach(h => {
205-
h.classList.remove('active');
206-
h.innerHTML = h.innerText;
207-
});
208-
209-
// Add caret icon and active class to clicked header
210-
th.classList.add('active');
211-
th.innerHTML = `${th.innerText} ${isAscending ? '<i class="bi bi-caret-down-fill"></i>' : '<i class="bi bi-caret-up-fill"></i>'}`;
212-
213-
// Sort the table rows based on the clicked header
214-
Array.from(table.querySelectorAll('tr:nth-child(n+2)'))
215-
.sort(comparer(thIndex, isAscending))
216-
.forEach(tr => table.appendChild(tr));
217-
}));
218-
});
219-
</script>
174+
<script>
175+
const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
176+
177+
const comparer = (idx, asc) => (a, b) =>
178+
((v1, v2) =>
179+
v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2)
180+
)(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
181+
182+
document.addEventListener('DOMContentLoaded', () => {
183+
// Find the sortable table and its headers
184+
const table = document.querySelector('#sortable.table.table-stripped');
185+
const headers = table.querySelectorAll('th[data-sort]');
186+
187+
// Add caret icon to initial header element
188+
const initialHeader = table.querySelector('[data-order]');
189+
initialHeader.innerHTML = `${initialHeader.innerText} <i class="bi bi-caret-down-fill"></i>`;
190+
191+
// Attach click event listener to all sortable headers
192+
headers.forEach(th => th.addEventListener('click', function() {
193+
// Get the clicked header's index, sort order, and sortable attribute
194+
const thIndex = Array.from(th.parentNode.children).indexOf(th);
195+
const isAscending = this.asc = !this.asc;
196+
const isSortable = th.getAttribute('data-sortable') !== 'false';
197+
198+
// If the column is not sortable, do nothing
199+
if (!isSortable) {
200+
return;
201+
}
202+
203+
// Remove caret icon and active class from all headers
204+
headers.forEach(h => {
205+
h.classList.remove('active');
206+
h.innerHTML = h.innerText;
207+
});
208+
209+
// Add caret icon and active class to clicked header
210+
th.classList.add('active');
211+
th.innerHTML = `${th.innerText} ${isAscending ? '<i class="bi bi-caret-down-fill"></i>' : '<i class="bi bi-caret-up-fill"></i>'}`;
212+
213+
// Sort the table rows based on the clicked header
214+
Array.from(table.querySelectorAll('tbody tr'))
215+
.sort(comparer(thIndex, isAscending))
216+
.forEach(tr => table.querySelector('tbody').appendChild(tr));
217+
}));
218+
});
219+
</script>
220220
@endpush
221221

222222
@endsection

0 commit comments

Comments
 (0)