Enhance row selection functionality by updating row highlight based on checkbox state and managing visibility of selection column
This commit is contained in:
@@ -237,6 +237,11 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
const checks = findRowCheckboxes();
|
const checks = findRowCheckboxes();
|
||||||
const count = checks.filter(c => c.checked).length;
|
const count = checks.filter(c => c.checked).length;
|
||||||
selectedCountSpan.textContent = count;
|
selectedCountSpan.textContent = count;
|
||||||
|
// update row highlight based on checkbox state
|
||||||
|
checks.forEach(cb => {
|
||||||
|
const tr = cb.closest('tr');
|
||||||
|
if (tr) tr.classList.toggle('table-active', cb.checked);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCheckboxesDisabled(disabled) {
|
function setCheckboxesDisabled(disabled) {
|
||||||
@@ -245,8 +250,28 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// disable checkboxes by default
|
function hideSelectionColumn() {
|
||||||
|
if (!table) return;
|
||||||
|
// hide any th/td that contains the selection checkbox
|
||||||
|
table.querySelectorAll('th, td').forEach(function(cell) {
|
||||||
|
if (cell.querySelector && cell.querySelector('input[type="checkbox"]')) {
|
||||||
|
cell.style.display = 'none';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function showSelectionColumn() {
|
||||||
|
if (!table) return;
|
||||||
|
table.querySelectorAll('th, td').forEach(function(cell) {
|
||||||
|
if (cell.querySelector && cell.querySelector('input[type="checkbox"]')) {
|
||||||
|
cell.style.display = '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable checkboxes by default and hide the selection column
|
||||||
setCheckboxesDisabled(true);
|
setCheckboxesDisabled(true);
|
||||||
|
hideSelectionColumn();
|
||||||
|
|
||||||
// row click toggling
|
// row click toggling
|
||||||
document.addEventListener('click', function(e) {
|
document.addEventListener('click', function(e) {
|
||||||
@@ -259,6 +284,8 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
const cb = tr.querySelector('input[type="checkbox"]');
|
const cb = tr.querySelector('input[type="checkbox"]');
|
||||||
if (cb && !cb.disabled) {
|
if (cb && !cb.disabled) {
|
||||||
cb.checked = !cb.checked;
|
cb.checked = !cb.checked;
|
||||||
|
// reflect highlight immediately
|
||||||
|
tr.classList.toggle('table-active', cb.checked);
|
||||||
updateSelectedCount();
|
updateSelectedCount();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -270,10 +297,12 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
toggleBtn.textContent = 'Disable row selection';
|
toggleBtn.textContent = 'Disable row selection';
|
||||||
selectionControls.style.display = '';
|
selectionControls.style.display = '';
|
||||||
setCheckboxesDisabled(false);
|
setCheckboxesDisabled(false);
|
||||||
|
showSelectionColumn();
|
||||||
} else {
|
} else {
|
||||||
toggleBtn.textContent = 'Enable row selection';
|
toggleBtn.textContent = 'Enable row selection';
|
||||||
selectionControls.style.display = 'none';
|
selectionControls.style.display = 'none';
|
||||||
setCheckboxesDisabled(true);
|
setCheckboxesDisabled(true);
|
||||||
|
hideSelectionColumn();
|
||||||
// clear any selected checkboxes
|
// clear any selected checkboxes
|
||||||
findRowCheckboxes().forEach(cb => cb.checked = false);
|
findRowCheckboxes().forEach(cb => cb.checked = false);
|
||||||
updateSelectedCount();
|
updateSelectedCount();
|
||||||
|
|||||||
Reference in New Issue
Block a user