11
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

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

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:2
  <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

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?