Dateien nach "backend/src/main/webapp" hochladen
This commit is contained in:
@@ -4,21 +4,16 @@
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
||||
<title>Config</title>
|
||||
<!-- Handsontable CSS -->
|
||||
<link rel="stylesheet" href="/backend/tabulator/css/tabulator.min.css">
|
||||
|
||||
<style>
|
||||
body {
|
||||
font-family: sans-serif;
|
||||
margin: 20px;
|
||||
}
|
||||
#hot {
|
||||
margin-top: 10px;
|
||||
width: 100%;
|
||||
min-height: 300px; /* damit man etwas sieht */
|
||||
}
|
||||
button {
|
||||
margin-right: 10px;
|
||||
}
|
||||
*{box-sizing:border-box} body{font-family:Arial,sans-serif;margin:0;padding:16px;background:#f4f4f4} h1{margin:0 0 12px}
|
||||
#hot,.tabulator{width:100%;max-width:100%}.toolbar{display:grid;grid-template-columns:auto minmax(240px,1fr) auto auto;gap:10px;align-items:center;margin-bottom:12px}
|
||||
button,input,select{min-height:44px;font:inherit}button{padding:10px 18px;border:0;border-radius:6px;background:#e30613;color:#fff;cursor:pointer}
|
||||
#globalSearch{width:100%;padding:8px 12px;border:1px solid #aaa;border-radius:6px}#activeFilter{padding:8px;border:1px solid #aaa;border-radius:6px;background:#fff}.filter-count{white-space:nowrap;color:#555}
|
||||
.tabulator-cell.value-editable{background:#fffbea;cursor:text}.switch-cell{cursor:pointer!important;user-select:none}
|
||||
.parameter-switch{position:relative;display:inline-block;width:48px;height:26px;pointer-events:none}.parameter-switch-track{position:absolute;inset:0;border-radius:999px;background:#9ca3af}.parameter-switch-knob{position:absolute;top:3px;left:3px;width:20px;height:20px;border-radius:50%;background:#fff;box-shadow:0 1px 3px #555;transition:transform .15s}.parameter-switch.is-on .parameter-switch-track{background:#22c55e}.parameter-switch.is-on .parameter-switch-knob{transform:translateX(22px)}
|
||||
@media(max-width:760px){body{padding:8px}.toolbar{grid-template-columns:1fr}.toolbar>*{width:100%}.tabulator{overflow-x:auto}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -26,14 +21,8 @@
|
||||
<h1>Station <span id="stationNumberDisplay"></span></h1>
|
||||
|
||||
|
||||
<!-- Zwei Buttons -->
|
||||
|
||||
<button onclick="setConfig()">Speichern (setConfig)</button>
|
||||
|
||||
<div id="hot"></div>
|
||||
<!-- Einbinden der Handsontable-JS-Bibliothek -->
|
||||
<script src="/backend/handsontable/handsontable/dist/handsontable.full.min.js"></script>
|
||||
<link rel="stylesheet" href="/backend/handsontable/handsontable/dist/handsontable.full.min.css">
|
||||
<div class="toolbar"><button type="button" onclick="setConfig()">Konfiguration speichern</button><input id="globalSearch" type="search" placeholder="Code, Beschreibung oder Wert suchen ..."><select id="activeFilter"><option value="all">Alle Parameter</option><option value="active">Nur eingeschaltet</option><option value="inactive">Nur ausgeschaltet</option></select><span id="filterCount" class="filter-count"></span></div><div id="hot"></div>
|
||||
<script src="/backend/tabulator/js/tabulator.min.js"></script>
|
||||
<link rel="stylesheet" href="/backend/ios.css">
|
||||
<script>
|
||||
/*
|
||||
@@ -317,81 +306,23 @@ function transformData(rawArray) {
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
3) Custom Renderer: boolean => Checkbox, sonst => Text
|
||||
*/
|
||||
function booleanOrTextRenderer(hotInstance, td, row, col, prop, value, cellProperties) {
|
||||
if (typeof value === "boolean") {
|
||||
// Standard-CheckboxRenderer => klickbare Checkbox
|
||||
Handsontable.renderers.CheckboxRenderer.apply(this, arguments);
|
||||
} else {
|
||||
// Andernfalls normaler Text
|
||||
Handsontable.renderers.TextRenderer.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
4) Handsontable initialisieren
|
||||
*/
|
||||
let hot;
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const container = document.getElementById('hot');
|
||||
// Start mit transformierten localData
|
||||
const transformedData = transformData(myDataRaw);
|
||||
|
||||
hot = new Handsontable(container, {
|
||||
data: transformedData,
|
||||
rowHeaders: true,
|
||||
colHeaders: [
|
||||
"code_nr",
|
||||
"Description",
|
||||
"code_wert",
|
||||
"Change by",
|
||||
"Changed Date",
|
||||
"used (checkbox)"
|
||||
],
|
||||
columns: [
|
||||
{
|
||||
data: "code_nr",
|
||||
type: "numeric",
|
||||
readOnly: true
|
||||
},
|
||||
{
|
||||
data: "description",
|
||||
type: "text",
|
||||
readOnly: true
|
||||
},
|
||||
{
|
||||
data: "code_wert",
|
||||
renderer: booleanOrTextRenderer
|
||||
},
|
||||
{
|
||||
data: "changedby",
|
||||
type: "text",
|
||||
readOnly: true
|
||||
},
|
||||
{
|
||||
data: "timestamp",
|
||||
type: "text",
|
||||
readOnly: true
|
||||
},
|
||||
{
|
||||
data: "used",
|
||||
renderer: booleanOrTextRenderer
|
||||
}
|
||||
],
|
||||
stretchH: "all",
|
||||
autoColumnSize: false,
|
||||
manualColumnResize: true,
|
||||
colWidths: [82, 300, 120, 120, 170, 120],
|
||||
wordWrap: false,
|
||||
licenseKey: "non-commercial-and-evaluation"
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
/* 3) Tabulator: editierbare Werte, Suche und sortierbares Ein/Aus am Tabellenende */
|
||||
function switchFormatter(cell){const on=cell.getValue()===true;const e=document.createElement("span");e.className="parameter-switch"+(on?" is-on":"");e.innerHTML='<span class="parameter-switch-track"></span><span class="parameter-switch-knob"></span>';return e}
|
||||
function valueFormatter(cell){return typeof cell.getValue()==="boolean"?switchFormatter(cell):(cell.getValue()??"")}
|
||||
function valueEditor(cell,onRendered,success,cancel){const value=cell.getValue();if(typeof value==="boolean"){success(!value);return false}const input=document.createElement("input");input.type="text";input.value=value??"";input.style.cssText="width:100%;height:100%;border:0;padding:6px";onRendered(()=>{input.focus();input.select()});input.addEventListener("blur",()=>success(input.value));input.addEventListener("keydown",e=>{if(e.key==="Enter")success(input.value);if(e.key==="Escape")cancel()});return input}
|
||||
function toggleCell(event,cell){event.preventDefault();event.stopPropagation();cell.setValue(cell.getValue()!==true)}
|
||||
function booleanSorter(a,b){return Number(a===true)-Number(b===true)}
|
||||
function applyTableFilters(){if(!table)return;const q=document.getElementById("globalSearch").value.trim().toLowerCase(),state=document.getElementById("activeFilter").value;table.setFilter(data=>{const text=!q||[data.code_nr,data.description,data.code_wert,data.changedby,data.timestamp].some(v=>String(v??"").toLowerCase().includes(q));const status=state==="all"||(state==="active"?data.used===true:data.used!==true);return text&&status});updateFilterCount()}
|
||||
function updateFilterCount(){if(table)document.getElementById("filterCount").textContent=table.getRows("active").length+" / "+table.getData().length}
|
||||
let table;
|
||||
document.addEventListener("DOMContentLoaded",function(){table=new Tabulator("#hot",{data:transformData(myDataRaw),index:"code_nr",height:Math.max(320,window.innerHeight-160)+"px",layout:"fitDataStretch",responsiveLayout:false,movableColumns:true,placeholder:"Keine Parameter gefunden",rowHeader:{formatter:"rownum",headerSort:false,width:48},columns:[
|
||||
{title:"Code",field:"code_nr",sorter:"number",width:90,frozen:true,headerFilter:"input"},
|
||||
{title:"Beschreibung",field:"description",minWidth:300,widthGrow:4,headerFilter:"input"},
|
||||
{title:"Wert",field:"code_wert",minWidth:160,widthGrow:2,formatter:valueFormatter,editor:valueEditor,editable:true,headerFilter:"input",cssClass:"value-editable",cellClick:function(e,c){if(typeof c.getValue()==="boolean")toggleCell(e,c);else c.edit(true)}},
|
||||
{title:"Geändert von",field:"changedby",minWidth:130,headerFilter:"input"},{title:"Geändert am",field:"timestamp",minWidth:170,headerFilter:"input"},
|
||||
{title:"Ein / Aus",field:"used",width:120,hozAlign:"center",headerHozAlign:"center",formatter:switchFormatter,cellClick:toggleCell,headerSort:true,sorter:booleanSorter,cssClass:"switch-cell"}
|
||||
]});
|
||||
table.on("dataFiltered",updateFilterCount);table.on("dataLoaded",updateFilterCount);document.getElementById("globalSearch").addEventListener("input",applyTableFilters);document.getElementById("activeFilter").addEventListener("change",applyTableFilters);updateFilterCount();window.addEventListener("resize",()=>{table.setHeight(Math.max(320,window.innerHeight-160));table.redraw(true)})});
|
||||
/*
|
||||
5a) getConfig() =>
|
||||
Lädt vom Server (z.B. '/backend/api/getStationConfig') und merged
|
||||
@@ -430,9 +361,9 @@ function getConfig() {
|
||||
}
|
||||
});
|
||||
|
||||
// Neu transformieren & in Handsontable laden
|
||||
// Neu transformieren & in Tabulator laden
|
||||
const newData = transformData(myDataRaw);
|
||||
hot.loadData(newData);
|
||||
table.replaceData(newData).then(applyTableFilters);
|
||||
|
||||
//alert("Daten gemerged & aktualisiert!");
|
||||
})
|
||||
@@ -442,7 +373,7 @@ function getConfig() {
|
||||
});
|
||||
}
|
||||
|
||||
7
|
||||
|
||||
function formatTimestampToYMDHMS(timestampNumber) {
|
||||
const dateObj = new Date(timestampNumber); // ms seit 1970
|
||||
|
||||
@@ -464,8 +395,8 @@ function formatTimestampToYMDHMS(timestampNumber) {
|
||||
=> sendet an '/backend/api/setStationConfig'
|
||||
*/
|
||||
function setConfig() {
|
||||
// 1) Daten aus Handsontable
|
||||
const allRows = hot.getSourceData();
|
||||
// 1) Daten aus Tabulator
|
||||
const allRows = table.getData();
|
||||
|
||||
// 2) Filtern: Nur used === true
|
||||
const usedRows = allRows.filter(row => row.used === true);
|
||||
|
||||
@@ -5,146 +5,130 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
||||
<title>Excel Contact Import & Export</title>
|
||||
<script src="/backend/xlsx/xlsx.full.min.js"></script>
|
||||
<script src="/backend/handsontable/handsontable/dist/handsontable.full.min.js"></script>
|
||||
<link rel="stylesheet" href="/backend/handsontable/handsontable/dist/handsontable.full.min.css">
|
||||
<link rel="stylesheet" href="/backend/tabulator/css/tabulator.min.css">
|
||||
<script src="/backend/tabulator/js/tabulator.min.js"></script>
|
||||
<link rel="stylesheet" href="/backend/ios.css">
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
text-align: center;
|
||||
background-color: #f4f4f4;
|
||||
padding: 20px;
|
||||
}
|
||||
.container {
|
||||
background: white;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
||||
display: inline-block;
|
||||
}
|
||||
button {
|
||||
margin: 10px;
|
||||
padding: 10px 20px;
|
||||
border: none;
|
||||
background-color: red;
|
||||
color: white;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
}
|
||||
#import-table {
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; padding: 20px; font-family: Arial, sans-serif; text-align: center; background-color: #f4f4f4; }
|
||||
.container { display: inline-block; width: min(1400px, 100%); padding: 20px; background: white; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,.1); }
|
||||
.button-area { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 10px; }
|
||||
button { margin: 10px 0; padding: 10px 20px; border: 0; background: red; color: white; border-radius: 5px; cursor: pointer; }
|
||||
button:hover:not(:disabled) { background: #c90000; }
|
||||
button:disabled { background: #999; cursor: not-allowed; }
|
||||
input[type="file"] { max-width: 100%; }
|
||||
#import-table { width: 100%; max-width: 100%; margin-top: 20px; text-align: left; }
|
||||
#status-message { min-height: 22px; margin-top: 12px; font-size: 14px; }
|
||||
.status-success { color: #087a20; } .status-error { color: #b00020; } .status-info { color: #333; }
|
||||
.tabulator { width: 100%; max-width: 100%; border: 1px solid #ccc; font-size: 13px; }
|
||||
.tabulator .tabulator-header, .tabulator .tabulator-header .tabulator-col { background: #ededed; }
|
||||
.tabulator-cell.tabulator-editing { border: 2px solid red !important; }
|
||||
@media (max-width:700px) { body { padding:10px; } .container { padding:12px; } .button-area { flex-direction:column; } button,input[type="file"] { width:100%; } }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h2>Excel Export & Import</h2>
|
||||
<button id="export-button">Kontakt Excel Export</button>
|
||||
<input type="file" id="file-input" accept=".xlsx">
|
||||
<button id="send-button">Daten Senden</button>
|
||||
<div id="import-table"></div>
|
||||
<div class="container">
|
||||
<h2>Excel Export & Import</h2>
|
||||
<div class="button-area">
|
||||
<button id="export-button" type="button">Kontakt Excel Export</button>
|
||||
<input type="file" id="file-input" accept=".xlsx,.xls">
|
||||
<button id="send-button" type="button" disabled>Daten senden</button>
|
||||
</div>
|
||||
|
||||
<div id="status-message" class="status-info" role="status" aria-live="polite"></div>
|
||||
<div id="import-table"></div>
|
||||
</div>
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
let importedData = [];
|
||||
let hot; // Handsontable instance
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
let table = null;
|
||||
let importedHeaders = [];
|
||||
const url = sessionStorage.getItem("url");
|
||||
const stationNumber = sessionStorage.getItem("stationNumber");
|
||||
const fixedHeaders = null;
|
||||
const sourceHeaders = null;
|
||||
const sendButton = document.getElementById("send-button");
|
||||
const fileInput = document.getElementById("file-input");
|
||||
const statusMessage = document.getElementById("status-message");
|
||||
|
||||
function exportExcel() {
|
||||
fetch(url + '/backend/api/getContact', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({'station': sessionStorage.getItem("stationNumber")})
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (!data || data.length === 0) {
|
||||
alert("Keine Daten zum Exportieren.");
|
||||
return;
|
||||
function setStatus(message, type = "info") { statusMessage.textContent = message; statusMessage.className = "status-" + type; }
|
||||
function asText(value) { return value === undefined || value === null ? "" : String(value); }
|
||||
function requireSession() {
|
||||
if (!url) { setStatus("Backend-URL ist in der Session nicht definiert.", "error"); return false; }
|
||||
if (!stationNumber) { setStatus("Stationsnummer ist in der Session nicht definiert.", "error"); return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
const headers = Object.keys(data[0]);
|
||||
const rows = data.map(obj => headers.map(header => obj[header]));
|
||||
function makeColumns(headers) { return headers.map(header => ({ title: header, field: header, editor: "input", minWidth: 120, headerSort: true })); }
|
||||
async function showTable(data, headers) {
|
||||
importedHeaders = headers;
|
||||
if (table) { table.setColumns(makeColumns(headers)); await table.replaceData(data); return; }
|
||||
table = new Tabulator("#import-table", { data, height: Math.max(300, window.innerHeight - 210) + "px", layout: "fitColumns", responsiveLayout: "collapse", movableColumns: true, placeholder: "Keine Daten importiert", rowHeader: { formatter: "rownum", headerSort: false, hozAlign: "center", frozen: true, width: 50 }, columns: makeColumns(headers) });
|
||||
}
|
||||
async function exportExcel() {
|
||||
if (!requireSession()) return;
|
||||
setStatus("Exportdaten werden geladen ...");
|
||||
try {
|
||||
const response = await fetch(url + "/backend/api/getContact", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ station: stationNumber }) });
|
||||
if (!response.ok) throw new Error("HTTP " + response.status + ": " + await response.text());
|
||||
const data = await response.json();
|
||||
if (!Array.isArray(data) || !data.length) throw new Error("Keine Daten zum Exportieren vorhanden.");
|
||||
const headers = fixedHeaders || Object.keys(data[0]);
|
||||
const keys = sourceHeaders || headers;
|
||||
const rows = data.map(item => keys.map(key => asText(item[key])));
|
||||
const worksheet = XLSX.utils.aoa_to_sheet([headers, ...rows]);
|
||||
worksheet["!cols"] = headers.map(header => ({ wch: Math.max(15, header.length + 2) }));
|
||||
const workbook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, "Contacts");
|
||||
XLSX.writeFile(workbook, "ContactsExport.xlsx");
|
||||
})
|
||||
.catch(error => alert("Fehler beim Exportieren: " + error.message));
|
||||
setStatus(data.length + " Zeile(n) exportiert.", "success");
|
||||
} catch (error) { console.error(error); setStatus("Fehler beim Exportieren: " + error.message, "error"); }
|
||||
}
|
||||
|
||||
function importExcelData(event) {
|
||||
const fileInput = document.getElementById("file-input");
|
||||
if (!fileInput.files.length) return;
|
||||
|
||||
function importExcelData() {
|
||||
const file = fileInput.files[0];
|
||||
if (!file) return;
|
||||
sendButton.disabled = true; setStatus("Excel-Datei wird eingelesen ...");
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(event) {
|
||||
const workbook = XLSX.read(event.target.result, { type: "binary" });
|
||||
const sheet = workbook.Sheets[workbook.SheetNames[0]];
|
||||
const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1 });
|
||||
|
||||
if (jsonData.length < 2) return;
|
||||
|
||||
const headers = jsonData[0];
|
||||
importedData = jsonData.slice(1);
|
||||
|
||||
if (hot) {
|
||||
hot.loadData(importedData);
|
||||
hot.updateSettings({ colHeaders: headers });
|
||||
} else {
|
||||
const container = document.getElementById("import-table");
|
||||
hot = new Handsontable(container, {
|
||||
data: importedData,
|
||||
colHeaders: headers,
|
||||
rowHeaders: true,
|
||||
width: '100%',
|
||||
height: 300,
|
||||
stretchH: 'all',
|
||||
autoColumnSize: false,
|
||||
manualColumnResize: true,
|
||||
colWidths: 120,
|
||||
wordWrap: false,
|
||||
licenseKey: 'non-commercial-and-evaluation'
|
||||
});
|
||||
}
|
||||
reader.onload = async event => {
|
||||
try {
|
||||
const workbook = XLSX.read(event.target.result, { type: "array", cellDates: false });
|
||||
if (!workbook.SheetNames.length) throw new Error("Die Datei enthält kein Arbeitsblatt.");
|
||||
const rows = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], { header: 1, defval: "", raw: false });
|
||||
if (rows.length < 2) throw new Error("Die Datei enthält keine Datenzeilen.");
|
||||
const headers = rows[0].map(value => asText(value).trim());
|
||||
if (headers.some(header => !header)) throw new Error("Eine oder mehrere Spaltenüberschriften sind leer.");
|
||||
if (new Set(headers).size !== headers.length) throw new Error("Spaltenüberschriften dürfen nicht doppelt vorkommen.");
|
||||
const data = rows.slice(1).filter(row => row.some(value => asText(value).trim() !== "")).map(row => Object.fromEntries(headers.map((header, index) => [header, asText(row[index])] )));
|
||||
if (!data.length) throw new Error("Keine gefüllten Datenzeilen gefunden.");
|
||||
await showTable(data, headers); sendButton.disabled = false; setStatus(data.length + " Zeile(n) importiert.", "success");
|
||||
} catch (error) { console.error(error); setStatus("Fehler beim Import: " + error.message, "error"); }
|
||||
};
|
||||
reader.readAsBinaryString(fileInput.files[0]);
|
||||
}
|
||||
|
||||
function sendExcelData() {
|
||||
const jsonData = importedData.map(row => {
|
||||
let obj = {};
|
||||
hot.getColHeader().forEach((header, index) => {
|
||||
obj[header] = row[index] || "";
|
||||
});
|
||||
return obj;
|
||||
});
|
||||
|
||||
if (!url) {
|
||||
alert("Backend URL is not defined.");
|
||||
return;
|
||||
}
|
||||
fetch(url + '/backend/api/updateContacts', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ station: stationNumber, data: jsonData })
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(result => alert("Daten erfolgreich gesendet!"))
|
||||
.catch(error => alert("Fehler beim Senden der Daten: " + error.message));
|
||||
reader.onerror = () => setStatus("Die Datei konnte nicht gelesen werden.", "error");
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
async function sendExcelData() {
|
||||
if (!table || !importedHeaders.length) { setStatus("Es wurden noch keine Daten importiert.", "error"); return; }
|
||||
if (!requireSession()) return;
|
||||
const data = table.getData().map(row => Object.fromEntries(importedHeaders.map(header => [header, asText(row[header])] )));
|
||||
sendButton.disabled = true; setStatus("Daten werden gesendet ...");
|
||||
try {
|
||||
const response = await fetch(url + "/backend/api/updateContacts", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ station: stationNumber, data }) });
|
||||
if (!response.ok) throw new Error("HTTP " + response.status + ": " + await response.text());
|
||||
const contentType = response.headers.get("content-type") || "";
|
||||
if (contentType.includes("application/json")) await response.json();
|
||||
setStatus(data.length + " Zeile(n) erfolgreich gesendet.", "success");
|
||||
} catch (error) { console.error(error); setStatus("Fehler beim Senden: " + error.message, "error"); }
|
||||
finally { sendButton.disabled = false; }
|
||||
}
|
||||
function resizeImportTable() {
|
||||
if (!table) return;
|
||||
table.setHeight(Math.max(300, window.innerHeight - 210));
|
||||
table.redraw(true);
|
||||
}
|
||||
|
||||
window.addEventListener("resize", resizeImportTable);
|
||||
window.addEventListener("orientationchange", resizeImportTable);
|
||||
document.getElementById("export-button").addEventListener("click", exportExcel);
|
||||
document.getElementById("file-input").addEventListener("change", importExcelData);
|
||||
document.getElementById("send-button").addEventListener("click", sendExcelData);
|
||||
});
|
||||
fileInput.addEventListener("change", importExcelData);
|
||||
sendButton.addEventListener("click", sendExcelData);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -3,152 +3,132 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
||||
<title>Excel Contact Import & Export</title>
|
||||
<title>Excel ERP Groups Import & Export</title>
|
||||
<script src="/backend/xlsx/xlsx.full.min.js"></script>
|
||||
<script src="/backend/handsontable/handsontable/dist/handsontable.full.min.js"></script>
|
||||
<link rel="stylesheet" href="/backend/handsontable/handsontable/dist/handsontable.full.min.css">
|
||||
<link rel="stylesheet" href="/backend/tabulator/css/tabulator.min.css">
|
||||
<script src="/backend/tabulator/js/tabulator.min.js"></script>
|
||||
<link rel="stylesheet" href="/backend/ios.css">
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
text-align: center;
|
||||
background-color: #f4f4f4;
|
||||
padding: 20px;
|
||||
}
|
||||
.container {
|
||||
background: white;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
||||
display: inline-block;
|
||||
}
|
||||
button {
|
||||
margin: 10px;
|
||||
padding: 10px 20px;
|
||||
border: none;
|
||||
background-color: red;
|
||||
color: white;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
}
|
||||
#import-table {
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; padding: 20px; font-family: Arial, sans-serif; text-align: center; background-color: #f4f4f4; }
|
||||
.container { display: inline-block; width: min(1400px, 100%); padding: 20px; background: white; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,.1); }
|
||||
.button-area { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 10px; }
|
||||
button { margin: 10px 0; padding: 10px 20px; border: 0; background: red; color: white; border-radius: 5px; cursor: pointer; }
|
||||
button:hover:not(:disabled) { background: #c90000; }
|
||||
button:disabled { background: #999; cursor: not-allowed; }
|
||||
input[type="file"] { max-width: 100%; }
|
||||
#import-table { width: 100%; max-width: 100%; margin-top: 20px; text-align: left; }
|
||||
#status-message { min-height: 22px; margin-top: 12px; font-size: 14px; }
|
||||
.status-success { color: #087a20; } .status-error { color: #b00020; } .status-info { color: #333; }
|
||||
.tabulator { width: 100%; max-width: 100%; border: 1px solid #ccc; font-size: 13px; }
|
||||
.tabulator .tabulator-header, .tabulator .tabulator-header .tabulator-col { background: #ededed; }
|
||||
.tabulator-cell.tabulator-editing { border: 2px solid red !important; }
|
||||
@media (max-width:700px) { body { padding:10px; } .container { padding:12px; } .button-area { flex-direction:column; } button,input[type="file"] { width:100%; } }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h2>Excel Export & Import</h2>
|
||||
<button id="export-button">ERP Groups Excel Export</button>
|
||||
<input type="file" id="file-input" accept=".xlsx">
|
||||
<button id="send-button">Daten Senden</button>
|
||||
<div id="import-table"></div>
|
||||
<div class="container">
|
||||
<h2>Excel Export & Import</h2>
|
||||
<div class="button-area">
|
||||
<button id="export-button" type="button">ERP Groups Excel Export</button>
|
||||
<input type="file" id="file-input" accept=".xlsx,.xls">
|
||||
<button id="send-button" type="button" disabled>Daten senden</button>
|
||||
</div>
|
||||
|
||||
<div id="status-message" class="status-info" role="status" aria-live="polite"></div>
|
||||
<div id="import-table"></div>
|
||||
</div>
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
let importedData = [];
|
||||
let hot; // Handsontable instance
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
let table = null;
|
||||
let importedHeaders = [];
|
||||
const url = sessionStorage.getItem("url");
|
||||
const stationNumber = sessionStorage.getItem("stationNumber");
|
||||
const fixedHeaders = ["ERP_GROUP_NUMBER", "ERP_GROUP_DESC", "KAP_NR"];
|
||||
const sourceHeaders = ["erp_grp_nr", "erp_grp_bez", "kap_nr"];
|
||||
const sendButton = document.getElementById("send-button");
|
||||
const fileInput = document.getElementById("file-input");
|
||||
const statusMessage = document.getElementById("status-message");
|
||||
|
||||
function exportExcel() {
|
||||
fetch(url + '/backend/api/getERPGroups', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ station: stationNumber, })
|
||||
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (!data || data.length === 0) {
|
||||
alert("Keine Daten zum Exportieren.");
|
||||
return;
|
||||
function setStatus(message, type = "info") { statusMessage.textContent = message; statusMessage.className = "status-" + type; }
|
||||
function asText(value) { return value === undefined || value === null ? "" : String(value); }
|
||||
function requireSession() {
|
||||
if (!url) { setStatus("Backend-URL ist in der Session nicht definiert.", "error"); return false; }
|
||||
if (!stationNumber) { setStatus("Stationsnummer ist in der Session nicht definiert.", "error"); return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
// Gewünschte Reihenfolge festlegen
|
||||
const headers = ["erp_grp_nr", "erp_grp_bez", "kap_nr"];
|
||||
const headersNew = ["ERP_GROUP_NUMBER", "ERP_GROUP_DESC", "KAP_NR"];
|
||||
const rows = data.map(obj => headers.map(header => obj[header]));
|
||||
|
||||
const worksheet = XLSX.utils.aoa_to_sheet([headersNew, ...rows]);
|
||||
function makeColumns(headers) { return headers.map(header => ({ title: header, field: header, editor: "input", minWidth: 120, headerSort: true })); }
|
||||
async function showTable(data, headers) {
|
||||
importedHeaders = headers;
|
||||
if (table) { table.setColumns(makeColumns(headers)); await table.replaceData(data); return; }
|
||||
table = new Tabulator("#import-table", { data, height: Math.max(300, window.innerHeight - 210) + "px", layout: "fitColumns", responsiveLayout: "collapse", movableColumns: true, placeholder: "Keine Daten importiert", rowHeader: { formatter: "rownum", headerSort: false, hozAlign: "center", frozen: true, width: 50 }, columns: makeColumns(headers) });
|
||||
}
|
||||
async function exportExcel() {
|
||||
if (!requireSession()) return;
|
||||
setStatus("Exportdaten werden geladen ...");
|
||||
try {
|
||||
const response = await fetch(url + "/backend/api/getERPGroups", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ station: stationNumber }) });
|
||||
if (!response.ok) throw new Error("HTTP " + response.status + ": " + await response.text());
|
||||
const data = await response.json();
|
||||
if (!Array.isArray(data) || !data.length) throw new Error("Keine Daten zum Exportieren vorhanden.");
|
||||
const headers = fixedHeaders || Object.keys(data[0]);
|
||||
const keys = sourceHeaders || headers;
|
||||
const rows = data.map(item => keys.map(key => asText(item[key])));
|
||||
const worksheet = XLSX.utils.aoa_to_sheet([headers, ...rows]);
|
||||
worksheet["!cols"] = headers.map(header => ({ wch: Math.max(15, header.length + 2) }));
|
||||
const workbook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, "ERPGroup");
|
||||
XLSX.writeFile(workbook, "ERPGropExport.xlsx");
|
||||
})
|
||||
.catch(error => alert("Fehler beim Exportieren: " + error.message));
|
||||
XLSX.writeFile(workbook, "ERPGroupExport.xlsx");
|
||||
setStatus(data.length + " Zeile(n) exportiert.", "success");
|
||||
} catch (error) { console.error(error); setStatus("Fehler beim Exportieren: " + error.message, "error"); }
|
||||
}
|
||||
|
||||
function importExcelData(event) {
|
||||
const fileInput = document.getElementById("file-input");
|
||||
if (!fileInput.files.length) return;
|
||||
|
||||
function importExcelData() {
|
||||
const file = fileInput.files[0];
|
||||
if (!file) return;
|
||||
sendButton.disabled = true; setStatus("Excel-Datei wird eingelesen ...");
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(event) {
|
||||
const workbook = XLSX.read(event.target.result, { type: "binary" });
|
||||
const sheet = workbook.Sheets[workbook.SheetNames[0]];
|
||||
const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1 });
|
||||
|
||||
if (jsonData.length < 2) return;
|
||||
|
||||
const headers = jsonData[0];
|
||||
importedData = jsonData.slice(1);
|
||||
|
||||
if (hot) {
|
||||
hot.loadData(importedData);
|
||||
hot.updateSettings({ colHeaders: headers });
|
||||
} else {
|
||||
const container = document.getElementById("import-table");
|
||||
hot = new Handsontable(container, {
|
||||
data: importedData,
|
||||
colHeaders: headers,
|
||||
rowHeaders: true,
|
||||
width: '100%',
|
||||
height: 300,
|
||||
stretchH: 'all',
|
||||
autoColumnSize: false,
|
||||
manualColumnResize: true,
|
||||
colWidths: 120,
|
||||
wordWrap: false,
|
||||
licenseKey: 'non-commercial-and-evaluation'
|
||||
});
|
||||
}
|
||||
reader.onload = async event => {
|
||||
try {
|
||||
const workbook = XLSX.read(event.target.result, { type: "array", cellDates: false });
|
||||
if (!workbook.SheetNames.length) throw new Error("Die Datei enthält kein Arbeitsblatt.");
|
||||
const rows = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], { header: 1, defval: "", raw: false });
|
||||
if (rows.length < 2) throw new Error("Die Datei enthält keine Datenzeilen.");
|
||||
const headers = rows[0].map(value => asText(value).trim());
|
||||
if (headers.some(header => !header)) throw new Error("Eine oder mehrere Spaltenüberschriften sind leer.");
|
||||
if (new Set(headers).size !== headers.length) throw new Error("Spaltenüberschriften dürfen nicht doppelt vorkommen.");
|
||||
const data = rows.slice(1).filter(row => row.some(value => asText(value).trim() !== "")).map(row => Object.fromEntries(headers.map((header, index) => [header, asText(row[index])] )));
|
||||
if (!data.length) throw new Error("Keine gefüllten Datenzeilen gefunden.");
|
||||
await showTable(data, headers); sendButton.disabled = false; setStatus(data.length + " Zeile(n) importiert.", "success");
|
||||
} catch (error) { console.error(error); setStatus("Fehler beim Import: " + error.message, "error"); }
|
||||
};
|
||||
reader.readAsBinaryString(fileInput.files[0]);
|
||||
}
|
||||
|
||||
function sendExcelData() {
|
||||
const jsonData = importedData.map(row => {
|
||||
let obj = {};
|
||||
hot.getColHeader().forEach((header, index) => {
|
||||
obj[header] = row[index] || "";
|
||||
});
|
||||
return obj;
|
||||
});
|
||||
|
||||
if (!url) {
|
||||
alert("Backend URL is not defined.");
|
||||
return;
|
||||
}
|
||||
fetch(url + '/backend/api/updateERP', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ station: stationNumber, data: jsonData })
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(result => alert("Daten erfolgreich gesendet!"))
|
||||
.catch(error => alert("Fehler beim Senden der Daten: " + error.message));
|
||||
reader.onerror = () => setStatus("Die Datei konnte nicht gelesen werden.", "error");
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
async function sendExcelData() {
|
||||
if (!table || !importedHeaders.length) { setStatus("Es wurden noch keine Daten importiert.", "error"); return; }
|
||||
if (!requireSession()) return;
|
||||
const data = table.getData().map(row => Object.fromEntries(importedHeaders.map(header => [header, asText(row[header])] )));
|
||||
sendButton.disabled = true; setStatus("Daten werden gesendet ...");
|
||||
try {
|
||||
const response = await fetch(url + "/backend/api/updateERP", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ station: stationNumber, data }) });
|
||||
if (!response.ok) throw new Error("HTTP " + response.status + ": " + await response.text());
|
||||
const contentType = response.headers.get("content-type") || "";
|
||||
if (contentType.includes("application/json")) await response.json();
|
||||
setStatus(data.length + " Zeile(n) erfolgreich gesendet.", "success");
|
||||
} catch (error) { console.error(error); setStatus("Fehler beim Senden: " + error.message, "error"); }
|
||||
finally { sendButton.disabled = false; }
|
||||
}
|
||||
function resizeImportTable() {
|
||||
if (!table) return;
|
||||
table.setHeight(Math.max(300, window.innerHeight - 210));
|
||||
table.redraw(true);
|
||||
}
|
||||
|
||||
window.addEventListener("resize", resizeImportTable);
|
||||
window.addEventListener("orientationchange", resizeImportTable);
|
||||
document.getElementById("export-button").addEventListener("click", exportExcel);
|
||||
document.getElementById("file-input").addEventListener("change", importExcelData);
|
||||
document.getElementById("send-button").addEventListener("click", sendExcelData);
|
||||
});
|
||||
fileInput.addEventListener("change", importExcelData);
|
||||
sendButton.addEventListener("click", sendExcelData);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user