Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BC): agregando datos a una tabla desde un formulario #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions README.md

This file was deleted.

247 changes: 247 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Empleado</title>

</head>
<body>

<h1>Empleado</h1>
<form id= "transactionForm">
<div class="formField">
<label for="transactionCode">Codigo</label>
<input id="transactionCode" name="transactionCode">
</div>
<div class="formField">
<input id= "FrontEnd" type="radio" name="transactionType" value="FRONT">
<label for="FrontEnd">FrontEnd</label>
<input id= "BackEnd" type="radio" name="transactionType" value="BACK">
<label for="BackEnd">BackEnd</label>
</div>
<div class="formField">
<label for="transactionName">Nombre</label>
<input id="transactionName" name="transactionName">
</div>
<div class="formField">
<label for="transactionLastName">Apellido</label>
<input id="transactionLastName" name="transactionLastName">
</div>
<div class="formField">
<label for="transactionDescription">Descripcion</label>
<input id="transactionDescription" name="transactionDescription">
</div>
<div class="formField">
<label for="transactionAmount">Salario</label>
<input id="transactionAmount" name="transactionAmount">
</div>

<div class="formField">
<label for="transactionLink">Linkedin</label>
<input id="transactionLink" name="transactionLink">
</div>
<div class="formField">
<button id="botonDeEnvio" onclick="location.reload()"> Grabar</button>
</div>
</form>
<div class="tableFormat">
<table id="transactionTable">
<tr data-transaction-id="">
<th>
Area
</th>
<th>
Nombre
</th>
<th>
Apellido
</th>
<th>
Descripcion
</th>
<th>
Salario
</th>
<th>
Linkedin
</th>
<th>
Eliminar
</th>
<th>
Ver
</th>
</tr>
</table>
</div>

<script>
const form = document.getElementById("transactionForm")
form.addEventListener("submit", function(event){
event.preventDefault();
let transactionFormData = new FormData(form);
let transactionObj = convertFormDataToTransactionObj(transactionFormData)
insertRowInTransactionTable(transactionObj);
saveTransactionObj(transactionObj);
form.reset();
})

document.addEventListener("DOMContentLoaded", function(event){
let transactionObjArr = JSON.parse(localStorage.getItem("transactionData")) || [];
transactionObjArr.forEach(function(arrayElement){
insertRowInTransactionTable(arrayElement)
console.log("se inserta el elemento");
});
})

function getNewTransactionId(){
let lastTransactionId = localStorage.getItem("lastTransactionId") || "-1";
let newTransactionId = JSON.parse(lastTransactionId) + 1;
localStorage.setItem("lastTransactionId", JSON.stringify(newTransactionId));
return newTransactionId;
}

function convertFormDataToTransactionObj(transactionFormData) {
let transactionType = transactionFormData.get("transactionType")
let transactionName = transactionFormData.get("transactionName")
let transactionLastName = transactionFormData.get("transactionLastName")
let transactionDescription = transactionFormData.get("transactionDescription")
let transactionAmount = transactionFormData.get("transactionAmount")
let transactionLink = transactionFormData.get("transactionLink")
let transactionCode = transactionFormData.get("transactionCode") || null
let transactionId = getNewTransactionId();

return {
"transactionType": transactionType,
"transactionName": transactionName,
"transactionLastName": transactionLastName,
"transactionDescription": transactionDescription,
"transactionAmount": transactionAmount,
"transactionLink": transactionLink,
"transactionId": JSON.parse(transactionCode) || transactionId,
}
}

function insertRowInTransactionTable(transactionObj) {
let transactionTableRef = document.getElementById("transactionTable");

let newTransactionRowRef = transactionTableRef.insertRow(-1);
newTransactionRowRef.setAttribute("transactionId", transactionObj["transactionId"])
let newTypeCellRef = newTransactionRowRef.insertCell(0)
newTypeCellRef.textContent = transactionObj["transactionType"];

newTypeCellRef = newTransactionRowRef.insertCell(1)
newTypeCellRef.textContent = transactionObj["transactionName"];

newTypeCellRef = newTransactionRowRef.insertCell(2)
newTypeCellRef.textContent = transactionObj["transactionLastName"];

newTypeCellRef = newTransactionRowRef.insertCell(3)
newTypeCellRef.textContent = transactionObj["transactionDescription"];

newTypeCellRef = newTransactionRowRef.insertCell(4)
newTypeCellRef.textContent = transactionObj["transactionAmount"];

newTypeCellRef = newTransactionRowRef.insertCell(5)
newTypeCellRef.textContent = transactionObj["transactionLink"];

let newDeleteCell = newTransactionRowRef.insertCell(6);
let deleteButton = document.createElement("button");
deleteButton.textContent = "Eliminar";
newDeleteCell.appendChild(deleteButton);

deleteButton.addEventListener("click", (event) => {
let transactionRow = event.target.parentNode.parentNode;
let transactionId = transactionRow.getAttribute("data-transaction-id");
transactionRow.remove();
deleteTransactionObj(transactionId);
})

let newSelectCell = newTransactionRowRef.insertCell(7);
let selectButton = document.createElement("button");
selectButton.textContent = "Ver";
newSelectCell.appendChild(selectButton);

selectButton.addEventListener("click", (event) => {
let transactionRow = event.target.parentNode.parentNode;
let transactionId = transactionRow.getAttribute("transactionid");
console.log(transactionId);
getDetailObj(transactionId);
})
}

function getDetailObj(transactionId) {
let transactionDetailObj = JSON.parse(localStorage.getItem("transactionData"));
const transactDetail = transactionDetailObj.find(p => p.transactionId === JSON.parse(transactionId));

document.querySelector("#transactionCode").value = transactDetail.transactionId;
document.querySelector("#transactionName").value = transactDetail.transactionName;
document.querySelector("#transactionLastName").value = transactDetail.transactionLastName;
document.querySelector("#transactionDescription").value = transactDetail.transactionDescription;
document.querySelector("#transactionAmount").value = transactDetail.transactionAmount;
document.querySelector("#transactionLink").value = transactDetail.transactionLink;
}


// le paso como parametro el transactionId de la transaccion que quiero eliminar
function deleteTransactionObj(transactionId){
//obtengo las transaccion de mi base de datos o en este caso el localstorage
let transactionObjArr = JSON.parse(localStorage.getItem("transactionData"));
//busco el indice o la posicion de la transaccion que quiero eliminar
let transactionIndexInArray = transactionObjArr.findIndex(element => element.transactionId === transactionId);
//elimino el elemento de esa posicion
transactionObjArr.splice(transactionIndexInArray, 1);
//convierto de objeto a json
let transactionObjJson = JSON.stringify(transactionObjArr);
// y finalmente guardo el array de la transaccion con formato JSon al localstorage
localStorage.setItem("transactionData", transactionObjJson);
}

function saveTransactionObj(transactionObj) {
let myTransactionArray = JSON.parse(localStorage.getItem("transactionData")) || [];
const data = myTransactionArray.find(i => i.transactionId === transactionObj.transactionId);
if (data) {
myTransactionArray.map(item => {
let newItem = item;
if (newItem.transactionId === transactionObj.transactionId) {
newItem = Object.assign(newItem, transactionObj);
}
return newItem;
})
} else {
myTransactionArray.push(transactionObj);
}
let transactionObjJson = JSON.stringify(myTransactionArray);
localStorage.setItem("transactionData", transactionObjJson);
form.reset();
}
</script>

<style>
th, td {
text-align: center;
padding: 10px;
border: solid black 1px;
}
.tableFormat table {
margin: 0 auto;
}
/* td {
border: solid red 1px;
} */
h1 {
text-align: center;
}
form {
text-align: center;
}
.formField {
padding-bottom: 20px;
}
#botonDeEnvio {
font-size: 23px;
}
</style>
</body>
</html>