Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

PSEUDO SELECTBOX EN DHTML ...


Information sur la source

Catégorie :Trucs & Astuces Niveau : Débutant Date de création : 17/04/2005 Vu : 3 757

Note :
9 / 10 - par 1 personne
9,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (2)
Ajouter un commentaire et/ou une note

Description

Salut à tous,
Pour éviter de recevoir des commentaires comme quoi ce que j'ai fait est nul ou mal écrit, je vais expliquer un peu plus pourquoi je poste ça ...
Je suis en train d'essayer de coder en Javascript une machine de Turing entièrement personnalisable mais surtout avec une interface utilisateur. Du coup, une partie du developpement est porté sur cette interface, et ce que je poste maintenant en fait parti. En effet, je devais trouver un moyen pour que l'utilisateur puisse entrer des quintuplets (que l'on appelle Etat, d'où le nom Behavior) où les éléments sont séparés par des virgules, et qu'ensuite ceux-ci soient affichés dans une SelectBox ou l'utilisateur pouvait supprimer les entrées qu'il voulait.
Du coup, pour que ce soit plus joli, j'ai abandonné l'option des balises select/option, pour me débrouiller autrement ...
Et ça donne cette source. Il faut 5 caractères quelconques, séparés par des virgules ([', g, ", o, e], par exemple ...). Ensuite, ils s'ajoutent au tableau.
 

Source

  • <html>
  • <head>
  • <title>[Gestion des différents états]</title>
  • <style type="text/css">
  • img
  • {
  • padding: 0px;
  • margin: 0px;
  • }
  • #BehaviorList
  • {
  • padding: 0px;
  • margin: 0px;
  • background-color: #FFFFFF;
  • border-style: solid;
  • border-color: #000000;
  • border-width: 1px;
  • padding: 0px;
  • margin: 0px;
  • border-collapse: collapse;
  • font-size: 11px;
  • font-color: #000000;
  • font-family: Courier New;
  • vertical-align: middle;
  • text-align: center;
  • }
  • #BehaviorList tbody td
  • {
  • width: 15px;
  • height: 15px
  • background-color: #FFFFFF;
  • border-style: solid;
  • border-color: #AAAAAA;
  • border-width: 1px;
  • }
  • #BehaviorList thead th
  • {
  • font-weight: bold;
  • background-color: #DDDDDD;
  • border-style: solid;
  • border-width: 1px;
  • border-color: #777777;
  • height: 15px;
  • width: 15px;
  • }
  • </style>
  • <script type="text/javascript">
  • var aBehaviorList = new Array();
  • function CreateBehavior(cString)
  • {
  • aBehavior = new Array(aBehaviorList.length + 1, "", "", "", "", "");
  • i = 0;
  • j = 1;
  • while(i < cString.length)
  • {
  • if(cString.substr(i, 1) == ",")
  • {
  • j++;
  • }
  • else
  • {
  • aBehavior[j] = aBehavior[j] + cString.substr(i, 1)
  • }
  • i++;
  • }
  • return aBehavior;
  • }
  • function AddBehavior(cString)
  • {
  • oBehaviorList = document.getElementById("BehaviorList");
  • aBehavior = CreateBehavior(cString);
  • aBehaviorList.unshift(aBehavior);
  • WriteBehavior(aBehavior);
  • }
  • function WriteBehavior(aBehavior)
  • {
  • oBehaviorList = document.getElementById("BehaviorList");
  • oImg = document.createElement("img");
  • oImg.src = "cross.png";
  • oImg.style.cursor = "hand";
  • oImg.alt = "Supprimer ..."
  • oBehaviorList.tBodies[0].insertRow(0);
  • oBehaviorList.tBodies[0].rows[0].insertCell(0);
  • oBehaviorList.tBodies[0].rows[0].cells[0].appendChild(oImg);
  • oBehaviorList.tBodies[0].rows[0].cells[0].onclick = function(){DelBehavior(aBehavior[0]);};
  • i = 0;
  • while(i < Math.min(aBehavior.length, oBehaviorList.tHead.rows[0].cells.length - 1))
  • {
  • oBehaviorList.tBodies[0].rows[0].insertCell(i + 1);
  • oTxt = document.createTextNode(aBehavior[i]);
  • oBehaviorList.tBodies[0].rows[0].cells[i + 1].appendChild(oTxt);
  • i++;
  • }
  • }
  • function DelBehavior(Index)
  • {
  • oBehaviorList = document.getElementById("BehaviorList");
  • aOldList = aBehaviorList;
  • aBehaviorList = new Array();
  • i = 0;
  • while(i < aOldList.length)
  • {
  • if(aOldList[i][0] == Index)
  • {
  • oBehaviorList.tBodies[0].deleteRow(i);
  • }
  • else
  • {
  • aBehaviorList.push(aOldList[i]);
  • }
  • i++;
  • }
  • }
  • </script>
  • </head>
  • <body>
  • <form action="#">
  • <input id="BehaviorInput" type="input" value="" /><input type="button" value="AddBehavior();" onclick="AddBehavior(this.previousSibling.value); this.previousSibling.value = ''"><br />
  • <table id="BehaviorList">
  • <thead>
  • <tr>
  • <th>a</th>
  • <th>b</th>
  • <th>c</th>
  • <th>d</th>
  • <th>e</th>
  • <th>f</th>
  • <th>g</th>
  • </tr>
  • </thead>
  • <tbody>
  • </tbody>
  • </table>
  • </form>
  • </body>
  • </html>
<html>
   <head>
      <title>[Gestion des différents états]</title>
      <style type="text/css">
         img
         {
            padding: 0px;
            margin: 0px;
         }
         #BehaviorList
         {
            padding: 0px;
            margin: 0px;
            background-color: #FFFFFF;
            border-style: solid;
            border-color: #000000;
            border-width: 1px;
            padding: 0px;
            margin: 0px;
            border-collapse: collapse;
            font-size: 11px;
            font-color: #000000;
            font-family: Courier New;
            vertical-align: middle;
            text-align: center;
         }
         #BehaviorList tbody td
         {
            width: 15px;
            height: 15px
            background-color: #FFFFFF;
            border-style: solid;
            border-color: #AAAAAA;
            border-width: 1px;
         }
         #BehaviorList thead th
         {
            font-weight: bold;
            background-color: #DDDDDD;
            border-style: solid;
            border-width: 1px;
            border-color: #777777;
            height: 15px;
            width: 15px;
         }
      </style>
      <script type="text/javascript">
         var aBehaviorList = new Array();
         function CreateBehavior(cString)
         {
            aBehavior = new Array(aBehaviorList.length + 1, "", "", "", "", "");
            i = 0;
            j = 1;
            while(i < cString.length)
            {
               if(cString.substr(i, 1) == ",")
               {
                  j++;
               }
               else
               {
                  aBehavior[j] = aBehavior[j] + cString.substr(i, 1)
               }
               i++;
            }
            return aBehavior;
         }
         function AddBehavior(cString)
         {
            oBehaviorList = document.getElementById("BehaviorList");
            aBehavior = CreateBehavior(cString);
            aBehaviorList.unshift(aBehavior);
            WriteBehavior(aBehavior);
         }
         function WriteBehavior(aBehavior)
         {
            oBehaviorList = document.getElementById("BehaviorList");
            oImg = document.createElement("img");
            oImg.src = "cross.png";
            oImg.style.cursor = "hand";
            oImg.alt = "Supprimer ..."
            oBehaviorList.tBodies[0].insertRow(0);
            oBehaviorList.tBodies[0].rows[0].insertCell(0);
            oBehaviorList.tBodies[0].rows[0].cells[0].appendChild(oImg);
            oBehaviorList.tBodies[0].rows[0].cells[0].onclick = function(){DelBehavior(aBehavior[0]);};
            i = 0;
            while(i < Math.min(aBehavior.length, oBehaviorList.tHead.rows[0].cells.length - 1))
            {
               oBehaviorList.tBodies[0].rows[0].insertCell(i + 1);
               oTxt = document.createTextNode(aBehavior[i]);
               oBehaviorList.tBodies[0].rows[0].cells[i + 1].appendChild(oTxt);
               i++;
            }
         }
         function DelBehavior(Index)
         {
            oBehaviorList = document.getElementById("BehaviorList");
            aOldList = aBehaviorList;
            aBehaviorList = new Array();
            i = 0;
            while(i < aOldList.length)
            {
               if(aOldList[i][0] == Index)
               {
                  oBehaviorList.tBodies[0].deleteRow(i);
               }
               else
               {
                  aBehaviorList.push(aOldList[i]);
               }
               i++;
            }
         }
      </script>
   </head>
   <body>
      <form action="#">
         <input id="BehaviorInput" type="input" value="" /><input type="button" value="AddBehavior();" onclick="AddBehavior(this.previousSibling.value); this.previousSibling.value = ''"><br />
         <table id="BehaviorList">
         <thead>
            <tr>
               <th>a</th>
               <th>b</th>
               <th>c</th>
               <th>d</th>
               <th>e</th>
               <th>f</th>
               <th>g</th>
            </tr>
         </thead>
         <tbody>
         </tbody>
         </table>
      </form>
   </body>
</html>

Conclusion

Si je poste ça, c'est pour montrer les possibilité du DHTML et du modèle DOM du Javascript. Déjà, tout est compatible Firefox/Internet Explorer, et ça permet de pas mal apprendre au niveau de la gestion des nodes et des tableaux. Alors j'espère que ça peut aider à comprendre le Javascript pour les débutants, parce que si ça, c'est compris, alors tout est compris !
++
L.S.
 

Commentaires et avis

signaler à un administrateur
Commentaire de ThunderPsycho le 18/04/2005 08:24:37

Très intéressant... 9/10
Ca manque un peu de commentaires mais ton code est bien aéré...
C'est quand même bien plus pratique qu'une armée de input ou de textbox pour entrer une grosse quantité de données. :o)

signaler à un administrateur
Commentaire de Arto_8000 le 18/04/2005 23:32:38

Bien réussi ,mais est-ce que l'on peut avoir l'image pour supprimer ce sera sûrement plus beau que l'image avec un X de IE.

Ajouter un commentaire



Nos sponsors

Sondage...

CalendriCode

Juillet 2009
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode

Comparez les prix Nouvelle version

Photothèque Nouveau !



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,250 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.