fix(edit,delete)

This commit is contained in:
kevin 2025-06-25 08:47:02 -04:00
parent 0ef6f9814d
commit 4e17e56041
5 changed files with 148 additions and 5 deletions

14
ajax/delete_model.php Normal file
View File

@ -0,0 +1,14 @@
<?php
require __DIR__ . '/../config.php';
header('Content-Type: application/json; charset=utf-8');
$id = intval($_POST['id'] ?? 0);
if ($id > 0) {
$stmt = $pdo->prepare("DELETE FROM models WHERE id = ?");
$ok = $stmt->execute([$id]);
echo json_encode(['success' => $ok]);
} else {
echo json_encode(['success' => false, 'error' => 'ID inválido']);
}

View File

@ -25,7 +25,7 @@ try {
$filtered = $stmt->fetchColumn();
}
$sql = "SELECT id, name FROM models $where ORDER BY id DESC LIMIT :start, :len";
$sql = "SELECT id, name, version, model_size, layers FROM models $where ORDER BY id DESC LIMIT :start, :len";
$stmt = $pdo->prepare($sql);
foreach ($params as $k => $v) {
$stmt->bindValue($k, $v);

87
editar_modelo.php Normal file
View File

@ -0,0 +1,87 @@
<?php
require 'config.php';
$id = intval($_GET['id'] ?? 0);
if ($id <= 0) {
header('Location: list_tables.php?error=ID inválido');
exit;
}
// Obtener datos actuales
$stmt = $pdo->prepare("SELECT * FROM models WHERE id = ?");
$stmt->execute([$id]);
$model = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$model) {
header('Location: list_tables.php?error=Modelo no encontrado');
exit;
}
// Si se envió el formulario
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
$version = $_POST['version'] ?? '';
$description = $_POST['description'] ?? '';
$layers = $_POST['layers'] !== '' ? intval($_POST['layers']) : null;
$model_size = $_POST['model_size'] !== '' ? floatval($_POST['model_size']) : null;
$stmt = $pdo->prepare("UPDATE models SET name=?, version=?, description=?, layers=?, model_size=? WHERE id=?");
$ok = $stmt->execute([$name, $version, $description, $layers, $model_size, $id]);
if ($ok) {
header('Location: list_tables.php?success=Modelo actualizado');
} else {
header('Location: editar_modelo.php?id=' . $id . '&error=No se pudo actualizar');
}
exit;
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Editar Modelo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container py-5">
<div class="card shadow-sm">
<div class="card-header bg-primary text-white">
Editar Modelo
</div>
<div class="card-body">
<?php if (isset($_GET['error'])): ?>
<div class="alert alert-danger"><?= htmlspecialchars($_GET['error']) ?></div>
<?php endif; ?>
<form method="POST">
<div class="mb-3">
<label class="form-label">Nombre</label>
<input type="text" name="name" class="form-control" value="<?= htmlspecialchars($model['name']) ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Versión</label>
<input type="text" name="version" class="form-control" value="<?= htmlspecialchars($model['version']) ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Descripción</label>
<textarea name="description" class="form-control"><?= htmlspecialchars($model['description']) ?></textarea>
</div>
<div class="mb-3">
<label class="form-label">Capas</label>
<input type="number" name="layers" class="form-control" value="<?= htmlspecialchars($model['layers']) ?>">
</div>
<div class="mb-3">
<label class="form-label">Tamaño/Peso</label>
<input type="number" step="0.01" name="model_size" class="form-control" value="<?= htmlspecialchars($model['model_size']) ?>">
</div>
<button type="submit" class="btn btn-success">Guardar Cambios</button>
<a href="list_tables.php" class="btn btn-secondary">Volver</a>
</form>
</div>
</div>
</div>
</body>
</html>

View File

@ -12,15 +12,19 @@
<body class="bg-light">
<div class="container py-5">
<div class="card shadow-sm">
<div class="card-header bg-secondary text-white">
<h3>Modelos en la Base de Datos</h3>
<div class="card-header bg-secondary text-white d-flex justify-content-between align-items-center">
<h3 class="mb-0">Modelos en la Base de Datos</h3>
<a href="register_model.php" class="btn btn-success"> Agregar Modelo</a>
</div>
<div class="card-body">
<table id="tables" class="table table-striped" style="width:100%">
<thead class="table-dark">
<tr>
<th>Nombre</th>
<th>Acción</th>
<th>Versión</th>
<th>Tamaño/Peso</th>
<th>Capas</th>
<th class="text-center">Acción</th>
</tr>
</thead>
</table>
@ -41,10 +45,27 @@
data: 'name',
title: 'Nombre'
},
{
data: 'version',
title: 'Versión'
},
{
data: 'model_size',
title: 'Tamaño/Peso'
},
{
data: 'layers',
title: 'Capas'
},
{
data: null,
orderable: false,
render: d => `<button class="btn btn-info btn-sm" onclick="alert('ID: '+d.id)">Ver</button>`
className: 'text-center',
title: 'Acción',
render: d => `
<button class="btn btn-info btn-sm me-1" onclick="editarModelo(${d.id})">Editar</button>
<button class="btn btn-danger btn-sm mx-1" onclick="eliminarModelo(${d.id}, this)">Eliminar</button>
`
}
],
language: {
@ -52,6 +73,26 @@
}
});
});
function eliminarModelo(id, btn) {
if (confirm('¿Seguro que deseas eliminar este modelo?')) {
$.post('ajax/delete_model.php', {
id: id
}, function(resp) {
if (resp.success) {
// Elimina la fila de la tabla sin recargar
$('#tables').DataTable().row($(btn).parents('tr')).remove().draw();
} else {
alert('No se pudo eliminar el modelo');
}
}, 'json');
}
}
function editarModelo(id) {
// Redirige al formulario de edición con el ID del modelo
window.location.href = 'editar_modelo.php?id=' + id;
}
</script>
</body>

View File

@ -70,6 +70,7 @@ if (isset($_GET['success'])) {
<input type="text" name="dataset" class="form-control">
</div>
<button type="submit" class="btn btn-success">Registrar Modelo</button>
<a href="list_tables.php" class="btn btn-secondary">Volver a la Lista</a>
</form>
</div>
</div>