Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Vue.js で多重に v-repeat を使う

More than 5 years have passed since last update.

例えばこんな感じのオブジェクトがあったとする

var list = {
  hoge: {
    a: "alpha",
    b: "bravo",
    c: "charlie"
  },
  fuga: {
    d: "delta",
    e: "echo"
  },
  piyo: {
    f: "foxtrot",
    g: "golf",
    h: "hotel"
  }
}

HTML はこんな感じにする

index.html
<ul id="multiple-list">
  <li v-repeat="sub: list">
    {{sub.$key}}
    <ul>
      <li v-repeat="sub">{{$key}} = {{$value}}</li>
    </ul>
  </li>
</ul>

ここが重要

index.html
  <li v-repeat="sub: list">

ViewModel を作る

main.js
var vm = new Vue({
  el: "#multiple-list",
  data: {
    list: {
      hoge: {
        a: "alpha",
        b: "bravo",
        c: "charlie"
      },
      fuga: {
        d: "delta",
        e: "echo"
      },
      piyo: {
        f: "foxtrot",
        g: "golf",
        h: "hotel"
      }
    }
  }
});

実行するとこうなる
2014-09-13-132938_217x248_scrot.png
余計なもの( \$key = hoge) がついてるので、 $key を取り除くフィルターを作る

main.js
var utils = Vue.require("utils");
Vue.filter("multiple", function(a){
  return utils.objectToArray(a).filter(function(v){
    return v.$key !== "$key"
  });
});

HTMLにフィルターを適応させる

index.html
<ul id="multiple-list">
  <li v-repeat="sub: list">
    {{sub.$key}}
    <ul>
      <li v-repeat="sub | multiple">{{$key}} = {{$value}}</li>
    </ul>
  </li>
</ul>

完成、やったぜ。

2014-09-13-133303_179x201_scrot.png

aoki
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away