Achieving a scrolling body with a fixed header in a Vue b-table

Currently, I have implemented a b-table element on a webpage to display data from a database. The table is paginated, but feedback suggests that users prefer all information displayed in a single scrolling view. However, the issue arises when I attempt to scroll the entire table within a containing div, as it also moves the column headers out of view. My goal is to find a solution where only the table body scrolls while keeping the column headers stationary, all without resorting to complex Javascript manipulation.

In the component reference for the bootstrap-vue table component, there is mention of a property called tbody-class, which appears to offer a way to define a custom class for the tbody element. Unfortunately, the page does not provide clear instructions on how to utilize this feature, and my attempts at using it have yielded no success:

<b-table 
    tbody-class="my-class"   <- Adds prop to table but not tbody
    :tbody-class="my-class"  <- Seems to be completely ignored
>

An apparent solution to a similar problem was mentioned in this issue thread, though details on the actual resolution are lacking. It references an upcoming update that should address the issue, yet neither the subsequent version's patch notes nor documentation acknowledge this addition (unless referring to the previous properties). Additionally, suggestions about applying styles through CSS selectors indirectly were ineffective in my case, as evidenced by inspecting the tbody element in Chrome.

.table.my-table > tbody {
    height: 100px;
}

The Vue version being used is 2.2.6.

Answer №1

Starting from version 2.0.0-rc.28, an optional prop sticky-header has been included to enable fixed headers.

new Vue({
  el: '#app',
  data: {
    items: [
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' },
      { heading1: 'table cell', heading2: 'table cell', heading3: 'table cell' }
    ],
  }
});
<html>

<header>
  <script src="//unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0a6a5b590e2fee6fee1e1">[email protected]</a>/dist/vue.min.js"></script>
  <script src="//unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e2c21213a3d3a3c2f3e63383b2b0e7c607e607e">[email protected]</a>/dist/bootstrap-vue.js"></script>

  <link type="text/css" rel="stylesheet" href="//unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="284a47475c5b5c5a4958681c061b0619">[email protected]</a>/dist/css/bootstrap.min.css" />
  <link type="text/css" rel="stylesheet" href="//unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2c0cdcdd6d1d6d0c3d28fd4d7c7e2908c928c92">[email protected]</a>/dist/bootstrap-vue.min.css" />
</header>

<body>
  <div id="app">
    <b-table sticky-header :items="items" head-variant="light"></b-table>
  </div>
</body>

</html>

Answer №2

In search of a solution to scroll only the table body while keeping the column headers fixed, I aim to achieve this within the element's boundaries without resorting to complex JavaScript modifications.

To demonstrate how this can be accomplished, I have created an example repository which includes a hidden scrollbar as well.

While there may be alternative methods to achieve the same effect, here is what worked for me.

<!-- wrap table in container with class. -->
    <b-container fluid class="table-container">
      <b-table
        :items="items" 
        :fields="fields" 
        caption-top
      >
      </b-table>
    </b-container>

Below is the CSS code snippet that hides the scrollbar.

.table-container {
     height: calc(100vh - 450px);
}
 .table-container table {
     display: flex;
     flex-flow: column;
     height: 100%;
     width: 100%;
}
 .table-container table thead {
     flex: 0 0 auto;
     width: 100%;
}
  (Additional CSS property declarations omitted for brevity) 
 ...
 .table-container tr:nth-child(even) {
     background-color: rgba(168, 168, 239, 1);
}

/* START Adjustments for width and scrollbar hidden */
 .table-container th.table-action, .table-container td.table-action {
     width: 5.8vw;
}
 (More adjustments included in the original text) 

An alternative approach for a fixed header with limitations can also be implemented (reference):

Various techniques can be utilized for this purpose, but consistent functionality across different browsers cannot always be guaranteed:

Consider creating a class called my-table-scroll

table.my-table-scroll,
 (Further CSS rules provided in the original content) 
 ...

To implement this, apply the specified class to your b-table component.

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

Obtain form data as an object in Vue when passed in as a slot

Currently, I am working on developing a wizard tool that allows users to create their own wizards in the following format: <wiz> <form> <page> <label /> <input /> </page> <page> <label /> ...

Handling a change event for a mat-datepicker in Angular 7 can be tricky, especially when its value is tied to an optional input parameter. Let's dive into how to

I've recently started working with angular development and encountered a challenge with a mat-datepicker. The value of the datepicker is connected to filterDate using [(ngModel)] as an @Input() parameter. I have implemented a handleChange event that e ...

Bypass the need for a login screen when you navigate to a Vue route link

I'm currently utilizing Laravel and Vue.js in my application. All of my Vue routes prompt for a login because I'm using Laravel auth. However, I would like to create certain routes that can be accessed without requiring a user to be logged in. Es ...

The ng-route feature seems to be malfunctioning and I am unable to identify the error, as no information is being displayed in the console

Here is the code in my boot.php file where I have set up the links <ul class="nav nav-pills nav-stacked"> <li role="presentation"><a href="#/valley">Valley</a></li> <li role="presentation"><a href="#/beach"> ...

HTML and CSS: Focus on border for <td> cells only

I'm looking to apply a border to only one cell (B2) without nesting tables. Here's the code: <table border="0"> <tr> <td>A1</td> <td>B1</td> <td>C1</td> </tr> ...

Creating a Map Using HTML and JavaScript

My current project involves creating a simple map that can be moved with mouse drag functionality, featuring images such as islands. I have successfully retrieved the mouse position by declaring variables posX and e.clientX, as well as for e.clientY. Howe ...

Ways to establish unchanging data based on a prop that has the potential to be modified

I am currently working on a demo project that is similar to a Todo-List, and I am facing a challenge with the implementation. Here is an excerpt of the code: Parent.vue <template> <li v-show="currentTab === 1"> <child key="1" :items="p ...

Aligning images in center of list

I'm in need of some CSS expertise to assist with this layout. Here is the current structure: <div class="movieList"> <div class="image" selected = true> <img class="poster" src="image1" selected = true/> </div> <d ...

Utilizing jQuery to effortlessly generate parent and child elements in a single function call

Is it possible to create multiple elements with the same function? Currently, I am able to split a number of list items like this: <ul class="dropdown-menu"> <li>Stuff</li> <li>Stuff</li> <li>Stuff</li> ...

This method or property is not supported by the object - How to call an Applet in Internet Explorer 9

cmd > java -version Java Version : "1.7.0_11" (Updated) Java(TM) SE Runtime Environment (build 1.7.0_11-b21) Java HotSpot(TM) Client VM (build 23.6-b04, mixed mode, sharing) browsers = IE7,IE8,IE9 (The functionality works smoothly in Google Chrome and ...

Using jQuery to insert a new string into the .css file

I'm currently working on a jQuery function to make my font size responsive to changes in width. While I am aware of other options like Media Query, I prefer a solution that offers smoother transitions. Using vw or vh units is not the approach I want t ...

Browser comparison: Chrome and Firefox - peculiar ID name causing table width difference without CSS. Suspected AdBlocking interference

There seems to be an issue with a table I have in Chrome. Whenever I change the ID name, it either sets the width to zero or doesn't display at all. You can view it here: http://jsfiddle.net/kdubs/W6GTE/ The specific line causing trouble is: <ta ...

What is the best way to bring up the keyboard on an iPhone when focusing an HTML text field?

Currently working on developing a web app for iPhone, and looking to implement a feature where a text field is automatically focused upon page load, prompting the keyboard to appear. Despite attempting the standard Javascript method: input.focus(); I see ...

Unable to include below the heading for the images

As I venture into the world of HTML, I am facing a challenge with placing titles below two images. Every time I attempt to add either h or p, the text ends up on the right side instead of below the images. What could I be doing wrong? This is all part of ...

Utilize JavaScript to trigger a function depending on the selected options from dropdown menus

I've been working on a fun little project for a web page where users can select two items from a drop-down menu and then click a button to play a sound corresponding to their selections. However, I'm facing some challenges with getting my JavaScr ...

Activate a side navigation bar in AdminLTE-3

I am using AdminLTE3 as my admin panel in Laravel, but I am facing an issue with the nav-item links not activating when clicked. Even though I have all the required assets for AdminLTE3 and used the starter.html file, the navigation items are not working p ...

Make the inner div fill the entire width within a container of a fixed width

On my blog page, I am currently using the col-md-8 class. Inside this column, my content is displayed. However, I want to stretch a particular div to occupy the full width within the col-md-8. Here is the section of my code: https://i.stack.imgur.com/uPS5 ...

Images are not visible when scrolling horizontally

Hello everyone, this is my first time reaching out with a question here! I've been working on setting up a horizontal scroll feature and I'm almost there, but I'm encountering an issue where the scroll bar is appearing at the bottom of the ...

The top value in CSS animation fails to change properly

Can anyone help me figure out why the animation doesn't work when I try to change the vertical position of a form using JQuery? I want the input field to smoothly move to its new position so that users can see it happening. Here is the JsFiddle link: ...

Learning how to track mouse wheel scrolling using jQuery

Is there a way to track mouse scrolling using jquery or javascript? I want the initial value to be 0 and increment by 1 when scrolling down and decrement by 1 when scrolling up. The value should always be positive. For example, if I scroll down twice, the ...