Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(VDataTableServer): Support column slots #16707

Merged
merged 1 commit into from
Feb 22, 2023
Merged

fix(VDataTableServer): Support column slots #16707

merged 1 commit into from
Feb 22, 2023

Conversation

kingyyy
Copy link
Contributor

@kingyyy kingyyy commented Feb 18, 2023

Description

Fixes #16704

Markup:

<template>
    <v-data-table-server
      :headers="headers"
      :items="desserts"
      :items-length="totalDesserts"
      :loading="loading"
      :items-per-page="2"
      item-value="name"
      show-select
      show-expand
      class="elevation-1"
      @update:options="options = $event"
    >
    <template v-slot:column.name="{ column }">
      {{ column.title.toUpperCase() }}
    </template>
  </v-data-table-server>
</template>
<script>
  export default {
    data () {
      return {
        totalDesserts: 0,
        desserts: [],
        loading: true,
        options: {},
        headers: [
          {
            title: 'Dessert (100g serving)',
            align: 'start',
            sortable: false,
            key: 'name',
          },
          { title: 'Calories', key: 'calories' },
          { title: 'Fat (g)', key: 'fat' },
          { title: 'Carbs (g)', key: 'carbs' },
          { title: 'Protein (g)', key: 'protein' },
          { title: 'Iron (%)', key: 'iron' },
        ],
      }
    },
    watch: {
      options: {
        handler () {
          this.getDataFromApi()
        },
        deep: true,
      },
    },
    methods: {
      getDataFromApi () {
        this.loading = true
        this.fakeApiCall().then(data => {
          this.desserts = data.items
          this.totalDesserts = data.total
          this.loading = false
        })
      },
      /**
       * In a real application this would be a call to fetch() or axios.get()
       */
      fakeApiCall () {
        return new Promise((resolve, reject) => {
          const { sortBy = [], page, itemsPerPage } = this.options

          let items = this.getDesserts()
          const total = items.length

          if (sortBy.length) {
            items = items.sort((a, b) => {
              const sortA = a[sortBy[0].key]
              const sortB = b[sortBy[0].key]

              if (sortBy[0].order) {
                if (sortA < sortB) return 1
                if (sortA > sortB) return -1
                return 0
              } else {
                if (sortA < sortB) return -1
                if (sortA > sortB) return 1
                return 0
              }
            })
          }

          if (itemsPerPage > 0) {
            items = items.slice((page - 1) * itemsPerPage, page * itemsPerPage)
          }

          setTimeout(() => {
            resolve({
              items,
              total,
            })
          }, 1000)
        })
      },
      getDesserts () {
        return [
          {
            name: 'Frozen Yogurt',
            calories: 159,
            fat: 6.0,
            carbs: 24,
            protein: 4.0,
            iron: 1,
          },
          {
            name: 'Ice cream sandwich',
            calories: 237,
            fat: 9.0,
            carbs: 37,
            protein: 4.3,
            iron: 1,
          },
          {
            name: 'Eclair',
            calories: 262,
            fat: 16.0,
            carbs: 23,
            protein: 6.0,
            iron: 7,
          },
          {
            name: 'Cupcake',
            calories: 305,
            fat: 3.7,
            carbs: 67,
            protein: 4.3,
            iron: 8,
          },
          {
            name: 'Gingerbread',
            calories: 356,
            fat: 16.0,
            carbs: 49,
            protein: 3.9,
            iron: 16,
          },
          {
            name: 'Jelly bean',
            calories: 375,
            fat: 0.0,
            carbs: 94,
            protein: 0.0,
            iron: 0,
          },
          {
            name: 'Lollipop',
            calories: 392,
            fat: 0.2,
            carbs: 98,
            protein: 0,
            iron: 2,
          },
          {
            name: 'Honeycomb',
            calories: 408,
            fat: 3.2,
            carbs: 87,
            protein: 6.5,
            iron: 45,
          },
          {
            name: 'Donut',
            calories: 452,
            fat: 25.0,
            carbs: 51,
            protein: 4.9,
            iron: 22,
          },
          {
            name: 'KitKat',
            calories: 518,
            fat: 26.0,
            carbs: 65,
            protein: 7,
            iron: 6,
          },
        ]
      },
    },
  }
</script>

Fixing missing header slots
@kingyyy kingyyy changed the title Update VDataTableServer.tsx fix(VDataTableServer): Missing column slots Feb 18, 2023
@kingyyy kingyyy changed the title fix(VDataTableServer): Missing column slots fix(VDataTableServer): Support column slots Feb 18, 2023
@johnleider johnleider added T: bug Functionality that does not work as intended/expected C: VDataTableServer labels Feb 20, 2023
@johnleider johnleider added this to the v3.1.x milestone Feb 20, 2023
@KaelWD KaelWD merged commit fe92699 into vuetifyjs:master Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: VDataTableServer T: bug Functionality that does not work as intended/expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] VDataTable header slot
3 participants