How to create a sticky header in BootstrapVue table in VueJS?

Hi Friends 👋,

Welcome To SortoutCode! ❤️

To create the add the sticky header b-table in BootstrapVue.We are passing the sticky-header as a prop to create the sticky header to b-table in BootstrapVue.

Today I am going to show you How do you create a sticky header on b-table in bootstrap-vue?

Table of contains

  • Install the Bootstrap and BootstrapVue
  • Create FirstComponent.vue and import it into App.js
  • Passing sticky-header as the props in b-table

Let’s start today’s tutorial let’s check the examples of how to add a sticky header in b-table in BootstrapVue?

Install the Bootstrap and BootstrapVue

First, we have to install the Vue project, I had installed the vueJS in my system. If you haven’t installed or have any problem with installation you can follow this article, it will show you step by step process of installation. In that article, I had also show you how to install BootstrapVue in your vue.js project step by step.

How to install or configure vue-bootstrap in VueJS?

Create FirstComponent.vue and import it into App.js

Create the new component into src/components/ components folder.the component name is FirstComponent.vue and import into App.js file:

App.js

<template>
  <div id="app">
    <FirstComponent />
  </div>
</template>

<script>
import FirstComponent from "./components/FirstComponent.vue";
export default {
  name: "App",
  components: {
    FirstComponent,
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Passing sticky-header as the props in b-table

We are going to create the sticky header in the bootstrap-vue table. Using the sticky-header prop to enable a vertically scrolling table with headers that remain fixed (sticky) as the table body scrolls. Let’s see the code:

FirstComponent.vue

<template>
  <div class="hello">
    <b-table sticky-header striped hover :items="items"></b-table>
  </div>
</template>

<script>
export default {
  name: "FirstComponent",
  data() {
    return {
      items: [
                { id: 1, site_name: "SortoutCode", site_url: "https://sortoutcode.com/" },
                { id: 2, site_name: "Aguidehub", site_url: "https://aguidehub.com/" },
                { id: 3, site_name: "Infinitbility", site_url: "https://infinitbility.com/" },
                { id: 4, site_name: "SortoutCode", site_url: "https://sortoutcode.com/" },
                { id: 5, site_name: "Aguidehub", site_url: "https://aguidehub.com/" },
                { id: 6, site_name: "SortoutCode", site_url: "https://sortoutcode.com/" },
                { id: 7, site_name: "Infinitbility", site_url: "https://infinitbility.com/" },
                { id: 8, site_name: "Test1", site_url: "https://test_1.com/" },
                { id: 9, site_name: "Test2", site_url: "https://test_1.com/" },
                { id: 10, site_name: "Test3", site_url: "https://test_1.com/" },
                { id: 11, site_name: "Test5", site_url: "https://test_1.com/" },
                { id: 12, site_name: "Test6", site_url: "https://test_1.com/" },
                { id: 13, site_name: "Test7", site_url: "https://test_1.com/" },
                { id: 14, site_name: "Test9", site_url: "https://test_1.com/" },

            ],
    };
  },
};
</script>

For now, let’s check the output.

Output

bootstrap vue b-table sticky header

All the best 👍.

Follow me on Twitter