elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Únete al Grupo Steam elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web (Moderador: #!drvy)
| | |-+  contribucion crud simple en memoria
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: contribucion crud simple en memoria  (Leído 3,562 veces)
sapito169


Desconectado Desconectado

Mensajes: 628



Ver Perfil
contribucion crud simple en memoria
« en: 7 Marzo 2023, 04:11 am »

hola les presneto un crud simple en memoria
sin librerias

 
Código
  1. <!DOCTYPE html>
  2. <title>Page Title</title>
  3. </head>
  4.  
  5.  
  6.  
  7. <div id="individual" style="display:none">
  8.  <h1 id="title"></h1>
  9.  codigo <input id="txtCodigo" type="text" disabled><br>
  10.  nombre <input id="txtNombre" type="text"><br>
  11.  precio <input id="txtPrecio" type="text"><br>
  12.  <button id="btnAceptar">aceptar</button>
  13.  <button id="btnCancelar">cancelar</button>
  14. </div>
  15.  
  16. <div id="list"  >
  17.  <h1 id="title">Product Managemen</h1>
  18.  <button id="btnAgregar">agregar</button>
  19.  
  20.  <div id="tblProduct">
  21.  </div>
  22. </div>
  23.  
  24.  
  25. window.onload=function(){
  26. var products = [];
  27. var id=0
  28. //var accion="lista"
  29. changeState("list")
  30.  
  31.  
  32.  
  33. drawTable("tblProduct",products)
  34.  
  35. document.getElementById("btnAgregar").onclick=  clickAdd
  36. document.getElementById("btnAceptar").onclick=  clickAceptar
  37. document.getElementById("btnCancelar").onclick=  clickCancelar
  38.  
  39. function toProduct(){
  40. return {
  41. "id":parseInt(blankZero(document.getElementById("txtCodigo").value)),
  42.    "name": document.getElementById("txtNombre").value ,
  43.    "price":parseInt(blankZero(document.getElementById("txtPrecio").value)),
  44. }
  45. }
  46.  
  47. function changeState(current){
  48. accion=current
  49.    if(accion=="list"){
  50.     document.getElementById("individual").style.display='none'
  51.        document.getElementById("list").style.display='block'
  52.        document.getElementById("title").innerHTML="Product Management"
  53.    }else if(accion=="update"){
  54.     document.getElementById("individual").style.display='block'
  55.        document.getElementById("list").style.display='none'
  56.        document.getElementById("btnAceptar").innerHTML="Actualizar"        
  57.        document.getElementById("title").innerHTML=""
  58.    }else if(accion=="add"){
  59.     document.getElementById("individual").style.display='block'
  60.        document.getElementById("list").style.display='none'
  61.  
  62.        document.getElementById("btnAceptar").innerHTML="Agregar"
  63.        document.getElementById("title").innerHTML=""
  64.    }
  65. }
  66.  
  67. function blankZero(value){
  68. return value==""?0:value
  69. }
  70.  
  71. function  clickAdd(){
  72.   changeState("add")
  73. }
  74.  
  75. function  clickAceptar(){
  76.  if(accion=="add"){
  77.   add(toProduct())
  78.  }
  79.  if(accion=="update"){
  80.   update( parseInt(e.target.getAttribute('x-id') ))
  81.  }
  82.   clean()
  83.  
  84.   changeState("list")
  85. }
  86. function  clickCancelar(){
  87.  clean()
  88.  accion="lista"
  89.  changeState("list")
  90. }
  91.  
  92.  
  93.  
  94. function clean(){
  95.  document.getElementById("txtNombre").focus()
  96.  
  97.  document.getElementById("txtCodigo").value=""  
  98.  document.getElementById("txtNombre").value=""  
  99.  document.getElementById("txtPrecio").value=""  
  100. }
  101.  
  102. function clickRemove(e){
  103. remove( parseInt(e.target.getAttribute('x-id') ))
  104.     clean()
  105. }
  106.  
  107. function clickUpadte(e){
  108.    changeState("update")
  109. update(parseInt(e.target.getAttribute('x-id') ) )
  110. }
  111.  
  112. function add(toAdd){
  113. id++
  114. toAdd.id=id
  115. products.push(toAdd);
  116. drawTable("tblProduct",products)
  117.  
  118. }
  119. function remove(id){
  120. products = products.filter(p=>p.id!==id)
  121. drawTable("tblProduct",products)
  122.  
  123. }
  124. function update(id){
  125. var index = products.findIndex(p=>p.id==id)
  126.  
  127.  
  128.  
  129. document.getElementById("txtCodigo").value=products[index].id  
  130. document.getElementById("txtNombre").value=products[index].name
  131. document.getElementById("txtPrecio").value=products[index].price  
  132.  
  133. }
  134.  
  135. function drawTable(idTable,products){
  136. var table="<table>\n"
  137.  
  138. table+="<tr>"
  139.    table+="<th>id</th>"
  140.    table+="<th>nombre</th>"
  141. table+="<th>precio</th>"
  142.    table+="<th>accion</th>"
  143.   table+="</tr>\n"
  144.  
  145.  for(var c=0;c<products.length;c++){
  146.    var current=products[c]
  147.   table+="<tr>"
  148.    table+="<td>"
  149.    table+=current.id
  150.    table+="</td>"
  151.    table+="<td>"
  152.    table+=current.name
  153.    table+="</td>"
  154.    table+="<td>"
  155.    table+=current.price
  156.    table+="</td>"
  157.  
  158.    table+="<td>"
  159.    table+="<button class='update' x-id='"
  160.    table+=current.id
  161.    table+="'"
  162.    table+=">edit</button>"
  163.    table+="<button class='remove' x-id='"
  164.    table+=current.id
  165.    table+="'"
  166.    table+=">remove</button>"
  167.    table+="</td>"
  168.  
  169.    table+="</tr>\n"
  170.  }
  171.  table+="</table>"
  172.  
  173.  document.getElementById(idTable).innerHTML=table
  174.  document.querySelectorAll("#tblProduct  .remove").forEach(p=>p.onclick=clickRemove)
  175.  
  176.  document.querySelectorAll("#tblProduct  .update").forEach(p=>p.onclick=clickUpadte)
  177. }
  178. }
  179.  
  180. </body>
  181. </html>
  182.  
  183.  
  184.  
  185.  


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
contribución pruebas unitarias asynchronas
Java
sapito169 1 2,114 Último mensaje 18 Julio 2019, 19:01 pm
por sapito169
contribucion ejemplo de eventbus
Java
sapito169 6 4,201 Último mensaje 19 Enero 2021, 05:20 am
por sapito169
contribucion shell inversa en jrunscript
Java
sapito169 0 2,076 Último mensaje 3 Enero 2022, 07:10 am
por sapito169
contribucion macro maliciosa
Hacking
sapito169 0 1,634 Último mensaje 5 Enero 2022, 06:54 am
por sapito169
contribucion una red neuronal con algebra lineal
Java
sapito169 4 3,973 Último mensaje 12 Agosto 2022, 21:36 pm
por Tachikomaia
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines