Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuetify.js data-table fixed-header scrolling via CSS

The Vuetify's v-data-table component has a fixed-header feature, which makes the header sticky when the height is less than the table height.

It is working when the height is set explicitly.

HTML:

    <v-content id="mycontent">
      <v-container fluid id="mycontainer">
        <h2>Header</h2>
        <div id="outer">
          <v-data-table id="mytable" :headers="headers" :items="items" :height="height"
                        fixed-header disable-pagination disable-sort hide-default-footer>
          </v-data-table>
        </div>
      </v-container>
    </v-content>

CSS:

#mycontent {
}
#mycontainer {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#outer {
  flex: 1;
}
#mytable {
}
#mytable table {
  width: auto;
}

JS:

  data: () => ({
    headers: [
      { text: "Header A", value: "a" },
      { text: "Header B, longer", value: "b" },
      { text: "Header C", value: "c" }
    ],
    height: 100
  }),
  computed: {
    items() {
      var items = [];
      for (var i = 1; i <= 50; i++) {
        items.push({ a: "Row " + i, b: "Column B", c: "Column C" });
      }
      return items;
    }
  },
  methods: {
    onResize() {
      this.height = document.getElementById("outer").clientHeight;
    }
  },
  mounted() {
    window.addEventListener("resize", this.onResize);
    this.$nextTick(this.onResize);
  }

Live demo

But is it possible to achieve the same effect with pure CSS, or at least without setting height explicitly?

like image 314
rustyx Avatar asked Sep 15 '25 15:09

rustyx


2 Answers

Try using height clac

.v-data-table__wrapper{height:calc(100vh - 150px) !important;}
like image 77
Sumit Patel Avatar answered Sep 18 '25 06:09

Sumit Patel


I wrote a little article explaining my solution to this problem using position: sticky: https://medium.com/@jareklipski/sticky-table-header-in-vuetify-js-fab39988dc3

The overall idea is to set the <th> position to sticky and top value to 0 (or height of your fixed site header). Only one obstacle is that Vuetify.js data table has overflow, which can be addressed by /deep/ selector:

.v-data-table /deep/ .v-data-table__wrapper {
  overflow: unset;
}

Here's a complete working example: https://github.com/loomchild/sticky-vuetify-table-header

like image 40
loomchild Avatar answered Sep 18 '25 04:09

loomchild