使用MVP模式实现分页和排序的最佳方法 [英] Best way to implement paging and sorting using MVP pattern

查看:136
本文介绍了使用MVP模式实现分页和排序的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在Google花了几个小时,以寻找有关如何以优化方式在gridview中实现分页/排序的想法.

数据库方面:

Hi Guys,

I spent couple of hours in google looking for idea on how to implement paging/sorting in gridview in an optimize manner.

Database side:

make a query that will only get the number of records for display. (i am clear with this)



(服务器代码):



(Server Code):

Use ObjectContainerDatasource and mapped it to your gridview.



我是否直接将其映射到gridview会不会绕过MVP的想法?
有关如何实现此目标的任何提示?

感谢您的帮助.
谢谢.
jepoy



Question if i map it directly to the gridview isn''t it bypassing the idea of MVP?
Any tips on how to achieve this?

appreciate any help.
thanks.
jepoy

推荐答案

如果所有数据都适合可用内存,则可以检索所有需要显示的数据,然后仅提取适合的项目在页面中.伪代码:

0)检索数据

1)计算您拥有的页数-Math.Min(1, Math.Floor(records_retrieved / records_per_page)))

2)提取项目.我可能会写一个扩展方法来做到这一点,然后就可以在任何适当类型的集合上使用它.该方法的实质如下所示:


If all of your data will fit in available memory, you could retrieve all of the data you need to display, and then extract only the items that will fit in a page. Psuedo code:

0) retrieve data

1) Calculate number of pages you have - Math.Min(1, Math.Floor(records_retrieved / records_per_page)))

2) Extract the items. I''d probably write an extension method to do this, and then you can use it on any apprpopriately typed collection. The guts of the method would look something like this:


List<T> result = new List<T>();
int skipItems = recsPerPage * (pageNumber - 1);
if (list.Count > skipItems)
{
    list.Skip(skipItems);
    result = list.Take(Math.Min(list.Count - skipItems, 100);
}
return result;



编辑====================

这是一些有效的代码:



EDIT =====================

Here''s some code that works:

List<string> myStrings = new List<string>();
myStrings.Add("1");
myStrings.Add("2");
myStrings.Add("3");
myStrings.Add("4");
myStrings.Add("5");

// we want the 3rd page of data (with 1 item per page)
List<string> result = myStrings.GetPageOfData(3, 1);</string>



扩展方法如下:



Here''s the extension method:

public static List<T> GetPageOfData<T>(this List<T> list, int pageNumber, int recsPerPage)
{
    List<T> results = new List<T>();
    int skipItems = recsPerPage * (pageNumber - 1);
    int itemsToReturn = Math.Min(list.Count - skipItems, recsPerPage) - 1;
    if (list.Count > skipItems)
    {
        for (int i = skipItems; i <= (skipItems + itemsToReturn); i++)
        {
            results.Add(list[i]);
        }
    } 
    return results;
}


这篇关于使用MVP模式实现分页和排序的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆