Implementado script principal

This commit is contained in:
unknown
2025-04-27 00:00:58 +02:00
parent e8958dc893
commit ae456c8890
4 changed files with 100 additions and 4 deletions

View File

@ -1,5 +1,5 @@
<?php
namespace App\Controllers\Catalogo;
namespace App\Controllers\Importadores;
use App\Controllers\BaseResourceController;
use App\Entities\Catalogo\CatalogoLibroEntity;

View File

@ -8,12 +8,33 @@
<div class="card card-info">
<div class="card-header">
<h3 class="card-title"><?= lang('Importador.listingPage') ?></h3>
<h3 class="card-title"><?= lang('Importador.listingPage') ?></h3>
</div><!--//.card-header -->
<div class="card-body">
<?= view('themes/_commonPartialsBs/_alertBoxes'); ?>
<input type="file" id="excelFile" accept=".xlsx, .xls">
<div id="tableContainer"></div>
<br>
<button id="importBtn">Importar</button>
<table id="excelTable" class="display">
<thead>
<tr>
<th>cnt_pedida</th>
<th>precio_compra</th>
<th>idlinea</th>
<th>input</th>
<th>descripcion</th>
<th>Acción</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div><!--//.card-body -->
<div class="card-footer">
@ -46,5 +67,6 @@
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="<?= site_url("/themes/vuexy/vendor/libs/datatables-sk/plugins/pdfmake/vfs_fonts.js") ?>"></script>
<script src="<?= site_url('themes/vuexy/vendor/libs/sweetalert2/sweetalert2.js') ?>"></script>
<script type="module" src="<?= site_url("assets/js/safekat/pages/importador/catalogo_tool.js") ?>"></script>
<script src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js"></script>
<script type="module" src="<?= site_url("assets/js/safekat/pages/importadores/catalogo/catalogo_tool.js") ?>"></script>
<?= $this->endSection() ?>

View File

@ -0,0 +1,74 @@
document.addEventListener('DOMContentLoaded', function () {
// Columnas requeridas
const REQUIRED_COLUMNS = ["cnt_pedida", "precio_compra", "idlinea", "input", "descripcion"];
let dataTable; // referencia al DataTable
// Inicializa el DataTable vacío
dataTable = $('#excelTable').DataTable();
document.getElementById('excelFile').addEventListener('change', function (e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function (e) {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, { type: 'array' });
const firstSheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonData = XLSX.utils.sheet_to_json(firstSheet, { header: 1 });
validateAndLoadDataTable(jsonData);
};
reader.readAsArrayBuffer(file);
});
function validateAndLoadDataTable(data) {
const headers = data[0].map(h => h.toString().trim());
const missing = REQUIRED_COLUMNS.filter(col => !headers.includes(col));
if (missing.length > 0) {
alert('Faltan las siguientes columnas en el Excel: ' + missing.join(', '));
dataTable.clear().draw(); // limpia tabla
return;
}
const indexes = REQUIRED_COLUMNS.map(col => headers.indexOf(col));
const rows = [];
for (let i = 1; i < data.length; i++) {
const rowData = indexes.map(idx => data[i][idx] ?? '');
rowData.push('<button class="deleteRow">Eliminar</button>'); // Agrega botón
rows.push(rowData);
}
dataTable.clear().rows.add(rows).draw();
// Agregar eventos a los nuevos botones de eliminar
$('#excelTable tbody').on('click', '.deleteRow', function () {
dataTable.row($(this).parents('tr')).remove().draw();
});
}
document.getElementById('importBtn').addEventListener('click', function () {
const allData = dataTable.rows().data().toArray();
// Eliminar la última columna (botón) de cada fila
const rowsToSend = allData.map(row => row.slice(0, -1));
fetch('/importar', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': '<?= csrf_hash() ?>'
},
body: JSON.stringify({ data: rowsToSend })
}).then(res => res.json())
.then(response => {
alert(response.message);
});
});
});