http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 6533|回复: 1

[Asp.Net] [MVC] WebGrid的方法介绍二

[复制链接]
发表于 2013-5-21 14:17:41 | 显示全部楼层 |阅读模式
WebGrid 给我们开发人员提供了很好的分页和排序功能,优点很明显。但看完下面的例子后会发现一个问题:不管获取每一页都会去取出所有的数据,假如说其中有100W条数据,在用foreach去循环的话,就会造成crash,所以需要我们更好地派生WebGrid
创建派生 WebGrid其中用...代替的大家可以通过F12到WebGrid class中查看具体参数,比较多,所以这边就用...代替了
  1. public class WebGrid<T> : WebGrid
  2.   {
  3.     public WebGrid(
  4.       IEnumerable<T> source = null,
  5.       ...
  6.           parameter list omitted for brevity)
  7.     : base(
  8.       source.SafeCast<object>(),
  9.       ...
  10.           parameter list omitted for brevity)
  11.     { }
  12.   public WebGridColumn Column(
  13.               string columnName = null,
  14.               string header = null,
  15.               Func<T, object> format = null,
  16.               string style = null,
  17.               bool canSort = true)
  18.     {
  19.       Func<dynamic, object> wrappedFormat = null;
  20.       if (format != null)
  21.       {
  22.         wrappedFormat = o => format((T)o.Value);
  23.       }
  24.       WebGridColumn column = base.Column(
  25.                     columnName, header,
  26.                     wrappedFormat, style, canSort);
  27.       return column;
  28.     }
  29.     public WebGrid<T> Bind(
  30.             IEnumerable<T> source,
  31.             IEnumerable<string> columnNames = null,
  32.             bool autoSortAndPage = true,
  33.             int rowCount = -1)
  34.     {
  35.       base.Bind(
  36.            source.SafeCast<object>(),
  37.            columnNames,
  38.            autoSortAndPage,
  39.            rowCount);
  40.       return this;
  41.     }
  42.   }

  43.   public static class WebGridExtensions
  44.   {
  45.     public static WebGrid<T> Grid<T>(
  46.              this HtmlHelper htmlHelper,
  47.              ...
  48.           parameter list omitted for brevity)
  49.     {
  50.       return new WebGrid<T>(
  51.         source,
  52.         ...
  53.           parameter list omitted for brevity);
  54.     }
  55.   }
复制代码
这样做有什么好处呢?          通过实现这个新的 WebGrid<T>,我添加了一个新的 Column 方法,该方法以 Func<T, object> 作为 format 参数,这意味着在调用扩展方法时不必再进行转换。         
                  通过这种 Grid 扩展方法,您能够利用编译器针对范型参数的类型推断功能。          因此,本例中我们只需要编写 Html.Grid(Model),而不必编写新的 WebGrid<Object>(Model)。          无论采用哪种方式,返回的类型都是 WebGrid<Object>。例如:
  1.     public class PagedObjsModel
  2.     {
  3.         /// <summary>
  4.         /// Model
  5.         /// </summary>
  6.         public IEnumerable<object> ModelValue { get; set; }

  7.         /// <summary>
  8.         /// 每页个数
  9.         /// </summary>
  10.         public int PageSize { get; set; }

  11.         /// <summary>
  12.         /// 页码
  13.         /// </summary>
  14.         public int PageNumber { get; set; }

  15.         /// <summary>
  16.         /// 总数
  17.         /// </summary>
  18.         public int TotalRows { get; set; }
  19.     }
复制代码
实现 WebGrid 服务器端分页的第一步是限制从数据源检索的数据量。          为此,需要知道请求的是哪一页数据,以便检索正确的数据页。          WebGrid 在呈现分页链接时,会重复使用页面的 URL,并在页码中附加一个查询字符串参数,例如 http://localhost:27617/Product/DefaultPagingAndSorting?page=3(该查询字符串参数的名称可通过帮助程序参数进行配置,这在支持同一页面中多个网格的分页时非常有用)。          也就是说,您可以在自己的操作方法中采用一个名为 page 的参数,然后使用查询字符串值填充该参数。        
                  如果只是通过修改现有代码向 WebGrid 传递单页数据,则 WebGrid 只会看到单页数据。          由于它不知道还有别的页面,因而不再呈现分页器控件。          幸运的是,WebGrid 还有一种名为 Bind 的方法,可用来指定数据。          Bind 不仅能够接受数据,而且有一个表示总行数的参数,从而据此计算页数。          为了使用此方法,需要更新 List 操作以检索更多信息并将其传入视图,如图 7 所示        
Controll代码如下:
  1. public ActionResult List(int page = 1)
  2. {
  3.   const int pageSize = 5;

  4.   int totalRecords;
  5.   IEnumerable<Product> products = productService.GetProducts(
  6.     out totalRecords, pageSize:pageSize, pageIndex:page-1);
  7.             
  8.   PagedProductsModel model = new PagedProductsModel
  9.                                  {
  10.                                    PageSize= pageSize,
  11.                                    PageNumber = page,
  12.                                    Products = products,
  13.                                    TotalRows = totalRecords
  14.                                  };
  15.   return View(model);
  16. }
复制代码
利用这些附加信息,即可更新视图以使用 WebGrid 的 Bind 方法。          通过调用 Bind 可提供要呈现的数据和总行数,并将 autoSortAndPage 参数设置为 false。          autoSortAndPage 参数告知 WebGrid 不需要应用分页,因为这由 List 方法负责。          对此可用下面代码说明
  1.       <div>
  2. @{
  3.   var grid = new WebGrid<Product>(null, rowsPerPage: Model.PageSize,
  4.     defaultSort:"Name");
  5.   grid.Bind(Model.Products, rowCount: Model.TotalRows, autoSortAndPage: false);
  6. }
  7. @grid.GetHtml(columns: grid.Columns(
  8. grid.Column("Name", format: @<text>@Html.ActionLink(item.Name,
  9.    "Details", "Product", new { id = item.ProductId }, null)</text>),
  10.   grid.Column("ListPrice", header: "List Price",
  11.     format: @<text>@item.ListPrice.ToString("0.00")</text>)
  12.   )
  13. )

  14. </div>
复制代码
当然在上述代码中,您也可以指定一些样式:可以先将columns值赋值好,然后通过下面代码填充GetHtml
  1. @grid.GetHtml(
  2.                 tableStyle: "ContactTB",
  3.                 headerStyle: "ContactHD",
  4.                 rowStyle: "Content",
  5.                 firstText: "首页",
  6.                 previousText: "上一页",
  7.                 nextText: "下一页",
  8.                 lastText: "末页",
  9.                 mode: WebGridPagerModes.All, //这段代码就是控制分页显示的样式,选择ALL,都兼容。
  10.                 columns: gridColumns
复制代码
Done, 这样分页就不会每次取出所有的数据了,而只会每次取出每页的数据。
由于关闭了 autoSortAndPage,排序功能遭到破坏。          WebGrid 利用查询字符串参数来传递排序列和方向,但我们已命令它不执行排序,所以WebGrid的方法介绍三介绍排序功能!





1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-5-21 15:01:04 | 显示全部楼层
强烈支持楼主ing……
楼主在使用代码控件时,选择一下语言,比如你的文章中选择C#会更好看一些
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-17 12:12

© 2014-2021

快速回复 返回顶部 返回列表