【问题标题】:Handle limit data with 2 buttons (Next and previous button ) in vuejs在 vuejs 中使用 2 个按钮(下一个和上一个按钮)处理限制数据
【发布时间】:2021-11-20 03:36:10
【问题描述】:

在我的工作中,这个概念看起来在数组中有很多数据,但第一个我只想在数组中显示 4 个对象(id 1 到 id 4)。所以,接下来我按下下一个按钮,我想循环数组中的下一个对象(id 5 到 id 8),然后我按下后退按钮,它们返回到我传递给它们的上一个对象。我不知道这其中的逻辑。

<template>
  <v-container fluid>
    <v-row no-gutters>
      <v-col cols="12" md="12" sm="12">
        <v-card>
          <v-row no-gutters>
            <v-col cols="12" md="12" sm="12" class="">
              <v-row no-gutters class="d-flex">
                <v-col
                  cols="6"
                  md="6"
                  v-for="(item, index) in content"
                  :key="`item-${index}`"
                >
                  <v-col cols="6">
                    <v-card-text>
                      <p>{{ item.id }}</p>
                      <p>my name is : {{ item.title }}</p>
                    </v-card-text></v-col
                  >
                </v-col>
              </v-row>
            </v-col>
          </v-row>

          <v-col cols="12" md="12" sm="12" class="d-flex">
            <v-col cols="6" md="6" sm="6">
              <v-btn medium elevation="" color="primary" @click="backpage()"
                >Back</v-btn
              >
            </v-col>
            <v-col cols="6" md="6" sm="6">
              <v-btn medium elevation="" color="primary" @click="nextPage()"
                >Next</v-btn
              >
            </v-col>
          </v-col>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  data: () => ({
    showCounted: 4,
    content: '',
    nextValue: Number,
    user: [
      {
        id: 1,
        title: 'Saysana',
      },
      {
        id: 2,
        title: 'Binh',
      },
      {
        id: 3,
        title: 'Say',
      },
      {
        id: 4,
        title: 'Q',
      },
      {
        id: 5,
        title: 'a',
      },
      {
        id: 6,
        title: 'b',
      },
      {
        id: 7,
        title: 'c',
      },
      {
        id: 8,
        title: 'e',
      },
      {
        id: 9,
        title: 'e',
      },
      {
        id: 10,
        title: 'e',
      },
      {
        id: 11,
        title: 'e',
      },
      {
        id: 12,
        title: 'e',
      },
    ],
  }),
  computed: {
    defaultPage() {
      return (this.content = this.user.slice(0, this.showCounted))
    },
  },

  methods: {
    async nextPage() {
      this.content = this.user.slice(this.showCounted, this.showCounted + 4)
      console.log(this.content)
      this.showCounted = this.showCounted + 4
    },
    defaultData() {
      this.content = this.user.slice(0, this.showCounted)
    },
    async backpage() {
      const previous = -this.showCounted - 4
      let to = 4
      this.content = this.user.slice(previous, to)
    },
  },
  mounted() {
    this.defaultData()
  },
}
</script>

【问题讨论】:

  • 这里有几个问题。循环时不应使用v-forindexcomputed 不应该有副作用,实际上在 mounted 中进行相同的更改。然后,您的逻辑有点奇怪,请尝试使用2分进行分页。第一个是 0 或 xx 是页面 times the amount of elements you want to display。那么,终点应该是y,即x + the amount of elements you want to display。另外,请使用 ESlint 仔细检查任何可能的问题。

标签: javascript vue.js nuxt.js vuetify.js


【解决方案1】:

尝试如下 sn-p: 添加数据属性currentPage: 1,然后在下一页/后一页递增/递减当前页和slice(this.showCounted * this.currentPage, this.showCounted * this.currentPage + this.showCounted)/slice(this.showCounted * this.currentPage - this.showCounted, this.showCounted * this.currentPage)。如果您在最后一页/第一页,您也可以禁用/启用按钮。这样您就可以选择要显示的任意数量的项目,只需将 showCounted 设置为所需的值。

Vue.config.productionTip = false
Vue.config.devtools = false
new Vue({
  el: '#demo',
  vuetify: new Vuetify(),
  data(){
    return {
      showCounted: 4,
      currentPage: 1,
      content: '',
      nextValue: Number,
      user: [
        {id: 1, title: 'Saysana'}, {id: 2,title: 'Binh'}, {id: 3, title: 'Say'}, 
        {id: 4, title: 'Q'}, {id: 5, title: 'a'}, {id: 6, title: 'b'},
        {id: 7, title: 'c'}, {id: 8, title: 'e'}, {id: 9, title: 'e'}, 
        {id: 10, title: 'e'}, {id: 11, title: 'e'},{id: 12, title: 'e'},
      ],
      isDisabledN: false,
      isDisabledB: false
    }
  },
  methods: {
    defaultData() {
      this.content = this.user.slice(0, this.showCounted)
    },
    nextPage() {
      this.content = this.user.slice(this.showCounted * this.currentPage, this.showCounted * this.currentPage + this.showCounted)
      this.currentPage++
      this.checkPage()
    },
    backpage() {
      this.currentPage--
      this.content = this.user.slice(this.showCounted * this.currentPage - this.showCounted, this.showCounted * this.currentPage)
      this.checkPage()
    },
    checkPage() {
      if (this.showCounted  * this.currentPage >= this.user.length) {
        this.isDisabledN = true
        this.isDisabledB = false
      } else if(this.currentPage <= 1) {
        this.isDisabledN = false
        this.isDisabledB = true
      } else {
        this.isDisabledN = false
        this.isDisabledB = false
      }
    }
  },
  mounted() {
    this.defaultData()
    this.checkPage()
  },
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
  <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
<div id="demo">
  <v-app>
    <v-main>
      <v-container fluid>
        <v-row no-gutters>
          <v-col cols="12" md="12" sm="12">
            <v-card>
              <v-row no-gutters>
                <v-col cols="12" md="12" sm="12" class="">
                  <v-row no-gutters class="d-flex">
                    <v-col
                      cols="6"
                      md="6"
                      v-for="(item, index) in content"
                      :key="`item-${index}`"
                    >
                      <v-col cols="6">
                        <v-card-text>
                          <p>{{ item.id }}</p>
                          <p>my name is : {{ item.title }}</p>
                        </v-card-text></v-col
                      >
                    </v-col>
                  </v-row>
                </v-col>
              </v-row>

              <v-col cols="12" md="12" sm="12" class="d-flex">
                <v-col cols="6" md="6" sm="6">
                  <v-btn :disabled="isDisabledB" ref="back" medium elevation="" color="primary" @click="backpage()"
                    >Back</v-btn
                  >
                </v-col>
                <v-col cols="6" md="6" sm="6">
                  <v-btn :disabled="isDisabledN" ref="next" medium elevation="" color="secondary" @click="nextPage()"
                    >Next</v-btn
                  >
                </v-col>
              </v-col>
            </v-card>
          </v-col>
        </v-row>
      </v-container>
   </v-main>
 </v-app>
</div>

【讨论】:

  • 用户正在使用SFC文件,尽量不要将他与一些CDN导入文件和Vue.config设置混淆。而是与他分享一些代码框或纯代码。
  • 嘿伙计,谢谢你,会尝试这样做。您认为围绕导入和配置的一些 cmets 会清除混乱吗?干杯:)
  • 配置部分已经在 Nuxt 上完成,或者应该在其他地方完成,不属于这个问题。至于导入,由于项目可能会使用 Nuxt CLI 生成,因此无需讨论导入。我们确实假设配置从一开始就做好了。
  • 好的,明白了,很纯粹的vue.js,感谢gain,享受:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 1970-01-01
相关资源
最近更新 更多