苏飞论坛

标题: WebGrid的方法介绍一 [打印本页]

作者: 一级菜鸟    时间: 2013-5-21 13:57
标题: WebGrid的方法介绍一

WebGrid 给我们开发人员提供了很好的分页和排序功能,优点很明显。但看完下面的例子后会发现一个问题:不管获取每一页都会去取出所有的数据,假如说其中有100W条数据,在用foreach去循环的话,就会造成crash,WebGrid的方法介绍二中我会具体说明如何去更好地派生WebGrid。

WebGrid的方法签名

public WebGrid(

IEnumerable<dynamic> source, 设置数据源

IEnumerable<string> columnNames = null, 要显示的列名

string defaultSort = null, 默认排序的字段

int rowsPerPage = 10, 每页的行数

bool canPage = true, 是否允许分页

bool canSort = true, 是否允许排序

string ajaxUpdateContainerId = null,

string ajaxUpdateCallback = null,

string fieldNamePrefix = null,字段名的前缀

string pageFieldName = null,

string selectionFieldName = null,

string sortFieldName = null,

string sortDirectionFieldName = null);
代码使用如下:

  1. List<CMS5_Razor.Models.Test> list = new List<CMS5_Razor.Models.Test>();
  2.     for (int i = 0; i < 210; i++) {
  3.         list.Add(new CMS5_Razor.Models.Test() { Name = "name" + i, Order = i, Content = "content" + i });
  4.     }
  5.     var grid = new WebGrid(source: list, rowsPerPage: 5, canPage: true
  6.         , selectionFieldName: "Name"
  7.         , fieldNamePrefix: "字段名的前缀"
  8.         );
复制代码
WebGrid.GetHtml函数签名

public IHtmlString GetHtml(

string tableStyle = null,表格样式

string headerStyle = null,头部

string footerStyle = null, 底部

string rowStyle = null, 每一行的样式

string alternatingRowStyle = null, 交替行

string selectedRowStyle = null, 被选中

string caption = null,标题

bool displayHeader = true, 是否显示头部

bool fillEmptyRows = false, 是否填充空行

string emptyRowCellValue = null, 空行里每个字段显示的值

IEnumerable<WebGridColumn> columns = null, 在这里设置各列

IEnumerable<string> exclusions = null, 不显示的字段

WebGridPagerModes mode =枚举值,一般情况选择WebGridPagerModes.All

string firstText = null, 对应第一页链接显示的文字

string previousText = null, 对应上一页链接显示的文字

string nextText = null, 对应下一页链接显示的文字

string lastText = null, 对应最后一页链接显示的文字

int numericLinksCount = 5,数字选择项的数目

object htmlAttributes = null  ~beta版本源码中没查到,暂时不知道如何使用,

);

Column的方法签名

public WebGridColumn Column(string columnName = null,

string header = null,列头的名字

Func<dynamic, object> format = null,  自定义的输出,非常实用

string style = null, 样式

bool canSort = true);是否允许排序
代码如下:
  1. grid.GetHtml(
  2. columns: grid.Columns(
  3.       grid.Column("Name", "名字", style: "mynameStyle"),
  4.                  grid.Column("Content", "Content", style: "mynameStyle"),
  5.       grid.Column("Order", "顺序", format: @<b><i>@item.Order</i></b>, canSort: false)
  6.                        ), tableStyle: "grid", headerStyle: "headerStyle1", selectedRowStyle: "selectedRowStyle",
  7.                   caption: "我是标题", displayHeader: !IsPost,
  8.                   fillEmptyRows: !IsPost, emptyRowCellValue: "这行没东西",
  9.                   mode: WebGridPagerModes.All, firstText: "第一页",
  10.                   previousText: "上一页", nextText: "下一页", lastText: "最后一页",
  11.                 numericLinksCount: 15
  12. )
复制代码

作者: 一级菜鸟    时间: 2013-5-21 14:01
If U have don't understand, U can leave a message, I will answer in a timely manner
作者: yangying    时间: 2013-5-21 14:59
支持 强烈支持楼主ing……




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4