67 lines
2.5 KiB
JavaScript
67 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
$(function () {
|
|
const filterForm = document.getElementById('kelompok-filter-form');
|
|
if (!filterForm) return;
|
|
|
|
const headers = {
|
|
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
|
|
'Accept': 'application/json'
|
|
};
|
|
const $desa = $('#filter-desa');
|
|
let desaRequest;
|
|
|
|
$('#filter-kecamatan').on('change', function () {
|
|
if (desaRequest) desaRequest.abort();
|
|
$desa.empty().append(new Option('Semua Desa', '')).prop('disabled', true);
|
|
if (!this.value) return;
|
|
|
|
desaRequest = $.ajax({
|
|
url: filterForm.dataset.desaUrl.replace(':id', this.value),
|
|
method: 'GET',
|
|
headers,
|
|
success: function (data) {
|
|
data.forEach(desa => $desa.append(new Option(desa.nama, desa.id)));
|
|
$desa.prop('disabled', false);
|
|
},
|
|
error: function (xhr, status) {
|
|
if (status !== 'abort') $desa.empty().append(new Option('Gagal memuat desa', ''));
|
|
}
|
|
});
|
|
});
|
|
|
|
$('#kelompok-per-page').on('change', function () { this.form.submit(); });
|
|
|
|
$(document).on('click', '.kelompok-tani-row', function (event) {
|
|
if ($(event.target).closest('a, button, input, select, textarea, label').length) return;
|
|
window.location.href = this.querySelector('.edit-record').href;
|
|
});
|
|
|
|
$(document).on('click', '.kelompok-tani-page .delete-record', function () {
|
|
const url = this.dataset.deleteUrl;
|
|
const name = this.dataset.nama;
|
|
Swal.fire({
|
|
title: 'Hapus kelompok tani?',
|
|
text: `Data kelompok tani "${name}" akan dihapus.`,
|
|
icon: 'warning',
|
|
showCancelButton: true,
|
|
confirmButtonText: 'Ya, hapus',
|
|
cancelButtonText: 'Batal',
|
|
customClass: { confirmButton: 'btn btn-danger me-3', cancelButton: 'btn btn-label-secondary' },
|
|
buttonsStyling: false
|
|
}).then(function (result) {
|
|
if (!result.isConfirmed) return;
|
|
$.ajax({
|
|
url,
|
|
method: 'DELETE',
|
|
headers,
|
|
success: function (response) {
|
|
Swal.fire({ icon: 'success', title: 'Terhapus', text: response.message, showConfirmButton: false, timer: 1500 })
|
|
.then(() => window.location.reload());
|
|
},
|
|
error: () => Swal.fire({ icon: 'error', title: 'Gagal menghapus', text: 'Data kelompok tani gagal dihapus. Silakan coba lagi.' })
|
|
});
|
|
});
|
|
});
|
|
});
|