Pagination在搜索框清空后不会返回到第一页

huangapple go评论103阅读模式
英文:

Pagination doesn't set back to first page after search box is cleared

问题

我正在使用Swift进行iOS应用程序开发,并遇到了分页功能的问题。

这个想法非常简单。当用户输入查询时,搜索功能应返回所有可能的结果,并使用分页器每页显示10行,如果结果超过10行。用户可以通过点击分页器浏览到每个结果页面。然而,当用户清除搜索框或由用户清空搜索框时,它应该始终返回/设置为第一个结果页面当前分页器应等于0),并准备好接受下一个输入。

目前发生的问题是,在使用分页器浏览以前查询的结果页面后,当用户清除搜索框以进行新查询时,它永远不会返回到第一页。结果,新查询搜索结果始终从以前浏览的页面号的当前页面号开始显示,导致忽略了第一页的结果,用户必须逐页使用分页器以返回查看第一页的结果。

当前的代码如下:

import SwiftUI

class DictionaryViewModel: ObservableObject {
    @Published var filteredWordPairs: [WordPair] = []
    @Published var searchText = "" {
        didSet {
            filterWordPairs()
        }
    }
    
    private var allWordPairs: [WordPair] = DataLoader.loadCSVFile()
    private var currentPage = 0
    private let pageSize = 10
    
    func wordPairsForPage(_ pageIndex: Int, pageSize: Int) -> ArraySlice<WordPair> {
        let startIndex = pageIndex * pageSize
        let endIndex = min(startIndex + pageSize, filteredWordPairs.count)
        
        if startIndex >= endIndex {
            return []
        }
        
        return filteredWordPairs[startIndex..<endIndex]
    }
    
    var canGoToNextPage: Bool {
        return (currentPage + 1) * pageSize < filteredWordPairs.count
    }
    
    func gotoPreviousPage() {
        currentPage = max(currentPage - 1, 0)
    }
    
    func gotoNextPage() {
        if canGoToNextPage {
            currentPage += 1
        }
    }
    
    private func filterWordPairs() {
        if searchText.isEmpty {
            filteredWordPairs = []
        } else {
            let lowercaseSearchText = searchText.lowercased()
            filteredWordPairs = allWordPairs.filter { wordPair in
                let lowercasedWord1 = wordPair.Word1.lowercased()
                let lowercasedWord2 = wordPair.Word2.lowercased()
                
                return lowercasedWord1.contains(lowercaseSearchText) ||
                    lowercasedWord2.contains(lowercaseSearchText) ||
                    lowercasedWord1.range(of: lowercaseSearchText) != nil ||
                    lowercasedWord2.range(of: lowercaseSearchText) != nil
            }
        }
        currentPage = 0 // 当搜索文本发生更改时重置currentPage
    }
}

希望这有助于解决你的问题。如果你需要进一步的帮助,请随时提出。

英文:

I'm using Swift for iOS app development and encountering a problem with pagination function.

The idea is quite straightforward. As the query is entered, the search function should return all possible results, and list them 10 rows per page using paginator if the results are more than 10. The user may browse to each result page by clicking paginator. However, it should always goes back/set to the first result page (current paginator should equal to 0) when the search box is cleared or emptied by the user and get ready for next input.

The problem currently occurring is that after browsing the result page of previous query with paginator, it never set back to first page when the search box is cleared for new query. In result, the new query search results started showing from current page number of previous browsed page number, which ended up the results from page 1 are overlooked and the user has to use paginator one by one to go back to see the results in first page.

Current code is as follows:

import SwiftUI

class DictionaryViewModel: ObservableObject {
@Published var filteredWordPairs: [WordPair] = []
@Published var searchText = &quot;&quot; {
    didSet {
        filterWordPairs()
    }
}

private var allWordPairs: [WordPair] = DataLoader.loadCSVFile()
private var currentPage = 0
private let pageSize = 10

func wordPairsForPage(_ pageIndex: Int, pageSize: Int) -&gt; ArraySlice&lt;WordPair&gt; {
    let startIndex = pageIndex * pageSize
    let endIndex = min(startIndex + pageSize, filteredWordPairs.count)
    
    if startIndex &gt;= endIndex {
        return []
    }
    
    return filteredWordPairs[startIndex..&lt;endIndex]
}

var canGoToNextPage: Bool {
    return (currentPage + 1) * pageSize &lt; filteredWordPairs.count
}

func gotoPreviousPage() {
    currentPage = max(currentPage - 1, 0)
}

func gotoNextPage() {
    if canGoToNextPage {
        currentPage += 1
    }
}

private func filterWordPairs() {
    if searchText.isEmpty {
        filteredWordPairs = []
    } else {
        let lowercaseSearchText = searchText.lowercased()
        filteredWordPairs = allWordPairs.filter { wordPair in
            let lowercasedWord1 = wordPair.Word1.lowercased()
            let lowercasedWord2 = wordPair.Word2.lowercased()
            
            return lowercasedWord1 .contains(lowercaseSearchText) ||
            lowercasedWord2.contains(lowercaseSearchText) ||
            lowercasedWord1.range(of: lowercaseSearchText) != nil ||
            lowercasedWord2.range(of: lowercaseSearchText) != nil
        }
    }
    currentPage = 0 // Reset currentPage when search text changes
    
}

}

答案1

得分: 1

当搜索文本更改时,您正确地将currentPage重置为0,但当用户使用分页器浏览页面时,您没有更新currentPage。我认为您需要在searchText.isEmpty时将currentPage重置为0。以下是更新后的代码:

@Published private var currentPage = 0

private func filterWordPairs() {
    if searchText.isEmpty {
        filteredWordPairs = []
        currentPage = 0 // 当搜索文本被清除时重置currentPage
    } else {
        let lowercaseSearchText = searchText.lowercased()
        filteredWordPairs = allWordPairs.filter { wordPair in
            let lowercasedWord1 = wordPair.Word1.lowercased()
            let lowercasedWord2 = wordPair.Word2.lowercased()

            return lowercasedWord1.contains(lowercaseSearchText) ||
                lowercasedWord2.contains(lowercaseSearchText) ||
                lowercasedWord1.range(of: lowercaseSearchText) != nil ||
                lowercasedWord2.range(of: lowercaseSearchText) != nil
        }
        currentPage = 0 // 当搜索文本更改时重置currentPage
    }
}
英文:

When the search text changes, you correctly reset the currentPage to 0, but you're not updating the currentPage when the user navigates through the pages using the paginator. I think you need to reset currentPage to 0 when searchText.isEmpty. Here's updated code

    @Published private var currentPage = 0

    private func filterWordPairs() {
        if searchText.isEmpty {
            filteredWordPairs = []
            currentPage = 0 // Reset currentPage when search text is cleared
        } else {
            let lowercaseSearchText = searchText.lowercased()
            filteredWordPairs = allWordPairs.filter { wordPair in
                let lowercasedWord1 = wordPair.Word1.lowercased()
                let lowercasedWord2 = wordPair.Word2.lowercased()

                return lowercasedWord1.contains(lowercaseSearchText) ||
                    lowercasedWord2.contains(lowercaseSearchText) ||
                    lowercasedWord1.range(of: lowercaseSearchText) != nil ||
                    lowercasedWord2.range(of: lowercaseSearchText) != nil
            }
            currentPage = 0 // Reset currentPage when search text changes
        }
    }

huangapple
  • 本文由 发表于 2023年8月9日 12:34:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864605-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定