在Asp.net开发中的GridView功能比较强大,可以方便的进行分页等操作。
在MonoRail中也提供了方便的分页功能,只要使用
PaginationHelper就可以了。当然,你如果是在数据库端进行分页的话就不是这篇笔记考虑的范围了。
Controller代码:

Model
public class User

{
private int id;
private string name, email, password, confirmation;

public User()

{
}
public User(string name, string email)

{
this.name = name;
this.email = email;
}

public int Id

{

get
{ return id; }

set
{ id = value; }
}

[ValidateNonEmpty("不能为空", FriendlyName="姓名")]
public string Name

{

get
{ return name; }

set
{ name = value; }
}

[ValidateNonEmpty, ValidateEmail]
public string Email

{

get
{ return email; }

set
{ email = value; }
}

[ValidateNonEmpty]
public string Password

{

get
{ return password; }

set
{ password = value; }
}

[ValidateSameAs("Password")]
public string Confirmation

{

get
{ return confirmation; }

set
{ confirmation = value; }
}
}
public void List()

{
IList list = new ArrayList();

for (int i = 1; i < 36; i++)

{
User user = new User();
user.Id = i;
user.Name = "永春_" + i;
user.Email = "GSpring_" + i + "@hotmail.com";
list.Add(user);
}

PropertyBag.Add("list1", PaginationHelper.CreatePagination(this, list, 10));
}
vm中代码:
#foreach($item in $list1)
#beforeall
<table>
<tr>
<th>ID</th>
<th>姓名</th>
<th>邮件地址</th>
</tr>
#before
<tr
#odd
Style='color:gray'>
#even
Style='color:red'>

#each
<td>$item.Id</td>
<td>$item.Name</td>
<td>$item.Email</td>
#after
</tr>

#afterall
</table>

#nodata
没有数据
#end
<p>
当前显示 $list1.FirstItem - $list1.LastItem 条 共 $list1.TotalItems 条
</p>

<p>
#if ($list1.HasFirst)
$PaginationHelper.CreatePageLink( 1, "第一页" )
#end
#if ($list1.HasPrevious)
$PaginationHelper.CreatePageLink( $list1.PreviousIndex, "前一页" )
#end
#if ($list1.HasNext)
$PaginationHelper.CreatePageLink( $list1.NextIndex, "后一页" )
#end
#if ($list1.HasLast)
$PaginationHelper.CreatePageLink( $list1.LastIndex, "末页" )
#end
<p>
#foreach ( $item in [1..$list1.LastIndex] )
$PaginationHelper.CreatePageLink( $item, $item.toString() )
#end


所有的分页相关功能几乎都在PaginationHelper中实现了,我们只要调用就可以了。
最后的效果如下:

提供了两种方式的分页,按页码和按前后页的方式,当然也可以定制自己需要的分页方式,代码很简单。
posted @ 2007-11-02 15:02
永春 阅读(857)
评论(6) 编辑 收藏 所属分类:
MonoRail