RIOT.JS - Boucles

Nous pouvons parcourir le tableau RIOT de primitives ou d'objets et créer / mettre à jour les éléments html lors de vos déplacements. En utilisant «chaque» construction, nous pouvons y parvenir.

  • Create array - Créez un tableau d'objets.

this.cities = [
   { city : "Shanghai" , country:"China" , done: true },
   { city : "Seoul"    , country:"South Korea" },
   { city : "Moscow"   , country:"Russia"      }
];
  • Add each attribute - Utilisez maintenant l'attribut "each".

<ul>
   <li each = { cities } ></li>
</ul>
  • Iterate array of objects - Itérez le tableau en utilisant les propriétés de l'objet.

<input type = "checkbox" checked = { done }> { city } - { country }

Exemple

Voici l'exemple complet.

custom7Tag.tag

<custom7Tag>
   <style>
      ul {
         list-style-type: none;
      }
   </style>
   <ul>
      <li each = { cities } >
         <input type = "checkbox" checked = { done }> { city } - { country }
      </li>
   </ul>
   <script>
      this.cities = [
         { city : "Shanghai" , country:"China" , done: true },
         { city : "Seoul"    , country:"South Korea" },
         { city : "Moscow"   , country:"Russia"      }
      ]; 
   </script>
</custom7Tag>

custom7.htm

<html>
   <head>
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/riot/3.13.2/riot+compiler.min.js"></script>
   </head>
   <body>
      <custom7Tag></custom6Tag>
      <script src = "custom7Tag.tag" type = "riot/tag"></script>
      <script>
         riot.mount("custom7Tag");
      </script>
   </body>
</html>

Cela produira le résultat suivant -