Help us understand the problem. What is going on with this article?

Vue.jsで10万件のリストを表示する

More than 3 years have passed since last update.

はじめに

Vue.jsは軽量ですが、数万件のリストを並べるとそりゃ普通につらい。
だから目に見えるとこ以外はdom描いてもらうのやめるってコードです。

デモ

デモ(jsfiddle)

コード

html
<div id="app">
 <div style="overflow:auto;height:170px;"
      @scroll="getScrollParam">
  <ul :style="listStyle">
    <li style="height:17px" 
        v-for="num in displayList">{{num}}</li>
  </ul>
 </div>
</div>
javascript
new Vue({
    el: '#app',
    data: function(){
      return {
        list:list,
        scroll:0,//スクロール量
        scrollMax: (list.length - displayRowNum) * listItemHeight //最大のスクロール量
      }
    },
    computed:{
      displayList:function(){
        //ユーザーの目に映るところだけリストを描画する
        var startIndex = parseInt(this.scroll/listItemHeight,10);
        return this.list.slice(startIndex,startIndex+displayRowNum);
      },
      listStyle:function(){
        //domを並べる代わりにpaddingでダミーの高さを出す
        return {
         'padding-top':this.scroll + 'px',
         'padding-bottom':(this.scrollMax - this.scroll) + 'px',
         'margin':0
        }
      }
    },
    methods:{
        getScrollParam:function(e){
          this.scroll = e.target.scrollTop;
        }
    }
})

おわりに

わかりやすさのために見えてる長さちょうどのリストを表示しましたが、
2,3倍の長さにしたほうがスクロール時にちらつかなくて良いです。
あと実際使う時はクロスブラウザのためにスクロール量や高さの取得に調整が必要です。

huigo
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした