老大,问个c#分页算法的问题
发布网友
发布时间:2022-09-25 21:12
我来回答
共1个回答
热心网友
时间:2023-09-21 12:52
好象没太多意义,那个currint应该就是10除2的结果,整个代码就大概是要显示从currentPage-5到currentPage+5之间这10页,就相当于
string output = "";
int num = 10;
if (totalPages < num) num = totalPages-1;//如果总页数不够10
int currint = num / 2;
int min = (currentPage >currint ? currentPage - currint : 1);//起始
int max = (currentPage + currint > totalPages ? totalPages : currentPage + currint);//结束
//为避免currentpage小于5时,以及避免最后几页显示不到10项,加上下面两行
if (max <= num) max = num+1;
if (max - min < num) min = max - num;
if (currentPage > totalPages) currentPage = totalPages;
for(int i=min;i<max;i++)
{
string css=(i==currentPage?"cpb":"pageLink");
output.AppendFormat("<a class='{4}' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, i, pageSize, i,css);
}
附上修改前后的图(最前的数字为当前页,为简单我直接把当前页用[]标出了)
热心网友
时间:2023-10-13 13:49
好象没太多意义,那个currint应该就是10除2的结果,整个代码就大概是要显示从currentPage-5到currentPage+5之间这10页,就相当于
string output = "";
int num = 10;
if (totalPages < num) num = totalPages-1;//如果总页数不够10
int currint = num / 2;
int min = (currentPage >currint ? currentPage - currint : 1);//起始
int max = (currentPage + currint > totalPages ? totalPages : currentPage + currint);//结束
//为避免currentpage小于5时,以及避免最后几页显示不到10项,加上下面两行
if (max <= num) max = num+1;
if (max - min < num) min = max - num;
if (currentPage > totalPages) currentPage = totalPages;
for(int i=min;i<max;i++)
{
string css=(i==currentPage?"cpb":"pageLink");
output.AppendFormat("<a class='{4}' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, i, pageSize, i,css);
}
附上修改前后的图(最前的数字为当前页,为简单我直接把当前页用[]标出了)