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(VDataFooter): better handle custom item options #8029

Merged
merged 2 commits into from Jul 26, 2019

Conversation

johnleider
Copy link
Member

fixes #8026

Description

When providing custom itemsPerPageOptions, if the current item did not exist in the options, it was pushed into the array. This means if no options are provided, 10 is the default and was being pushed into the array.

Now the system simply determines if the provided itemsPerPage exists in the items and if not, the first item is selected.

Motivation and Context

itemsPerPage has a default value of 10, which consequently was getting pushed into the footers items array because it was a value that did not exist within the provided list. This causes issues when adding custom itemsPerPageOptions that does not contain the value 10, (or whatever value is defined for the options).

How Has This Been Tested?

jest / visually

Markup:

<template>
  <div class="ma-5 pa-5">
    <v-data-table
      :headers="headers"
      :items="desserts"
      :options.sync="options"
      :server-items-length="totalDesserts"
      :loading="loading"
      class="elevation-1"
      :footer-props="footerProps"
    ></v-data-table>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        totalDesserts: 0,
        desserts: [],
        loading: true,
        footerProps: { itemsPerPageOptions : [5, 15, -1] },
        // footerProps: { itemsPerPageOptions : [50, 60, 70] },
        options: {
          // itemsPerPage: -1,
          // itemsPerPage: 60,
        },
        headers: [
          {
            text: 'Dessert (100g serving)',
            align: 'left',
            sortable: false,
            value: 'name',
          },
          { text: 'Calories', value: 'calories' },
          { text: 'Fat (g)', value: 'fat' },
          { text: 'Carbs (g)', value: 'carbs' },
          { text: 'Protein (g)', value: 'protein' },
          { text: 'Iron (%)', value: 'iron' },
        ],
      }
    },
    watch: {
      options: {
        handler () {
          this.getDataFromApi()
            .then(data => {
              this.desserts = data.items
              this.totalDesserts = data.total
            })
        },
        deep: true,
      },
    },
    mounted () {
      this.getDataFromApi()
        .then(data => {
          this.desserts = data.items
          this.totalDesserts = data.total
        })
    },
    methods: {
      getDataFromApi () {
        this.loading = true
        return new Promise((resolve, reject) => {
          const { sortBy, descending, page, itemsPerPage } = this.options

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

          if (this.options.sortBy) {
            items = items.sort((a, b) => {
              const sortA = a[sortBy]
              const sortB = b[sortBy]

              if (descending) {
                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(() => {
            this.loading = false
            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>

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Improvement/refactoring (non-breaking change that doesn't add any features but makes things better)

Checklist:

  • The PR title is no longer than 64 characters.
  • The PR is submitted to the correct branch (master for bug fixes and documentation updates, dev for new features and breaking changes).
  • My code follows the code style of this project.
  • I've added relevant changes to the documentation (applies to new features and breaking changes in core library)
  • I've added new examples to the kitchen (applies to new features and breaking changes in core library)

… match

default itemsPerPage is 10 and was being pushed into the items array

fixes #8026
@johnleider johnleider added the T: bug Functionality that does not work as intended/expected label Jul 26, 2019
@johnleider johnleider added this to the v2.0.x milestone Jul 26, 2019
@johnleider johnleider requested a review from a team July 26, 2019 02:34
@johnleider johnleider self-assigned this Jul 26, 2019
@codecov
Copy link

codecov bot commented Jul 26, 2019

Codecov Report

Merging #8029 into master will decrease coverage by <.01%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #8029      +/-   ##
==========================================
- Coverage   85.77%   85.77%   -0.01%     
==========================================
  Files         334      334              
  Lines        9092     9095       +3     
  Branches     2413     2413              
==========================================
+ Hits         7799     7801       +2     
- Misses       1205     1206       +1     
  Partials       88       88
Impacted Files Coverage Δ
...uetify/src/components/VDataIterator/VDataFooter.ts 94.33% <85.71%> (-2.03%) ⬇️
packages/vuetify/src/services/goto/index.ts 93.93% <0%> (+1.08%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 11a223c...4a8262d. Read the comment docs.

nekosaur
nekosaur previously approved these changes Jul 26, 2019
@johnleider johnleider merged commit 8b7a25b into master Jul 26, 2019
@johnleider johnleider deleted the fix/8026-data-table-pagination branch July 26, 2019 13:43
@lock lock bot locked as resolved and limited conversation to collaborators Aug 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
T: bug Functionality that does not work as intended/expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug Report] 10 shows up in pagination options even when overridden
2 participants