Ver Mensaje Individual
  #30 (permalink)  
Antiguo 03/04/2010, 18:09
ImNoob007
 
Fecha de Ingreso: octubre-2007
Mensajes: 8
Antigüedad: 16 años, 6 meses
Puntos: 0
Respuesta: Formulario Dinámico en javascript PARTE 2 FIN.

Código Javascript:
Ver original
  1. //CLASE FORMULARIO
  2. function CForm(arForm, sIdPadre)
  3. {
  4.     var ePadre=document.body;
  5.     this.eForm=document.createElement('form');
  6.  
  7.     with(this.eForm)
  8.     {
  9.         id=arForm['id'];
  10.         setAttribute("name", arForm['Nombre']);
  11.         setAttribute("method", arForm['Metodo']);
  12.         setAttribute("action", arForm['Accion']);
  13.  
  14.         style.height=arForm['Alto'];
  15.         style.width=arForm['Ancho'];
  16.         style.backgroundColor=arForm['ColorFondo'];
  17.         style.position=arForm['Posicion'];
  18.         style.zIndex=arForm['z'];
  19.         style.top=arForm['y'];
  20.         style.left=arForm['x'];
  21.         style.padding=arForm['Padding'];
  22.         /*style.border="1px solid green";*/
  23.     }
  24.  
  25.     if(sIdPadre==null)
  26.     {
  27.         ePadre.appendChild(this.eForm);
  28.     }
  29.     else
  30.     {
  31.         ePadre=document.getElementById(sIdPadre);
  32.         ePadre.appendChild(this.eForm);
  33.     }
  34.  
  35.     //GETS
  36.     this.getID=function()
  37.     {
  38.         return this.eForm.id;
  39.     }
  40.     this.getWidth=function()
  41.     {
  42.         return this.eForm.offsetWidth;
  43.     }
  44.  
  45.     this.getHeight=function()
  46.     {
  47.         return this.eForm.offsetHeight;
  48.     }
  49.  
  50.     this.getTop=function()
  51.     {
  52.         return this.eForm.offsetTop;
  53.     }
  54.  
  55.     this.getLeft=function()
  56.     {
  57.         return this.eForm.offsetLeft;
  58.     }
  59.  
  60.     //SETS
  61.     this.setWidth=function(sValor)
  62.     {
  63.         this.eForm.style.width=sValor;
  64.     }
  65.  
  66.     this.setHeight=function(sValor)
  67.     {
  68.         this.eForm.style.height=sValor;
  69.     }
  70.  
  71.     this.setTop=function(sValor)
  72.     {
  73.         this.eForm.style.top=sValor;
  74.     }
  75.  
  76.     this.setLeft=function(sValor)
  77.     {
  78.         this.eForm.style.left=sValor;
  79.     }
  80. }
  81.  
  82. //CLASE INPUT boton, texto, hidden submit..
  83. function CInput(arInput, eParent)
  84. {
  85.     var ePadre=document.body;
  86.     this.eInput=document.createElement('input');
  87.  
  88.     with(this.eInput)
  89.     {
  90.         setAttribute("type", arInput['Tipo']);
  91.         id=arInput['id'];
  92.         setAttribute("name", arInput['Nombre']);
  93.         setAttribute("value", arInput['Valor']);
  94.         //Estilo
  95.         style.width=arInput['Ancho'];
  96.     }
  97.  
  98.     if(arInput['Tipo']=="button")
  99.     {
  100.         //Registramos los eventos
  101.         //http://www.w3.org/TR/DOM-Level-2-Events/events.html
  102.         if(document.all) //ES IE
  103.         {
  104.             this.eInput.attachEvent("onclick", pCancelar);
  105.         }
  106.         else //OTROS NAVEGADORES
  107.         {
  108.             this.eInput.addEventListener("click", pCancelar, true);
  109.         }
  110.     }
  111.  
  112.  
  113.     function pCancelar()
  114.     {
  115.         var eDivAux = document.getElementById(eGDivForm.getID());
  116.         document.body.removeChild(eDivAux);
  117.  
  118.         eDivAux = document.getElementById(eGDivFondo.getID());
  119.         document.body.removeChild(eDivAux);
  120.  
  121.         //Destruyo mis objetos
  122.         oGVentana=null;
  123.         eGDivFondo=null;
  124.         eGDivForm=null;
  125.         eGFormulario=null;
  126.         eGTabla=null;
  127.         eGHidFila=null;
  128.         eGTxtConcep=null;
  129.         eGTxtCant=null;
  130.         eGbotCancelar=null;
  131.         eGSubAceptar=null;
  132.  
  133.         //Actualizo mi variable
  134.         bGFrmCreado=false;
  135.     }
  136.  
  137.     if(eParent==null)
  138.     {
  139.         ePadre.appendChild(this.eInput);
  140.     }
  141.     else
  142.     {
  143.         eParent.appendChild(this.eInput);
  144.     }
  145.  
  146.     //GETS
  147.     this.getID=function()
  148.     {
  149.         return this.eInput.id;
  150.     }
  151.  
  152.     this.getValor=function()
  153.     {
  154.         return this.eInput.value;
  155.     }
  156.  
  157.     this.getWidth=function()
  158.     {
  159.         return this.eInput.offsetWidth;
  160.     }
  161.  
  162.     //SETS
  163.     this.setWidth=function(sValor)
  164.     {
  165.         this.eInput.style.width=sValor;
  166.     }
  167. }
  168.  
  169. //CLASE TABLA
  170. function CTabla(arTabla, sIdPadre)
  171. {
  172.     var arTextos=new Array();
  173.     arTextos[0]="F";
  174.     arTextos[1]="CONCEPTO";
  175.     arTextos[2]="CANTIDAD";
  176.     arTextos[3]="";
  177.     arTextos[4]="";
  178.  
  179.     var ePadre=document.body;
  180.     this.eTable=document.createElement('table');
  181.  
  182.     with(this.eTable)
  183.     {
  184.         id=arTabla['id'];
  185.         setAttribute("name", arTabla['Nombre']);
  186.  
  187.         style.height=arTabla['Alto'];
  188.         style.width=arTabla['Ancho'];
  189.         style.backgroundColor=arTabla['ColorFondo'];
  190.         style.position=arTabla['Posicion'];
  191.         style.zIndex=arTabla['z'];
  192.         style.top=arTabla['y'];
  193.         style.left=arTabla['x'];
  194.         style.padding=arTabla['Padding'];
  195.         style.border=arTabla['Borde'];
  196.     }
  197.     //Genero la tabla
  198.     for(var i=0; i<arTabla['Filas']; i++)
  199.     {
  200.         var auxTR=document.createElement("tr");
  201.         for(var j=0; j<arTabla['Columnas']; j++)
  202.         {
  203.             var auxTD=document.createElement("td");
  204.             var auxText;
  205.             if(i==0) //Es la cabecera
  206.             {
  207.                 auxText=document.createTextNode(arTextos[j]);
  208.                 auxTD.appendChild(auxText);
  209.             }
  210.             else //El resto de filas
  211.             {
  212.                 switch (j)
  213.                 {
  214.                     case 0:
  215.                        eGHidFila = new CInput(arHIDDEN, auxTD);
  216.                        auxText=document.createTextNode(eGHidFila.getValor());
  217.                        auxTD.appendChild(auxText);
  218.                         break
  219.                     case 1:
  220.                        eGTxtConcep = new CInput(arTXTCONCEP, auxTD);
  221.                         break
  222.                     case 2:
  223.                        eGTxtCant = new CInput(arTXTCANT, auxTD);
  224.                         break
  225.                     case 3:
  226.                        eGSubAceptar = new CInput(arSUBACEPTAR, auxTD);
  227.                         break
  228.                     case 4:
  229.                        eGbotCancelar = new CInput(arBOTCANCELAR, auxTD);
  230.                        break
  231.                     default:
  232.                         document.write("Esta columna no existe!!");
  233.                 }
  234.             }
  235.             auxTR.appendChild(auxTD);
  236.         }
  237.         this.eTable.appendChild(auxTR);
  238.     }
  239.  
  240.     if(sIdPadre==null)
  241.     {
  242.         ePadre.appendChild(this.eTable);
  243.     }
  244.     else
  245.     {
  246.         ePadre=document.getElementById(sIdPadre);
  247.         ePadre.appendChild(this.eTable);
  248.     }
  249.  
  250.     //GETS
  251.     this.getID=function()
  252.     {
  253.         return this.eTable.id;
  254.     }
  255.     this.getWidth=function()
  256.     {
  257.         return this.eTable.offsetWidth;
  258.     }
  259.  
  260.     this.getHeight=function()
  261.     {
  262.         return this.eTable.offsetHeight;
  263.     }
  264.  
  265.     this.getTop=function()
  266.     {
  267.         return this.eTable.offsetTop;
  268.     }
  269.  
  270.     this.getLeft=function()
  271.     {
  272.         return this.eTable.offsetLeft;
  273.     }
  274.  
  275.     //SETS
  276.     this.setWidth=function(sValor)
  277.     {
  278.         this.eTable.style.width=sValor;
  279.     }
  280.  
  281.     this.setHeight=function(sValor)
  282.     {
  283.         this.eTable.style.height=sValor;
  284.     }
  285.  
  286.     this.setTop=function(sValor)
  287.     {
  288.         this.eTable.style.top=sValor;
  289.     }
  290.  
  291.     this.setLeft=function(sValor)
  292.     {
  293.         this.eTable.style.left=sValor;
  294.     }
  295. }
  296.  
  297. //Procedimiento que vincularas a un evento Click y que dara como resultado
  298. //la generacion del formulario
  299. function pClick()
  300. {
  301.     //Objeto ventana obtiene el tamaño de la pantalla
  302.     //necesario para el centrado y el recubrimiento de los controles
  303.     oGVentana = new CVentana();
  304.  
  305.     //El elemento Div con opacidad
  306.     eGDivFondo= new CDiv(arDIVFONDO,null);
  307.     eGDivFondo.setWidth(oGVentana.getAncho());
  308.     eGDivFondo.setHeight(oGVentana.getAlto());
  309.  
  310.     //El elemento Div contenedor del formulario
  311.     eGDivForm = new CDiv(arDIVFORM, null);
  312.    
  313.     //Centro el contenedor en pantalla
  314.     var aux=(eGDivFondo.getWidth()-eGDivForm.getWidth())/2 + "px";
  315.     eGDivForm.setLeft(aux);
  316.     aux=(eGDivFondo.getHeight()-eGDivForm.getHeight())/2 + "px";
  317.     eGDivForm.setTop(aux);
  318.  
  319.     //Creo el Formulario y lo adjunto a su padre el eGDivForm
  320.     eGFormulario=new CForm(arFORM, eGDivForm.getID());
  321.  
  322.     //Creo la Tabla (para tabular mis controles) y lo adjunto a su padre eGFormulario
  323.     eGTabla=new CTabla(arTABLA, eGFormulario.getID());
  324.  
  325.     //Indico que se ha generado el formulario, esto me servira para
  326.     //el evento que se ejecuta cuando se redimensiona la pantalla
  327.     bGFrmCreado=true;
  328. }
  329.  
  330. window.onresize = function ()
  331. {
  332.     if(bGFrmCreado)
  333.     {
  334.         //Creo nuevamente oVentana para actualizar su tamaño
  335.         oGVentana = new CVentana();
  336.         //Redimensiono el Div con opacidad
  337.         eGDivFondo.setWidth(oGVentana.getAncho());
  338.         eGDivFondo.setHeight(oGVentana.getAlto());
  339.  
  340.         //centro divForm
  341.         var aux=(eGDivFondo.getWidth()-eGDivForm.getWidth())/2 + "px";
  342.         eGDivForm.setLeft(aux);
  343.         aux=(eGDivFondo.getHeight()-eGDivForm.getHeight())/2 + "px";
  344.         eGDivForm.setTop(aux);
  345.     }
  346. }

TE CREAS UN ARCHIVO.JS y lo vinculas en tu html. Te viene a pelo esta clase. Pq te genera un formulario dinamico con 3 campos CLAVE - DESCRIPCION - PRECIO y como esta parametrizado lo puedes modificar a tu gusto.

En tu evento onload del body puedes llamar al procedimiento pClick(). Lo suyo seria ejecutarlo con un evento click de un boton.

Última edición por ImNoob007; 03/04/2010 a las 18:12 Razón: Faltaba Informacion