何かのリストがあって最初は上から3件だけを表示させ、「一覧を見る」みたいなボタンをクリックするとリストの全部を表示させる。みたいなSPAのサイトを作るときのメモ。
eachの後にifをいれる。
each
でループ処理する際に、ループする回数が取得できる。
ループ回数がi
に入ってくるので、それをif
で判定してあげる。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<app></app>
<script type="riot/tag">
<app>
<ul>
<li each={ t, i in this.arr } if={ i < 3 }>{ t.text }</li>
</ul>
this.arr = [
{text:"1つめ"},
{text:"2つめ"},
{text:"3つめ"},
{text:"4つめ"},
{text:"5つめ"},
]
</app>
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/riot/2.3.11/riot+compiler.min.js"></script>
<script>riot.mount('*')</script>
</body>
</html>