javascript - Sticky Header in Quasar QTable - Stack Overflow

I'm trying to get the header on my QTable to be sticky. I have used the code from the Quasar docum

I'm trying to get the header on my QTable to be sticky. I have used the code from the Quasar documentation into my project and only my template at the top of the table is sticky while the header of the table is not. After a lot of playing around and experimenting with different code from people, I still have not been able to acplish what I want to do. Any help would be appreciated. Component code is below:

<template>
  <div>
    <q-toolbar class="bg-grey-1 text-subtitle1 text-blue-grey-8 shadow-2 rounded-borders">
      <q-breadcrumbs class="text-grey" active-color="primary">
        <template v-slot:separator>
          <q-icon size="24px" name="arrow_forward" color="primary" />
        </template>

        <q-breadcrumbs-el
          label="Settings"
          icon="settings"
          class="hover cursor-pointer"
          @click="$router.push('/system')"
        />
        <q-breadcrumbs-el label="Course" class="text-primary" icon="class" />
      </q-breadcrumbs>
    </q-toolbar>
    <q-table
      class="q-mt-sm my-sticky-virtscroll-table"
      :columns="columns"
      :data.sync="getData"
      :pagination.sync="serverPagination"
      row-key="id"
      selection="single"
      :selected.sync="selected"
      @request="request"
    >
      <template slot="top">
        <q-btn dense color="primary" icon="arrow_drop_down" class="q-mr-sm"></q-btn>
        <q-input
          v-model="filter"
          placeholder="Search by Academic Program"
          type="text"
          class="col-3"
          @keypress.enter.native="search"
        />
        <q-btn
          class="q-pl-sm q-pr-sm"
          color="primary"
          flat
          @click="$refs.addCourse.toggle()"
        >
          <q-icon name="fas fa-plus" />
          <q-tooltip
            anchor="top middle"
            self="bottom middle"
            :offset="[10, 10]"
          >
            <strong>Add new Course</strong>
          </q-tooltip>
        </q-btn>
        <q-btn
          class="q-pl-sm"
          color="primary"
          :disable="!selected.length"
          flat
          @click="$refs.editCourse.toggle(selected[0])"
        >
          <q-icon name="fas fa-pencil-alt" />
          <q-tooltip
            anchor="top middle"
            self="bottom middle"
            :offset="[10, 10]"
          >
            <strong>Edit Course</strong>
            <br />Select record first
          </q-tooltip>
        </q-btn>
        <div class="col" />
        <q-btn
          color="negative"
          :disable="!selected.length"
          flat
          round
          @click="$refs.deleteCourse.toggle()"
        >
          <q-icon name="fas fa-trash-alt" />
          <q-tooltip
            anchor="top middle"
            self="bottom middle"
            :offset="[10, 10]"
          >
            <strong>Delete Course</strong>
            <br />Select record first
          </q-tooltip>
        </q-btn>
      </template>
    </q-table>
    <addCourse ref="addCourse"></addCourse>
    <editCourse :data="this.selected" ref="editCourse"></editCourse>
    <deleteCourse ref="deleteCourse"></deleteCourse>
  </div>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator'
import AddCourse from './AddCourse.vue'
import EditCourse from './EditCourse.vue'
import DeleteCourse from './DeleteCourse.vue'
import { Course, CourseModule, DataTablePagination } from '../../../store/course/index'

@Component({
  ponents: {
    addCourse: AddCourse,
    editCourse: EditCourse,
    deleteCourse: DeleteCourse
  }
})
export default class ManageCourse extends Vue {
  private filter: string = ''
  private selected: Course [] = []
  private columns = [
    {
      name: 'name',
      label: 'Name',
      align: 'center',
      required: true,
      field: 'name',
      sortable: true
    },
    {
      name: 'code',
      label: 'Code',
      align: 'center',
      required: true,
      field: 'code',
      sortable: true
    },
    {
      name: 'creditHours',
      label: 'Credit Hours',
      align: 'center',
      required: true,
      field: 'creditHours'
    },
    {
      name: 'numberOfLabs',
      label: 'Number of Labs',
      align: 'center',
      required: true,
      field: 'numberOfLabs'
    },
    {
      name: 'contactHours',
      label: 'Contact Hours',
      align: 'center',
      required: true,
      field: 'contactHours'
    }
  ]

  get getData() {
    return CourseModule.courses
  }

  get serverPagination() {
    return CourseModule.pagination
  }

  set serverPagination(value: DataTablePagination) {
    CourseModule.SET_PAGINATION(value)
  }

  request(args: any) {
    CourseModule.SET_PAGINATION(args.pagination)
    CourseModule.fetchCourses()
  }

  search() {
    CourseModule.SET_FILTER(this.filter)
    CourseModule.fetchCourses()
  }

  beforeMount() {
    CourseModule.fetchCourses()
  }
}
</script>

<style scoped lang="sass">
.my-sticky-virtscroll-table
  /* height or max-height is important */
  height: calc(100vh - 150px)

  .q-table__top,
  .q-table__bottom,
  thead tr:first-child th /* bg color is important for th; just specify one */
    background-color: #fff

  thead tr th
    position: sticky
    z-index: 1
  /* this will be the loading indicator */
  thead tr:last-child th
    /* height of all previous header rows */
    top: 48px
  thead tr:first-child th
    top: 0
</style>

I'm trying to get the header on my QTable to be sticky. I have used the code from the Quasar documentation into my project and only my template at the top of the table is sticky while the header of the table is not. After a lot of playing around and experimenting with different code from people, I still have not been able to acplish what I want to do. Any help would be appreciated. Component code is below:

<template>
  <div>
    <q-toolbar class="bg-grey-1 text-subtitle1 text-blue-grey-8 shadow-2 rounded-borders">
      <q-breadcrumbs class="text-grey" active-color="primary">
        <template v-slot:separator>
          <q-icon size="24px" name="arrow_forward" color="primary" />
        </template>

        <q-breadcrumbs-el
          label="Settings"
          icon="settings"
          class="hover cursor-pointer"
          @click="$router.push('/system')"
        />
        <q-breadcrumbs-el label="Course" class="text-primary" icon="class" />
      </q-breadcrumbs>
    </q-toolbar>
    <q-table
      class="q-mt-sm my-sticky-virtscroll-table"
      :columns="columns"
      :data.sync="getData"
      :pagination.sync="serverPagination"
      row-key="id"
      selection="single"
      :selected.sync="selected"
      @request="request"
    >
      <template slot="top">
        <q-btn dense color="primary" icon="arrow_drop_down" class="q-mr-sm"></q-btn>
        <q-input
          v-model="filter"
          placeholder="Search by Academic Program"
          type="text"
          class="col-3"
          @keypress.enter.native="search"
        />
        <q-btn
          class="q-pl-sm q-pr-sm"
          color="primary"
          flat
          @click="$refs.addCourse.toggle()"
        >
          <q-icon name="fas fa-plus" />
          <q-tooltip
            anchor="top middle"
            self="bottom middle"
            :offset="[10, 10]"
          >
            <strong>Add new Course</strong>
          </q-tooltip>
        </q-btn>
        <q-btn
          class="q-pl-sm"
          color="primary"
          :disable="!selected.length"
          flat
          @click="$refs.editCourse.toggle(selected[0])"
        >
          <q-icon name="fas fa-pencil-alt" />
          <q-tooltip
            anchor="top middle"
            self="bottom middle"
            :offset="[10, 10]"
          >
            <strong>Edit Course</strong>
            <br />Select record first
          </q-tooltip>
        </q-btn>
        <div class="col" />
        <q-btn
          color="negative"
          :disable="!selected.length"
          flat
          round
          @click="$refs.deleteCourse.toggle()"
        >
          <q-icon name="fas fa-trash-alt" />
          <q-tooltip
            anchor="top middle"
            self="bottom middle"
            :offset="[10, 10]"
          >
            <strong>Delete Course</strong>
            <br />Select record first
          </q-tooltip>
        </q-btn>
      </template>
    </q-table>
    <addCourse ref="addCourse"></addCourse>
    <editCourse :data="this.selected" ref="editCourse"></editCourse>
    <deleteCourse ref="deleteCourse"></deleteCourse>
  </div>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator'
import AddCourse from './AddCourse.vue'
import EditCourse from './EditCourse.vue'
import DeleteCourse from './DeleteCourse.vue'
import { Course, CourseModule, DataTablePagination } from '../../../store/course/index'

@Component({
  ponents: {
    addCourse: AddCourse,
    editCourse: EditCourse,
    deleteCourse: DeleteCourse
  }
})
export default class ManageCourse extends Vue {
  private filter: string = ''
  private selected: Course [] = []
  private columns = [
    {
      name: 'name',
      label: 'Name',
      align: 'center',
      required: true,
      field: 'name',
      sortable: true
    },
    {
      name: 'code',
      label: 'Code',
      align: 'center',
      required: true,
      field: 'code',
      sortable: true
    },
    {
      name: 'creditHours',
      label: 'Credit Hours',
      align: 'center',
      required: true,
      field: 'creditHours'
    },
    {
      name: 'numberOfLabs',
      label: 'Number of Labs',
      align: 'center',
      required: true,
      field: 'numberOfLabs'
    },
    {
      name: 'contactHours',
      label: 'Contact Hours',
      align: 'center',
      required: true,
      field: 'contactHours'
    }
  ]

  get getData() {
    return CourseModule.courses
  }

  get serverPagination() {
    return CourseModule.pagination
  }

  set serverPagination(value: DataTablePagination) {
    CourseModule.SET_PAGINATION(value)
  }

  request(args: any) {
    CourseModule.SET_PAGINATION(args.pagination)
    CourseModule.fetchCourses()
  }

  search() {
    CourseModule.SET_FILTER(this.filter)
    CourseModule.fetchCourses()
  }

  beforeMount() {
    CourseModule.fetchCourses()
  }
}
</script>

<style scoped lang="sass">
.my-sticky-virtscroll-table
  /* height or max-height is important */
  height: calc(100vh - 150px)

  .q-table__top,
  .q-table__bottom,
  thead tr:first-child th /* bg color is important for th; just specify one */
    background-color: #fff

  thead tr th
    position: sticky
    z-index: 1
  /* this will be the loading indicator */
  thead tr:last-child th
    /* height of all previous header rows */
    top: 48px
  thead tr:first-child th
    top: 0
</style>
Share Improve this question asked Feb 14, 2020 at 16:19 Jeffrey pennerJeffrey penner 1951 gold badge4 silver badges19 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

It's a Vue scoped-css thing.

By default, the scoped styles won't affect child ponents, unless you add the special deep binator, which is >>>. For preprocessors, such as sass, use the /deep/ binator alias or the ::v-deep pseudo-element.

So, you should do something like:

<style scoped lang="sass">
.my-sticky-virtscroll-table::v-deep
  /* height or max-height is important */
  height: calc(100vh - 150px)

  .q-table__top,
  .q-table__bottom,
  thead tr:first-child th /* bg color is important for th; just specify one */
    background-color: #fff

  /* ... */
</style>

Edit: use ::v-deep instead of the deprecated /deep/. See https://stackoverflow./a/55368933/645296

<style lang="sass">
.sticky-header
  /* height or max-height is important */
  max-height: 100vh

  .q-table__top,
  .q-table__bottom,
  thead tr:first-child th
    /* bg color is important for th; just specify one */
    background-color: #fff

  thead tr th
    position: sticky
    z-index: 1
  thead tr:first-child th
    top: 0

  /* this is when the loading indicator appears */
  &.q-table--loading thead tr:last-child th
    /* height of all previous header rows */
    top: 48px
</style>
<q-table class="sticky-header"></q-table>

https://quasar.dev/vue-ponents/table#Sticky-header%2Fcolumn

I think this will work for you.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744940041a4602265.html

相关推荐

  • javascript - Sticky Header in Quasar QTable - Stack Overflow

    I'm trying to get the header on my QTable to be sticky. I have used the code from the Quasar docum

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信