From 4d0bd470bb78127966d969044eb92c9249e588f9 Mon Sep 17 00:00:00 2001 From: szabomarton Date: Mon, 18 Nov 2024 13:41:34 +0100 Subject: [PATCH] added JS --- 20241118_JS/feladat1.html | 34 +++++++++++++++++++++++ 20241118_JS/feladat1.js | 57 +++++++++++++++++++++++++++++++++++++++ 20241118_JS/munkasok.json | 1 + 3 files changed, 92 insertions(+) create mode 100644 20241118_JS/feladat1.html create mode 100644 20241118_JS/feladat1.js create mode 100644 20241118_JS/munkasok.json diff --git a/20241118_JS/feladat1.html b/20241118_JS/feladat1.html new file mode 100644 index 0000000..99d39c8 --- /dev/null +++ b/20241118_JS/feladat1.html @@ -0,0 +1,34 @@ + + + + + + Document + + +
+ + + +
+ +
+ + + \ No newline at end of file diff --git a/20241118_JS/feladat1.js b/20241118_JS/feladat1.js new file mode 100644 index 0000000..0f04e2b --- /dev/null +++ b/20241118_JS/feladat1.js @@ -0,0 +1,57 @@ +const url = "http://127.0.0.1/PHP_digivagyok/20241118_JS/munkasok.json"; + +document.getElementById("button").addEventListener("click", function(){ + let value = document.getElementById("selection").value; + console.log(value); + main(value); + + +}); + + +/* +async function main(munkasNumber) { + let xhttp = new XMLHttpRequest(); + xhttp.open("GET", url, true); + + xhttp.onreadystatechange = async function(){ + if (xhttp.readyState === 4){ + if (xhttp.status === 200){ + let sol = await JSON.parse(xhttp.responseText); + + console.log(sol); + + createDomElement(sol[munkasNumber - 1]); + } else if(xhttp.status === 404){ + console.log("Az erőforrás nem található - 404 hibakód"); + } else{ + console.error(`Hiba történt, státuszkód: ${xhttp.status}`); + } + } + } + xhttp.send(); +} +*/ + +async function main(munkasNumber) { + try{ + let response = await fetch(url); + + if (!response.ok){ + throw new Error(`Hiba történt: ${response.status}`); + } + + let adatok = await response.json(); + + createDomElement(adatok[munkasNumber - 1]); + } catch (error) { + console.error(`Hiba ${error}`); + } +} + + +function createDomElement(element) { + let line = document.createElement("div"); + line.innerHTML = `Név: ${element.nev}, Beosztás: ${element.beosztas}, Kor: ${element.kor}, Fizetés:${element.fizetes}Ft`; + document.getElementById("eredmeny").appendChild(line); +} diff --git a/20241118_JS/munkasok.json b/20241118_JS/munkasok.json new file mode 100644 index 0000000..0996c15 --- /dev/null +++ b/20241118_JS/munkasok.json @@ -0,0 +1 @@ +[{"nev":"Jani","beosztas":"sofőr","kor":45,"fizetes":300000},{"nev":"Pali","beosztas":"naplopó","kor":37,"fizetes":250000},{"nev":"Laci","beosztas":"vezető","kor":46,"fizetes":380000},{"nev":"Zsolt","beosztas":"főnök","kor":25,"fizetes":3000000},{"nev":"Tihamér","beosztas":"sofőr","kor":30,"fizetes":300000}] \ No newline at end of file