What is the process for fixing the top header row to display column titles in a bootstrap-vue table?

I am currently using a bootstrap-vue table with the following structure;

https://i.sstatic.net/5sv6R.png

Below is the code snippet for reference;

<template>
  <div>
    <b-table hover :items="items"></b-table>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        items: [
          { age: 40, first_name: 'Dickerson', last_name: 'Macdonald' },
          { age: 21, first_name: 'Larsen', last_name: 'Shaw' },
          {
            age: 89,
            first_name: 'Geneva',
            last_name: 'Wilson',
            _rowVariant: 'danger'
          },
          {
            age: 40,
            first_name: 'Thor',
            last_name: 'MacDonald',
            _cellVariants: { age: 'info', first_name: 'warning' }
          },
          { age: 29, first_name: 'Dick', last_name: 'Dunlap' }
        ]
      }
    }
  }
</script>

If dealing with a large number of rows in the table, there might be a need to pin the top header row in place. This will ensure that as you scroll through the content, the header row containing column names remains visible at the top, enhancing readability.

The table formatting provided here was sourced from the official bootstrap-vue documentation.

My current setup involves Vue v2.6 paired with BootstrapVue libraries.

Answer №1

If you're looking to make a table header sticky, BootstrapVue's table has a handy sticky-header prop just for that purpose.

The documentation explains how to use it:

<b-table :sticky-header="true">
  ...insert your table contents here
</b-table>
  • This will set the table's height to 300px
  • It will apply an overflow of auto
  • The table's position will be set to relative
  • The individual <th /> elements in the table header will have a position of sticky (with specific styles like z-index: 2)

Note: These settings are necessary for the table header to remain sticky relative to the table itself, rather than the entire page. Using @LajosArpad's solution may cause the table to stick to the page instead.

If you want a custom height for the sticky header, pass it as the value of the sticky-header prop:

<b-table sticky-header="80vh">
  ...insert your table contents here
</b-table>

Check out the working example on codesandbox.

Answer №2

To achieve the desired effect, you must apply the CSS property:

position: sticky;

For more information, refer to: https://developer.mozilla.org/en-US/docs/Web/CSS/position

Here is a demonstration:

.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  background-color: yellow;
  padding: 50px;
  font-size: 20px;
}
<div class="sticky">foo</div>
<div style="height: 2000px;"></div>

It is essential to understand Vue's generated structure and correctly apply the position: sticky; style to the desired sticky element.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Integrating Vue.js authentication with Laravel: How to do it?

I'm encountering an issue with my Vue.js component Code : <?php namespace App\Http\Controllers; use App\Models\Movie; use App\Models\User; use App\Models\Comment; use App\Models\CommentSpam; use A ...

transforming the elements within a div into a visual representation

I'm in the process of developing a web-based image editor. My main div will have multiple nested div elements within it. My goal is to save the entire main div as an image in a designated folder when the user clicks on a save button. I initially atte ...

Automatically numbering table columns with custom language localization in JavaScript using Jquery

Is there a method to automatically number table data in a local or custom language? As a Bengali individual, I have figured out how to automatically number the first data of each row using css and js. However, I am uncertain about how to implement custom n ...

Loading JS and CSS files in relation to the website root directory

When it comes to including CSS/JS files, what are the best practices for defining the file URI? Some people prefer to include files relative to the website root by specifying the full URI: <link rel="stylesheet" href="/my/path/to/css/main.css"> Thi ...

Dotted JavaScript property within an object

Having trouble sorting an array of objects using the lodash orderBy function because it doesn't work when the iteratees contain a dot in the middle. For a better understanding of the issue, check out this plunker. plunker ar users = [ { 'us ...

Assign the value from the list to a variable in order to execute an API call

Imagine a scenario where there's a button that displays a random joke based on a specific category. The categories are fetched using an API request from https://api.chucknorris.io/jokes/categories The jokes are generated from https://api.chucknorris. ...

What is the process for identifying children records of a parent (Adonis Lucid Many-to-Many) that match a specific criteria?

I am currently searching for the presence of specific Permissions within a single parent Role in a many-to-many relationship. const roles = await Role .query() .preload('permissions') this.role = roles.find(role => role.id === someid) co ...

Is it possible to include numbers and commas in JQuery Validation?

I have implemented a jQuery validation plugin to validate the fields of a form. One specific requirement is to validate a field to only allow commas and numbers. Below is the HTML code snippet: <input type="text" placeholder="Number of Employees" requ ...

Rotate the cylinder according to the normal vector of the tube endpoint

I'm working on creating a curved 3D arrow using three.js. My approach involves using a Tube along a curved path and a Cone shaped cylinder. Here's how they currently look: https://i.sstatic.net/jmVB6.png https://i.sstatic.net/P756z.png My goal ...

Selection box and interactive buttons similar to those found in Gmail

Looking to achieve these effects for <option> and <button> using CSS and JavaScript. Any suggestions on how to do this? ...

Using jQuery to highlight the navigation menu when a specific div scrolls into view

I have implemented a side navigation consisting of circular divs. Clicking on one scrolls you to the corresponding .block div, and everything functions correctly. However, I am now curious if it is feasible to highlight the relevant .nav-item div based on ...

Retrieve all the items listed in the markdown file under specific headings

Below is an example of a markdown file: # Test ## First List * Hello World * Lorem Ipsum * Foo ## Second List - Item 1 ## Third List + Item A Part of Item A + Item B ## Not a List Blah blah blah ## Empty ## Another List Blah blah blah * ITEM # ...

What steps can be taken to avoid the appearance of the JavaScript prompt "Leaving site"?

Hi there, I'm currently trying to find a way to remove the Javascript prompt/confirm message that asks "Do you want to leave this site?" like shown in this link: The issue I am facing is that when a modal opens and the user clicks on "YES", it redire ...

Retrieve the name of the product from the corresponding parent element

My goal is to trigger an alert box with the product name when the "Buy now" button is clicked. I have added the necessary code in jquery to maintain the onclick event of the button. <h2 class="product-name"><a href="product1.php" title="Sample Pr ...

Determine the sibling input value using jQuery in an ASP.NET MVC application

In my Asp.net MVC project in Visual Studio 2015, I have a page where I need to update the user's shopping cart with ajax when the quantity of an item is changed. The challenge I'm facing is retrieving the Article Code from another input field in ...

I'm currently working on a React toaster component, but I'm facing an issue where it's not displaying

I have created the Toaster component programmatically and I am passing the message through another component. This is how my Toaster component looks: export default class MyToaster extends React.Component { constructor(props) { super(props); ...

Sorting by number or date in indexedDB may lead to unexpected results due to the

I have set up an index in my database called time on the created_at field: var os = thisDB.createObjectStore(name, { keyPath : "id" }); os.createIndex("time", "created_at", {unique: false }); Instead of storing the time as a standard date format, I conv ...

Can you please explain why I am unable to remove the item in my code using Node.js and Express?

Currently, I am in the process of learning NodeJS and working on an application that involves adding Bicicleta objects. However, I have encountered an issue where I am unable to delete these objects successfully. Even though the POST request for deletion r ...

Tips for populating a DOJO Select using JSON data that includes various parameters instead of just label and value

I am trying to populate a DOJO select element with JSON data where the item values for value are expressed by the code property. Here's an example of what I have: //require dojo Select var select = new Select({ name: "stateSelect", ...

Using jQuery within MediaWiki pages beyond just Monobook.js or extensions is a powerful tool

Currently, I am experimenting with some jQuery functionalities in a MediaWiki content page to enhance various features. I have referred to the following resources: http://www.mediawiki.org/wiki/Manual:$wgRawHtml (particularly for incorporating Paypal but ...