document.addEventListener('DOMContentLoaded', function() { var batchDeleteBtn =…
- User
document.addEventListener('DOMContentLoaded', function() { var batchDeleteBtn = document.querySelector('.js-batch-delete');
codeif (!batchDeleteBtn) return; batchDeleteBtn.addEventListener('click', function(e) { e.preventDefault(); // Get selected rows from yiiGridView // This example assumes selected checkboxes have 'input[name="selection[]"]:checked' inside #w0 var checkboxes = document.querySelectorAll('#w0 input[name="selection[]"]:checked'); var rowIds = Array.from(checkboxes).map(function(chk) { return chk.value; }); if (rowIds.length === 0) { alert('Please select at least one item to delete.'); return; } if (!confirm('Are you sure you want to delete the selected items?')) { return; } // The URL, replace with your yii Url helper output or write directly as a string var url = "<?= \yii\helpers\Url::toRoute(['session-user/multiple-delete']) ?>"; fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest', // suggested to mimic AJAX 'X-CSRF-Token': (document.querySelector('meta[name="csrf-token"]') || {}).content || '' }, body: new URLSearchParams({row_id: rowIds}) }) .then(function(response) { if (!response.ok) { throw new Error('Network response was not ok'); } return response.text(); }) .then(function() { // Reload the grid via PJAX equivalent // Pure vanilla JS PJAX reload - you have to implement reloading or trigger pjax in your environment var pjaxContainer = document.querySelector('#w0-pjax'); if (pjaxContainer) { // If yii Pjax is used, you can trigger reload via yii Pjax js api: // But since this is vanilla, we'll try to reload the container via fetch // Or you can force full page reload: // location.reload(); // Or you can trigger a custom event or call your grid reload method accordingly // For demonstration, let's trigger a reload by dispatching an event: var event = new CustomEvent('pjax:reload'); pjaxContainer.dispatchEvent(event); } else { // Fallback reload: location.reload(); } }) .catch(function() { alert('An error occurred while attempting to delete the items. Please try again.'); }); }); - Assistant
- User
$js = <<<JS document.addEventListener('DOMContentLoaded', function() { var batchDeleteBtn = document.querySelector('.js-batch-delete'); if (!batchDeleteBtn) { return; }
code - Assistant
- User
- Assistant