Customizing the design of vuetify table header to your liking

My goal is to implement a custom style for v-data table headers using the fixHeader method. The CSS code is intended to keep the header fixed in place even when scrolling horizontally.

The issue arises as the style is applied to the inner <span> element within the outer <<th>>, fixing the header relative to the th element. What I actually need is for this dynamic style to be applied to the outer th element instead of its inner span, so that it fixes the header relative to the entire row of headers.

<template>
  <v-data-table
    dense
    :headers="listAttrs"
    :items="list"
    >
      <template v-for="(col, i) in fixedColumns" v-slot:[`header.${col.value}`]="{ header }">
       <span :style="fixHeader(col.value)"  >{{ col.text }}</span>
      </template>
  </v-data-table>
</template>

<script>
  fixHeader(key) {        
    let offset = 0
    let index = this.listAttrs.findIndex(el => el.value === key)
    let col = this.listAttrs[index]
    let fixed = col?.fixed

    for (let x = 0 ;  x < index ; x++ ) {
      offset += Number(this.listAttrs[x].width)
    
      if(this.fixedColumns[this.fixedColumns.length - 1].value === key)
        return {
          left: offset+'px', position: 'sticky !important',background: 'white',boxShadow: 'inset -7px 0 9px -7px rgba(0,0,0,0.4)',  
        } else if(fixed) {
          return {left: offset+'px', position: 'sticky !important', background: 'white', 
        }
     }
  }  
</script>

Shown below is an image of the parsed HTML code:

I am aware of setting a class property in the headers array and defining a class that will be applied to the th element:

headers: [
  { text: 'Actions', value: 'actions', class:'fixed1' },
  { text: 'Inst',value: 'inst',class:'fixed2' },
]

However, this approach doesn't work for me because it only allows passing fixed classes, whereas I need to pass in a dynamic style with the left property adjusted based on a function.

Moreover, by hiding the default header using hide-default-header prop and overwriting all headers with your own template, you lose the sorting functionality provided by the default Header. Is there a way to maintain both the custom styling and sorting capability?

Answer №1

If you want to have headers that adjust based on certain conditions, consider defining them as a computed property in your Vue.js project.

computed: {
    headers(){
      ...
    }
}

For a more detailed example, take a look at this link: https://codepen.io/ramseyfeng/pen/NWVgKBm

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

Modify the state from a listener that is enclosed in the useEffect function

Within my hook called useQueryEvents, I have a setup that involves fetching all past transactions for a user and listening to the network for incoming/outgoing transactions. These transactions are then passed into a function called addActionToActivity, whi ...

Display the second dropdown after the user has made a selection in the first dropdown

Recently, I've been delving into the world of html/javascript and navigating through the process of implementing the code found in this specific link. While I can see the solution outlined in the link provided below, I'm struggling with how to i ...

What are the steps for sorting objects in an array by their data type attribute?

I am currently working with a JavaScript array of people objects that is dynamically rendered from JS. My goal is to implement a filter on the objects based on the selection made in the dropdown menu and matching it with the department attribute specified ...

How can I change the orientation of a cube using d3js?

Seeking guidance on creating an accurate chart using d3js How can I rotate the SVG to display the opposite angle as shown in the image? Any recommended resources for achieving this desired result would be greatly appreciated. The provided code only disp ...

The $route.reload() function seems to be ineffective in Internet Explorer

I'm currently using AngularJs to develop an application. The issue I am encountering is related to data not being refreshed in IE, even after executing the $route.reload() function. Strangely enough, this problem only occurs in Internet Explorer and w ...

What is the process for spawning a terminal instance within a NodeJS child process?

I'm in the process of setting up a discord channel to serve as an SSH terminal. This involves using a NodeJS server to establish the connection. A custom command will then be used to spawn a new terminal instance that can function as a shell. However ...

What are the advantages and disadvantages of transmitting data to Vue through Blade Views in Laravel compared to sending it directly to Vue Components using Axios?

Currently, I am transferring data from Laravel to Vue (using MySQL DB's) in the following manner: routes/web.php: Route::get('/', [MyController::class, 'index']); app/Http/Controllers/MyController.php: public function index() { ...

What is the method to implement timeago.js?

I've recently embarked on my journey to learn JavaScript, and it has only been two weeks since I started. As a beginner, I'm encountering some difficulties with implementing timeago from . The specific line of instruction that's giving me tr ...

Deactivating Bootstrap Modal in Angular

Looking for advice on managing a Bootstrap Modal in Angular 7 I have a Form inside a Bootstrap Modal that I need to reset when the modal is closed (by clicking outside of it). Despite searching on Google, I haven't been able to find a solution. Any ...

vue rich text editor with syncfusion image upload functionality

Is there a way to upload an image into my vuex action and retrieve the image value? I'm facing issues with loading it through vuex. Can I download images via vuex as well? Take a look at the screenshots below. View Image View Image View Image <tem ...

What is the best way to adjust the color of the colon within my timer digits using a span element?

I am facing an issue with my timer where the span that was created to display a colon between the numbers does not change color along with the timer digits. I attempted using var colon = document.getElementById(":"); but it still did not work as expected. ...

What is the best way to utilize a reduce function to eliminate the need for looping through an object in JavaScript?

Currently, my code looks like this: const weekdays = eachDay.map((day) => format(day, 'EEE')); let ret = { Su: '', Mo: '', Tu: '', We: '', Th: '', Fr: '', Sa: '' }; w ...

Troubleshoot and resolve the issue of a page scroll freeze bug occurring while scrolling through an overflowed

My webpage features a Hero section with 2 columns - the left column contains a gallery slider, and the right column consists of 2 text blocks. The challenge here is that the right column needs to be 100% of the screen height while scrolling. To achieve thi ...

Utilizing Jquery to toggle collapsed divs with a click event

Here is how my HTML appears: <h3><a href="#" title="story title">Story Title</a> <img class="expandstory" src="/images/plus.png" /></h3> <div class="description">Short description about the story</div> <h3 ...

Configuring Angular routes based on service method invocation

I have my routes configured in @NgModule. I also have a service that determines which parts of the application to display based on specific conditions. I need to call this service and adjust the routes according to its output. Issue: The route configurati ...

What is the code to create a forward slash on a webpage using HTML/CSS?

I want to create a unique parallelogram/slash design on my website. While it's simple to place two rectangles side by side, achieving the slash effect is proving to be quite challenging. Can this be done using only CSS or HTML, or does it require SVGs ...

Adjusting the minimum value on a textfield with JQuery Validate plugin in real-time

I am attempting to dynamically update the minimum value on one field based on input from other fields. Here is a brief overview of my code: $("#new_project").on("click", function() { switch($('input:radio[name=quality-level]:checked').val() ...

Capturing images on Android devices through the camera option using HTML input file type

Having an issue with an LG G2 Android device running version 4.4.2 and the default browser. I'm currently using this tag to allow users to upload images from their devices to the server: {<input type="file" accept="image/*;” />} When I clic ...

My bootstrap collapse navbar isn't functioning properly. Despite pressing the icon on a smaller screen, the menu fails to open. Can anyone provide a solution to this issue?

The hamburger menu is not working properly. I am facing an issue where the navigation does not appear when I press on the hamburger icon on a small screen. Can someone please guide me on how to resolve this problem? :/ <nav class="navbar bg-d ...

The error message states that the property "user" is not found in the type "Session & Partial<SessionData>"

I recently had a javascript code that I'm now attempting to convert into typescript route.get('/order', async(req,res) => { var sessionData = req.session; if(typeof sessionData.user === 'undefined') { ...