implementados filtros y delete. falta los textos del delete

This commit is contained in:
2025-09-28 09:41:15 +02:00
parent 847249d2de
commit 50599cf33e
5 changed files with 163 additions and 41 deletions

View File

@ -1,16 +1,37 @@
$(() => {
const language = document.documentElement.lang || 'es-ES';
// CSRF global para AJAX
const csrfToken = document.querySelector('meta[name="_csrf"]')?.getAttribute('content');
const csrfHeader = document.querySelector('meta[name="_csrf_header"]')?.getAttribute('content');
if (csrfToken && csrfHeader) {
$.ajaxSetup({
beforeSend: function (xhr) {
xhr.setRequestHeader(csrfHeader, csrfToken);
}
});
}
const table = new DataTable('#users-datatable', {
processing: true, serverSide: true, pageLength: 50,
processing: true,
serverSide: true,
orderCellsTop: true,
pageLength: 50,
language: { url: '/assets/libs/datatables/i18n/' + language + '.json' },
responsive: true,
ajax: { url: '/users/datatable', method: 'GET' },
order: [[0,'asc']],
ajax: {
url: '/users/datatable',
method: 'GET',
data: function (d) {
d.f_role = $('#search-role').val() || ''; // 'USER' | 'ADMIN' | 'SUPERADMIN' | ''
d.f_enabled = $('#search-status').val() || ''; // 'true' | 'false' | ''
}
},
order: [[0, 'asc']],
columns: [
{ data: 'id', name: 'id' , orderable: true },
{ data: 'fullName', name: 'fullName' , orderable: true },
{ data: 'userName', name: 'userName' , orderable: true },
{ data: 'id', name: 'id', orderable: true },
{ data: 'fullName', name: 'fullName', orderable: true },
{ data: 'userName', name: 'userName', orderable: true },
{ data: 'roles', name: 'roleRank' },
{ data: 'enabled', name: 'enabled', searchable: false },
{ data: 'actions', name: 'actions' }
@ -18,8 +39,21 @@ $(() => {
columnDefs: [{ targets: -1, orderable: false, searchable: false }]
});
table.on("keyup", ".user-filter", function () {
const colName = $(this).data("col");
const colIndex = table.settings()[0].aoColumns.findIndex(c => c.name === colName);
if (colIndex >= 0) {
table.column(colIndex).search(this.value).draw();
}
});
table.on("change", ".user-filter-select", function () {
table.draw();
});
const modalEl = document.getElementById('userFormModal');
const modal = bootstrap.Modal.getOrCreateInstance(modalEl);
const modal = bootstrap.Modal.getOrCreateInstance(modalEl);
// Abrir "Crear"
$('#addUserButton').on('click', (e) => {
@ -36,7 +70,7 @@ $(() => {
$(document).on('click', '.btn-edit-user', function (e) {
e.preventDefault();
const id = $(this).data('id');
$.get('/users/form', { id }, function (html) {
$.get('/users/form', { id }, function (html) {
$('#userModalBody').html(html);
const title = $('#userModalBody #userForm').data('edit');
$('#userFormModal .modal-title').text(title);
@ -44,6 +78,40 @@ $(() => {
});
});
// Botón "Eliminar"
$(document).on('click', '.btn-delete-user', function (e) {
e.preventDefault();
const id = $(this).data('id');
Swal.fire({
title: '¿Eliminar usuario?',
text: 'Esta acción no se puede deshacer.',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Sí, eliminar',
cancelButtonText: 'Cancelar',
reverseButtons: true
}).then((result) => {
if (!result.isConfirmed) return;
$.ajax({
url: '/users/' + id,
type: 'DELETE',
success: function () {
Swal.fire({ icon: 'success', title: 'Eliminado', timer: 1200, showConfirmButton: false });
$('#users-datatable').DataTable().ajax.reload(null, false);
},
error: function (xhr) {
// usa el mensaje del backend; fallback genérico por si no llega JSON
const msg = (xhr.responseJSON && xhr.responseJSON.message)
|| 'Error al eliminar el usuario.';
Swal.fire({ icon: 'error', title: 'No se pudo eliminar', text: msg });
}
});
});
});
// Submit del form en el modal
$(document).on('submit', '#userForm', function (e) {
e.preventDefault();
@ -59,7 +127,7 @@ $(() => {
if (typeof html === 'string' && html.indexOf('id="userForm"') !== -1 && html.indexOf('<html') === -1) {
$('#userModalBody').html(html);
const isEdit = $('#userModalBody #userForm input[name="_method"][value="PUT"]').length > 0;
const title = $('#userModalBody #userForm').data(isEdit ? 'edit' : 'add');
const title = $('#userModalBody #userForm').data(isEdit ? 'edit' : 'add');
$('#userFormModal .modal-title').text(title);
return;
}
@ -72,7 +140,7 @@ $(() => {
if (xhr.status === 422 && xhr.responseText) {
$('#userModalBody').html(xhr.responseText);
const isEdit = $('#userModalBody #userForm input[name="_method"][value="PUT"]').length > 0;
const title = $('#userModalBody #userForm').data(isEdit ? 'edit' : 'add');
const title = $('#userModalBody #userForm').data(isEdit ? 'edit' : 'add');
$('#userFormModal .modal-title').text(title);
return;
}