Penambahan menu Perkuliahan

This commit is contained in:
Ariyutta
2026-11-06 02:23:37 +07:00
parent ddd02d7df9
commit efb3a1d41d
57 changed files with 7427 additions and 88 deletions
+186 -2
View File
@@ -679,6 +679,11 @@ Alpine.data('satuRemoteTable', (config) => ({
loading: false,
picked: [],
controller: null,
// Opsional: tautan Lihat per baris ("__ID__" diganti id) dan penghapusan sungguhan (POST {ids})
viewUrl: config.viewUrl ?? null,
destroyUrl: config.destroyUrl ?? null,
token: config.token ?? '',
pending: [],
get window() {
const start = Math.max(1, Math.min(this.page - 2, this.pages - 4));
@@ -703,7 +708,9 @@ Alpine.data('satuRemoteTable', (config) => ({
const params = new URLSearchParams({ page: String(page), per: String(this.per) });
if (this.q.trim() !== '') params.set('q', this.q.trim());
Object.entries(this.values).forEach(([name, value]) => {
if (value !== '' && value !== null) params.set(name, value);
// Kotak centang: hanya dikirim saat dicentang
if (value === '' || value === null || value === false) return;
params.set(name, value === true ? '1' : value);
});
// Permintaan lama dibatalkan agar hasil yang terlambat tidak menimpa yang terbaru
@@ -744,12 +751,189 @@ Alpine.data('satuRemoteTable', (config) => ({
window.toast('info', `${label}: tampilan prototipe, tidak ada data yang diubah.`);
},
link(row) {
return this.viewUrl ? this.viewUrl.replace('__ID__', encodeURIComponent(row.id)) : '#';
},
remove() {
if (!this.picked.length) {
window.toast('warning', 'Pilih data yang akan dihapus terlebih dahulu.');
return;
}
this.demo('Hapus');
this.askRemove(this.picked);
},
askRemove(ids) {
if (!this.destroyUrl) {
this.demo('Hapus');
return;
}
this.pending = [...ids];
const dialog = this.$refs.dialog;
if (dialog && !dialog.open) dialog.showModal();
},
closeDialog() {
this.$refs.dialog?.close();
},
backdrop(event) {
if (event.target === this.$refs.dialog) this.closeDialog();
},
async confirmRemove() {
const ids = [...this.pending];
if (!ids.length) return;
this.loading = true;
try {
const response = await fetch(this.destroyUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': this.token, 'X-Requested-With': 'XMLHttpRequest' },
body: JSON.stringify({ ids }),
});
const data = await response.json().catch(() => null);
if (!response.ok) throw new Error(data?.message || 'Data tidak dapat dihapus. Muat ulang halaman lalu coba lagi.');
console.info(`[SIAKAD] POST ${this.destroyUrl}`, { ids: ids.length, hasil: data?.message });
window.toast('success', data?.message || 'Data dihapus.');
this.closeDialog();
this.loading = false;
this.load(this.page);
} catch (error) {
console.error('[SIAKAD] Gagal menghapus', error);
window.toast('danger', error.message);
this.loading = false;
}
},
}));
/*
* Kegiatan Akademik (Portal operator, akun DB lama): tabel jenis kegiatan dengan tambah/ubah sebaris dan hapus
* berdialog. Baris disaring dan dihalamankan di sisi klien (jumlahnya puluhan); setiap simpan mengirim JSON dan
* memakai daftar segar dari server.
*/
Alpine.data('satuKegiatan', (config) => ({
rows: config.rows,
urls: config.urls,
token: config.token,
ready: config.ready,
q: '',
per: config.per,
page: 1,
editing: null,
form: { kode: '', nama: '', warna: '' },
saving: false,
pending: null,
get filtered() {
const q = this.q.trim().toLowerCase();
if (q === '') return this.rows;
return this.rows.filter((row) => [row.kode, row.nama, row.warna ?? ''].some((value) => String(value).toLowerCase().includes(q)));
},
get pages() {
return Math.max(1, Math.ceil(this.filtered.length / this.per));
},
get view() {
const start = (this.page - 1) * this.per;
return this.filtered.slice(start, start + this.per);
},
get window() {
const start = Math.max(1, Math.min(this.page - 2, this.pages - 4));
return Array.from({ length: Math.min(5, this.pages) }, (_, i) => start + i);
},
reset() {
this.page = 1;
},
go(page) {
this.page = Math.min(Math.max(1, page), this.pages);
},
startInsert() {
this.editing = 'new';
this.form = { kode: '', nama: '', warna: '' };
this.$nextTick(() => this.$refs.nama?.focus());
},
startEdit(row) {
this.editing = row.id;
this.form = { kode: row.kode, nama: row.nama, warna: row.warna ?? '' };
this.$nextTick(() => this.$refs.nama?.focus());
},
cancel() {
this.editing = null;
},
async request(url, method, body) {
const response = await fetch(url, {
method,
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': this.token, 'X-Requested-With': 'XMLHttpRequest' },
body: body === undefined ? undefined : JSON.stringify(body),
});
const data = await response.json().catch(() => null);
if (!response.ok) {
const first = data?.errors ? Object.values(data.errors).flat()[0] : null;
throw new Error(first || data?.message || 'Permintaan gagal. Muat ulang halaman lalu coba lagi.');
}
console.info(`[SIAKAD] ${method} ${url}`, { hasil: data?.message });
return data;
},
async save() {
if (this.saving || this.editing === null) return;
if (this.form.nama.trim() === '') {
window.toast('warning', 'Nama kegiatan akademik wajib diisi.');
this.$refs.nama?.focus();
return;
}
const isNew = this.editing === 'new';
const url = isNew ? this.urls.store : this.urls.update.replace('__ID__', this.editing);
const body = { kode: this.form.kode.trim() || null, nama: this.form.nama.trim(), warna: this.form.warna.trim() || null };
this.saving = true;
try {
const data = await this.request(url, isNew ? 'POST' : 'PUT', body);
this.rows = data.rows;
this.editing = null;
window.toast('success', data.message);
} catch (error) {
window.toast('danger', error.message);
} finally {
this.saving = false;
}
},
askRemove(row) {
this.pending = row;
const dialog = this.$refs.dialog;
if (dialog && !dialog.open) dialog.showModal();
},
closeDialog() {
this.$refs.dialog?.close();
},
backdrop(event) {
if (event.target === this.$refs.dialog) this.closeDialog();
},
async confirmRemove() {
if (!this.pending || this.saving) return;
this.saving = true;
try {
const data = await this.request(this.urls.destroy.replace('__ID__', this.pending.id), 'DELETE');
this.rows = data.rows;
if (this.page > this.pages) this.page = this.pages;
window.toast('success', data.message);
this.closeDialog();
} catch (error) {
window.toast('danger', error.message);
} finally {
this.saving = false;
}
},
}));