Jésus marcha sur l'eau. Arjuna fit du funambulisme sans fil.

Jésus marcha sur l'eau. Arjuna fit du funambulisme sans fil. - HTML/CSS - Programmation

Marsh Posté le 09-05-2005 à 15:43:25    

PS d'avance : Vi, c'est un 3615 mylife, mais je pense que les éléments technique ci-dessous (notamment la technique des objets en JS) peut être très bénéfique pour tous, donc le code me paraît relativement intéressant à lire, même s'il n'est pas buggé.
 
Alors...
 
Imaginez.
 
Une page ASP instancie un objet (DLL) codé en VB. Ce dernier appelle une PS dans SQL Server, puis met en forme les données dans une collection arborescante d'objets.
L'ASP parse tout ce joyeux bordel, pour écrire un JavaScript composé d'objets dans tous les sens, eux aussi arborescants.
Ce JS modifie à la volée un tableau généré à la volée par l'ASP (nombre de lignes variables).
 
C'est bô, ça marche :love:, même sur NS7 et un veix tromblon de NT4 avec un IE d'avant la guerre du feu.
 
Par contre, ça fait peur, je plainds celui qui va repasser derrière :lol:
 
Pourtant, le code est assez propre, le résultat dans le HTML est nikel.
 
Extrait du code dans l'ASP :

Code :
  1. Response.Write "<script language=""JavaScript"" type=""text/javascript"">" & vbCrLf
  2.  Response.Write " var envelops = new Array();" & vbCrLf
  3.  Response.Write " function data()" & vbCrLf
  4.  Response.Write " {" & vbCrLf
  5.  Response.Write "  this.envelops = new Array();" & vbCrLf
  6.  Response.Write "  this.groupinvoices = new Array();" & vbCrLf
  7.  Response.Write "  this.addEnvelop = function (envname, numero, amount)" & vbCrLf
  8.  Response.Write "  {" & vbCrLf
  9.  Response.Write "   var index = new Number();" & vbCrLf
  10.  Response.Write "   index = -1;" & vbCrLf
  11.  Response.Write "   for (i = 0; i < this.envelops.length; i++)" & vbCrLf
  12.  Response.Write "   {" & vbCrLf
  13.  Response.Write "    if (this.envelops[i].name == envname)" & vbCrLf
  14.  Response.Write "    {" & vbCrLf
  15.  Response.Write "     index = i;" & vbCrLf
  16.  Response.Write "     break;" & vbCrLf
  17.  Response.Write "    }" & vbCrLf
  18.  Response.Write "   }" & vbCrLf
  19.  Response.Write "   if (index == -1)" & vbCrLf
  20.  Response.Write "   {" & vbCrLf
  21.  Response.Write "    index = this.envelops.length;" & vbCrLf
  22.  Response.Write "    this.envelops[index] = new envelop(envname);" & vbCrLf
  23.  Response.Write "   }" & vbCrLf
  24.  Response.Write "   this.envelops[index].addInvoice(numero, amount);" & vbCrLf
  25.  Response.Write "   this.addGroupInvoice(numero, amount);" & vbCrLf
  26.  Response.Write "  }" & vbCrLf
  27.  Response.Write "  this.addGroupInvoice = function (numero, total)" & vbCrLf
  28.  Response.Write "  {" & vbCrLf
  29.  Response.Write "   for (i = 0; i < this.groupinvoices.length; i++)" & vbCrLf
  30.  Response.Write "   {" & vbCrLf
  31.  Response.Write "    if (this.groupinvoices[i].numero == numero)" & vbCrLf
  32.  Response.Write "    {" & vbCrLf
  33.  Response.Write "     this.groupinvoices[i].total += total;" & vbCrLf
  34.  Response.Write "     return;" & vbCrLf
  35.  Response.Write "    }" & vbCrLf
  36.  Response.Write "   }" & vbCrLf
  37.  Response.Write "   this.groupinvoices[this.groupinvoices.length] = new groupinvoice(numero, total);" & vbCrLf
  38.  Response.Write "  }" & vbCrLf
  39.  Response.Write "  this.getGroupInvoiceByNumero = function (numero)" & vbCrLf
  40.  Response.Write "  {" & vbCrLf
  41.  Response.Write "   for (i = 0; i < this.groupinvoices.length; i++)" & vbCrLf
  42.  Response.Write "   {" & vbCrLf
  43.  Response.Write "    if (this.groupinvoices[i].numero == numero)" & vbCrLf
  44.  Response.Write "    {" & vbCrLf
  45.  Response.Write "     return this.groupinvoices[i];" & vbCrLf
  46.  Response.Write "    }" & vbCrLf
  47.  Response.Write "   }" & vbCrLf
  48.  Response.Write "   return new groupinvoice(numero, 0);" & vbCrLf
  49.  Response.Write "  }" & vbCrLf
  50.  Response.Write " }" &vbCrLf
  51.  Response.Write " function envelop(name)" & vbCrLf
  52.  Response.Write " {" & vbCrLf
  53.  Response.Write "  this.name = name;" & vbCrLf
  54.  Response.Write "  this.invoices = new Array();" & vbCrLf
  55.  Response.Write "  this.addInvoice = function (numero, amount) {this.invoices[this.invoices.length] = new invoice(numero, amount);}" & vbCrLf
  56.  Response.Write "  this.getInvoice = function (numero) {for (i = 0; i < this.invoices.length; i++) {if (this.invoices[i].numero == numero) return this.invoices[i];} return new invoice(numero, 0);}" & vbCrLf
  57.  Response.Write " }" & vbCrLf
  58.  Response.Write " function invoice(numero, amount)" & vbCrLf
  59.  Response.Write " {" & vbCrLf
  60.  Response.Write "  this.numero = numero;" & vbCrLf
  61.  Response.Write "  this.amount = amount;" & vbCrLf
  62.  Response.Write " }" & vbCrLf
  63.  Response.Write " function groupinvoice(numero, total)" & vbCrLf
  64.  Response.Write " {" & vbCrLf
  65.  Response.Write "  this.numero = numero;" & vbCrLf
  66.  Response.Write "  this.total = total;" & vbCrLf
  67.  Response.Write " }" & vbCrLf
  68.  Response.Write " var index = new Number();" & vbCrLf
  69.  Response.Write " var objData = new data();" & vbCrLf
  70.  Response.Write "</script>" & vbCrLf
  71.  Dim prevNumfac
  72.  prevNumfac = ""
  73.  For Each facture in procurement.workingFactures
  74.   If prevNumfac <> facture.numero Then
  75.    ' On n'avance que si on a changé de facture
  76.    i = i + 1
  77.   End If
  78.   If i > (pageNb - 1) * CInt(session("sizeListeRech" )) And i <= pageNb * CInt(session("sizeListeRech" )) Then
  79.    Response.Write "<script language=""JavaScript"" type=""text/javascript"">" & vbCrLf
  80.    Response.Write " objData.addEnvelop('" & facture.envname & "', '" & facture.numero & "', " & facture.prixtotalht & " );" & vbCrLf
  81.    Response.Write "</script>" & vbCrLf
  82.    If prevNumfac <> facture.numero Then
  83. %>
  84. [... block de HTML ...]
  85. <%
  86.     isEven = not isEven
  87.    End If
  88.   Else
  89.    If i > pageNb * CInt(session("sizeListeRech" )) Then
  90.     Exit For
  91.    End If
  92.   End If
  93.   prevNumfac = facture.numero
  94.  Next
  95.  Response.Write "<script language=""JavaScript"" type=""text/javascript"">" & vbCrLf
  96.  Response.Write " displayEnveloppes(0);" & vbCrLf
  97.  Response.Write "</script>" & vbCrLf


 
Et le résultat dnas le HTML :
 

Code :
  1. <script language="JavaScript" type="text/javascript">
  2. var envelops = new Array();
  3. function data()
  4. {
  5.  this.envelops = new Array();
  6.  this.groupinvoices = new Array();
  7.  this.addEnvelop = function (envname, numero, amount)
  8.  {
  9.   var index = new Number();
  10.   index = -1;
  11.   for (i = 0; i < this.envelops.length; i++)
  12.   {
  13.    if (this.envelops[i].name == envname)
  14.    {
  15.     index = i;
  16.     break;
  17.    }
  18.   }
  19.   if (index == -1)
  20.   {
  21.    index = this.envelops.length;
  22.    this.envelops[index] = new envelop(envname);
  23.   }
  24.   this.envelops[index].addInvoice(numero, amount);
  25.   this.addGroupInvoice(numero, amount);
  26.  }
  27.  this.addGroupInvoice = function (numero, total)
  28.  {
  29.   for (i = 0; i < this.groupinvoices.length; i++)
  30.   {
  31.    if (this.groupinvoices[i].numero == numero)
  32.    {
  33.     this.groupinvoices[i].total += total;
  34.     return;
  35.    }
  36.   }
  37.   this.groupinvoices[this.groupinvoices.length] = new groupinvoice(numero, total);
  38.  }
  39.  this.getGroupInvoiceByNumero = function (numero)
  40.  {
  41.   for (i = 0; i < this.groupinvoices.length; i++)
  42.   {
  43.    if (this.groupinvoices[i].numero == numero)
  44.    {
  45.     return this.groupinvoices[i];
  46.    }
  47.   }
  48.   return new groupinvoice(numero, 0);
  49.  }
  50. }
  51. function envelop(name)
  52. {
  53.  this.name = name;
  54.  this.invoices = new Array();
  55.  this.addInvoice = function (numero, amount) {this.invoices[this.invoices.length] = new invoice(numero, amount);}
  56.  this.getInvoice = function (numero) {for (i = 0; i < this.invoices.length; i++) {if (this.invoices[i].numero == numero) return this.invoices[i];} return new invoice(numero, 0);}
  57. }
  58. function invoice(numero, amount)
  59. {
  60.  this.numero = numero;
  61.  this.amount = amount;
  62. }
  63. function groupinvoice(numero, total)
  64. {
  65.  this.numero = numero;
  66.  this.total = total;
  67. }
  68. var index = new Number();
  69. var objData = new data();
  70. </script>
  71. <script language="JavaScript" type="text/javascript">
  72. objData.addEnvelop('SFT01', 'dsc12100', 2495);
  73. </script>
  74. <script language="JavaScript" type="text/javascript">
  75. objData.addEnvelop('SFT01', 'dsc12201', 995);
  76. </script>
  77. ...


 
Mais le plus joli, c'est le truc qui affiche les données à la volée dans le tableau HTLM :sol:
 

Code :
  1. <script language="JavaScript" type="text/javascript">
  2. function displayEnveloppes(fromIndex)
  3. {
  4.  var i = new Number();
  5.  var j = new Number();
  6.  tableau = document.getElementById("arrData" );
  7.  for (j = 0; j < Math.min(4, objData.envelops.length); j++)
  8.  {
  9.   tableau.rows[4].cells[(j * 2) + 8].innerHTML = objData.envelops[j + fromIndex].name;
  10.  }
  11.  if (fromIndex > 0 && objData.envelops.length > 4)
  12.  {
  13.   tableau.rows[4].cells[8].innerHTML = "<a href=\"javascript:displayEnveloppes(" + Math.max(0, fromIndex - 4) + " );\">&lt;&lt;</a>&nbsp;" + tableau.rows[4].cells[8].innerHTML;
  14.  }
  15.  if (fromIndex < objData.envelops.length - 4)
  16.  {
  17.   tableau.rows[4].cells[14].innerHTML += "&nbsp;<a href=\"javascript:displayEnveloppes(" + Math.min(objData.envelops.length - 4, fromIndex + 4) + " );\">&gt;&gt;</a>";
  18.  }
  19.  for (j = 0; j < objData.groupinvoices.length; j++)
  20.  {
  21.   tableau.rows[6 + j].cells[7].innerHTML = objData.getGroupInvoiceByNumero(tableau.rows[6 + j].cells[5].innerHTML).total;
  22.   for (i = 0; i < Math.min(4, objData.envelops.length); i++)
  23.   {
  24.    tableau.rows[6 + j].cells[(i * 2) + 9].innerHTML = objData.envelops[i + fromIndex].getInvoice(tableau.rows[6 + j].cells[5].innerHTML).amount;
  25.   }
  26.  }
  27. }
  28. </script>


Message édité par Arjuna le 09-05-2005 à 16:29:39
Reply

Marsh Posté le 09-05-2005 à 15:43:25   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed