【问题标题】:Using filter with slice array methods not working使用带有切片数组方法的过滤器不起作用
【发布时间】:2021-11-14 08:01:02
【问题描述】:

我有一个搜索输入,用于过滤显示在表格中的一组用户。该表包括分页,因此我使用 .slice 让 10 个用户在每页的表上显示。出于某种原因,如果我在第 1 页,搜索过滤就会起作用(猜测是因为第 1 页上没有切片?);转到另一个页面后,过滤停止工作,这与 .slice 有关,因为如果我删除 .slice 代码,它就可以工作。任何想法为什么会这样?有些东西我在这里看不到..

tableData() {
      return this.users
        .filter(
          (user) =>
            user.firstName.toLowerCase().includes(this.search.toLowerCase()) ||
            user.lastName.toLowerCase().includes(this.search.toLowerCase()) ||
            user.email.toLowerCase().includes(this.search.toLowerCase()),
        )
        .slice(this.perPage * (this.currentPage - 1), this.perPage * this.currentPage);
    },

【问题讨论】:

    标签: javascript arrays vue.js frontend


    【解决方案1】:

    这是因为您的结果现在在首页或之前的某个位置。

    您正在对结果进行切片。

    【讨论】:

    • 如果是这种情况,我有什么办法可以解决这个问题?
    • 搜索时,表格正在调整大小。也许您应该在用户输入内容时将他带回第一页。
    • 谢谢我试试
    • 那行得通。谢谢
    猜你喜欢
    • 2021-12-18
    • 2015-08-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2017-01-06
    • 2018-02-14
    相关资源
    最近更新 更多