From ecdca382c47dfb4388f83f92deeccd10f531db2e Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Sat, 21 Dec 2024 08:06:28 +0700 Subject: [PATCH] Tambahkan fitur penghapusan banyak data cabang - Menambahkan route baru untuk endpoint `deleteMultiple` pada BranchController. - Update BranchController dengan fungsi `deleteMultiple` untuk menghapus banyak data cabang berdasarkan ID yang diterima. - Menambahkan tombol "Delete Selected" di halaman cabang untuk memulai proses penghapusan. - Menambahkan fungsi JavaScript untuk menangani seleksi data, konfirmasi penghapusan menggunakan SweetAlert, dan pengiriman data menggunakan Ajax. - Memperbarui tampilan untuk mendukung penghapusan multiple dengan kontrol visibilitas tombol "Delete Selected". --- app/Http/Controllers/BranchController.php | 7 +++ resources/views/branch/index.blade.php | 71 ++++++++++++++++++++++- routes/web.php | 1 + 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/BranchController.php b/app/Http/Controllers/BranchController.php index 86cc7a3..b1c070f 100644 --- a/app/Http/Controllers/BranchController.php +++ b/app/Http/Controllers/BranchController.php @@ -82,6 +82,13 @@ } } + public function deleteMultiple(Request $request) + { + $ids = $request->input('ids'); + Branch::whereIn('id', $ids)->delete(); + return response()->json(['message' => 'Branches deleted successfully']); + } + public function dataForDatatables(Request $request) { if (is_null($this->user) || !$this->user->can('branch.view')) { diff --git a/resources/views/branch/index.blade.php b/resources/views/branch/index.blade.php index cbee8de..2b413ea 100644 --- a/resources/views/branch/index.blade.php +++ b/resources/views/branch/index.blade.php @@ -21,6 +21,7 @@
Export to Excel Tambah Cabang + @@ -93,10 +94,52 @@ } }) } + + function deleteSelectedRows() { + const selectedCheckboxes = document.querySelectorAll('input[data-datatable-row-check="true"]:checked'); + if (selectedCheckboxes.length === 0) { + Swal.fire('Warning', 'Please select at least one row to delete.', 'warning'); + return; + } + + Swal.fire({ + title: 'Are you sure?', + text: `You are about to delete ${selectedCheckboxes.length} selected row(s). This action cannot be undone!`, + icon: 'warning', + showCancelButton: true, + confirmButtonColor: '#3085d6', + cancelButtonColor: '#d33', + confirmButtonText: 'Yes, delete them!' + }).then((result) => { + if (result.isConfirmed) { + const ids = Array.from(selectedCheckboxes).map(checkbox => checkbox.value); + + $.ajaxSetup({ + headers: { + 'X-CSRF-TOKEN': '{{ csrf_token() }}' + } + }); + + $.ajax('{{ route("basicdata.branch.deleteMultiple") }}', { + type: 'POST', + data: { ids: ids } + }).then((response) => { + Swal.fire('Deleted!', 'Selected rows have been deleted.', 'success').then(() => { + window.location.reload(); + }); + }).catch((error) => { + console.error('Error:', error); + Swal.fire('Error!', 'An error occurred while deleting the rows.', 'error'); + }); + } + }) + } + @endpush diff --git a/routes/web.php b/routes/web.php index 784453f..e31308c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -41,6 +41,7 @@ Route::get('restore/{id}', [BranchController::class, 'restore'])->name('restore'); Route::get('datatables', [BranchController::class, 'dataForDatatables'])->name('datatables'); Route::get('export', [BranchController::class, 'export'])->name('export'); + Route::post('delete-multiple', [BranchController::class, 'deleteMultiple'])->name('deleteMultiple'); }); Route::resource('cabang', BranchController::class, [