RIOT.JS - Loop

Kita dapat melakukan iterasi melalui array RIOT primitif atau Objek dan membuat / memperbarui elemen html saat dalam perjalanan. Menggunakan "setiap" konstruksi kita bisa mencapainya.

  • Create array - Buat larik objek.

this.cities = [
   { city : "Shanghai" , country:"China" , done: true },
   { city : "Seoul"    , country:"South Korea" },
   { city : "Moscow"   , country:"Russia"      }
];
  • Add each attribute - Sekarang gunakan atribut "each".

<ul>
   <li each = { cities } ></li>
</ul>
  • Iterate array of objects - Iterasi array menggunakan properti objek.

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

Contoh

Berikut contoh lengkapnya.

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>

tersuai7.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>

Ini akan menghasilkan hasil sebagai berikut -