URL: https://www.progressiverobot.com/vuejs-vue-virtual-scroller/

As a developer, you've probably had to implement a big list or table for an app, either by loading lots of data without pagination, or using an infinite scroll pagination and scrolling several times through it. These lists or tables can become slow, especially when they have lots of elements, CSS styles and/or transitions.

vue-virtual-scroller is a Vue.js plugin that applies the virtual scroll technique in order to performantly render a list to the DOM, whether it's a plain HTML ul > li list, a table, or a customized list.

Setup vue-virtual-scroller

vue-virtual-scroller illustration for: Setup vue-virtual-scroller

After you have a basic Vue.js project created, start by installing the plugin from npm:

				
					
$ npm install -D vue-virtual-scroller

				
			

Then, in your <^>main.js<^> file you must include its CSS file and initialize it:

				
					
[label main.js]

import "vue-virtual-scroller/dist/vue-virtual-scroller.css";

import Vue from "vue";

import VueVirtualScroller from "vue-virtual-scroller";



Vue.use(VueVirtualScroller);

// ...

				
			

That should be enough to start using it.

Creating a VirtualList

To start with a simple example, let's create a plain list that takes lots of data and uses <^>vue-virtual-scroller<^> to render it.

Let's use JSON-Generator to generate the JSON for 5000 entries and save it to a <^>data.json<^> file. You can use the following structure:

				
					
[

  '{{repeat(5000)}}',

  {

    _id: '{{objectId()}}',

    age: '{{integer(20, 40)}}',

    name: '{{firstName()}} {{surname()}}',

    company: '{{company().toUpperCase()}}'

  }

]

				
			

Let's create a <^>VirtualList.vue<^> file, where we import <^>data.json<^> and add it to an items component state property. Then, use the <virtual-scroller> component, passing those items:

				
					
[label VirtualList.vue]

&lt;template&gt;

  &lt;virtual-scroller :items="items" item-height="40" content-tag="ul"&gt;

    &lt;template slot-scope="props"&gt;

      &lt;li :key="props.itemKey"&gt;{{props.item.name}}&lt;/li&gt;

    &lt;/template&gt;

  &lt;/virtual-scroller&gt;

&lt;/template&gt;



&lt;script&gt;

import items from "./data.json";



export default {

  data: () =&gt; ({ items })

};

&lt;/script&gt;

				
			

We must set an item-height to the virtual-scroller component. Alternatively, since we're creating a list, we've set the content-tag="ul" which will wrap the contents into a <ul> tag.

vue-virtual-scroller allows to use scoped slots to render the contents for full flexibility with the rendering. By using slot-scope="props" we have access to the virtual-scroller exposed data, and use it for rendering.

props contains an itemKey property that we should bind using :key="props.itemKey" on the root of the content for performance reasons. Then we can access props.item containing the raw item from the JSON we've passed through the items property to virtual-scroller.

If you'd like to style the elements in the list, you can add a class attribute to the virtual-scroller component, and access its contents:

				
					
&lt;template&gt;

  &lt;virtual-scroller class="virtual-list" ...&gt;&lt;/virtual-scroller&gt;

&lt;/template&gt;



&lt;style&gt;

.virtual-list ul {

  list-style: none;

}

&lt;/style&gt;

				
			

Or, with scoped styles, use the /deep/ selector:

				
					
&lt;style scoped&gt;

.virtual-list /deep/ ul {

  list-style: none;

}

&lt;/style&gt;

				
			

Creating a VirtualTable

Similar to the <^>VirtualList<^> component, let's create a <^>VirtualTable.vue<^> file for a table:

				
					
[label VirtualTable.vue]

&lt;template&gt;

  &lt;virtual-scroller :items="items" item-height="40" content-tag="table"&gt;

    &lt;template slot-scope="props"&gt;

      &lt;tr :key="props.itemKey"&gt;

        &lt;td&gt;{{props.item.age}}&lt;/td&gt;

        &lt;td&gt;{{props.item.name}}&lt;/td&gt;

        &lt;td&gt;{{props.item.company}}&lt;/td&gt;

      &lt;/tr&gt;

    &lt;/template&gt;

  &lt;/virtual-scroller&gt;

&lt;/template&gt;



&lt;script&gt;

import items from "./data.json";



export default {

  data: () =&gt; ({ items })

};

&lt;/script&gt;

				
			

We encounter a problem with this example; we want to add a <thead> tag as the table header in order to show the columns names: <^>Age<^>, <^>Name<^> and <^>Company<^>.

Fortunately, virtual-scroller distributes its internal content using the following slots structure:

				
					
&lt;main&gt;

  &lt;slot name="before-container"&gt;&lt;/slot&gt;

  &lt;container&gt;

    &lt;slot name="before-content"&gt;&lt;/slot&gt;

    &lt;content&gt;

      &lt;!-- Your items here --&gt;

    &lt;/content&gt;

    &lt;slot name="after-content"&gt;&lt;/slot&gt;

  &lt;/container&gt;

  &lt;slot name="after-container"&gt;&lt;/slot&gt;

&lt;/main&gt;

				
			

Any of those slots can be used to place contents in there. container will be replaced by the tag value of the container-tag property, by default div, and the content by the content-tag value.

It's therefore easy to add a thead using the before-content slot:

				
					
&lt;template&gt;

  &lt;virtual-scroller

    :items="items"

    item-height="40"

    container-tag="table"

    content-tag="tbody"

    &gt;

      &lt;thead slot="before-content"&gt;

        &lt;tr&gt;

          &lt;td&gt;Age&lt;/td&gt;

          &lt;td&gt;Name&lt;/td&gt;

          &lt;td&gt;Company&lt;/td&gt;

        &lt;/tr&gt;

      &lt;/thead&gt;

      &lt;template slot-scope="props"&gt;

        &lt;tr :key="props.itemKey"&gt;

          &lt;td&gt;{{props.item.age}}&lt;/td&gt;

          &lt;td&gt;{{props.item.name}}&lt;/td&gt;

          &lt;td&gt;{{props.item.company}}&lt;/td&gt;

        &lt;/tr&gt;

      &lt;/template&gt;

  &lt;/virtual-scroller&gt;

&lt;/template&gt;

				
			

Notice that we've changed content-tag="table" for container-tag="table" and content-tag="tbody" in order to keep a usual table structure, since now we're using more elements.

I'm sure you can imagine how to add a tfoot element as well 😉.

Wrapping Up

We've used the vue-virtual-scroller plugin to create components for a <^>VirtualList<^> and a <^>VirtualTable<^>. If you try them out with the 5000 items we've generated, they should render and scroll quite smoothly. Check vue-virtual-scroller docs to see more options and customizations.

You can see the working code of the article in this Codesandox.

Stay cool 🦄