简体   繁体   English

Razor 中的分页和过滤组合

[英]pagination & filtering combo in Razor Pages

There is issue that is troubling me for 2 days.有一个问题困扰了我 2 天。 Can't figure it out, so maybe another pair or few pair of fresh eyes can point me in right direction.想不通,所以也许另一双或几双新鲜的眼睛可以为我指明正确的方向。

I have a repository for CRUD operations我有一个用于 CRUD 操作的存储库

    public async Task<int> GetCount()
    {
        var data = await GetData();
        return data.Count;
    }

    public async Task<List<Item>> GetPaginatedResult(int currentPage, int pageSize = 20)
    {
        var data = await GetData();
        return data.OrderBy(d => d.Day).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
    }

    public async Task<List<Item>> GetData()
    {
        return await _db.Set<Item>().ToListAsync();
    }

Item is a simple model class, I have no problems here.项目是一个简单的 model class,我这里没有问题。 Pagination works perfectly until I try to combine pagination with filtering.分页工作完美,直到我尝试将分页与过滤结合起来。

I have one dropdown that offer year number (2022, 2021) and another with month (int value).我有一个下拉菜单提供年份编号(2022、2021),另一个提供月份(int 值)。

This is part of code that causing problems:这是导致问题的代码的一部分:

        var tempData = await repository!.GetPaginatedResult(CurrentPage, PageSize);
        Items = tempData;
        //Items = tempData.Where(s => s.Year == SearchYear && s.Month == Month);
        var sumList = await repository!.GetData();
        //Count = sumList.Where(s => s.Year == SearchYear && s.Month == Month).OrderBy(x => x.Day).Count();
        Count = sumList.OrderBy(x => x.Day).Count();
        //SumValues = sumList.Where(s => s.Year == SearchYear && s.Month == Month).OrderBy(x => x.Day).Sum(s => s.Value);
        SumValues = sumList.Sum(s => s.Value);

This way pagination works, before filtering is applied.在应用过滤之前,这种方式分页工作。

When I use this line instead:当我改用这条线时:

Items = tempData.Where(s => s.Year == SearchYear && s.Month == Month);

there is no errors but strange behaviour.没有错误,但有奇怪的行为。 When use commented lines instead current ones I get for example on first page only 9 records shown instead 20 what is PageSize, also after first click on Next pagination button, like Items list is empty.当使用注释行而不是当前行时,例如,我在第一页上只显示 9 条记录而不是 20 条什么是 PageSize,也是在第一次单击 Next 分页按钮后,例如 Items 列表为空。

SumValues, what is just a text showing Sum of filtered records is working fine, everything else is mess. SumValues,只是显示过滤记录总和的文本工作正常,其他一切都是一团糟。

View is classic for populating table:视图是填充表的经典:

     <tbody>
        @foreach(var obj in Model.Items!)
        {
            <tr>
                <td>@obj.Day</td>
                <td>@obj.Month</td>
                <td>@obj.Year</td>
                <td>@obj.ItemName</td>
                <td>@obj.Destination</td>
                <td class = "text-end">@string.Format("{0:#,#}", obj.Quantity)</td>
                <td class = "text-end">@string.Format("{0:#,#.00}", obj.Value)</td>
                <td class = "text-center">Edit Delete</td>
            </tr>
        }
    </tbody>

When you are dealing with both filtering and pagination, you need to be careful with order of operations.当您同时处理过滤和分页时,您需要注意操作顺序。 Only after filtering you will know how many records you want to display anyway.只有在过滤之后,您才会知道无论如何要显示多少条记录。 It seems like you first do pagination and then filtering, so fe on first page 9 out of 20 records meet your criteria and on second page 0 out of N do.似乎您首先进行分页然后进行过滤,因此 fe 在 20 条记录中的第 9 页上符合您的条件,在N条中的第 0 页上符合您的条件。 I recommend to reverse the order of operations.我建议颠倒操作顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM