public JsonResult GetArchivesJson(jqDataTableParameter tableParam) { #region 1.0 场景一 ////1. 获取所有文章 //List<Article> DataSource = articleService.GetDataListBy(a => true, a => a.Id); ////2. 构造aaData //var data = DataSource.Select(a => new object[]{ // a.Id, // a.Title+ " ("+a.SubTime.ToString()+")", // (categoryService.GetDataListBy(c=>c.Id==a.CategoryId)[0]).Name, // a.ViewCount, // commentService.GetDataListBy(c=>c.CmtArtId==a.Id).Count, // a.Digg, // a.Status==1?"正常":"删除" //}); ////3. 返回json,aaData是一个数组,数组里面还是字符串数组 //return Json(new //{ // sEcho = 1, // iTotalRecords = DataSource.Count, // iTotalDisplayRecords = data.Count(), // aaData = data //}, JsonRequestBehavior.AllowGet); #endregion } public JsonResult GetArchivesJson(jqDataTableParameter tableParam)
场景二:服务端处理分页后数据,客户端呈现,此时为true,
服务端代码:
public JsonResult GetArchivesJson(jqDataTableParameter tableParam) { #region 2.0 场景二 //客户端需要"bServerSide": true, 用mDataProp绑定字段,obj.aData.Id获取字段(.属性) //0.0 全部数据 List<Article> DataSource = articleService.GetDataListBy(a => true); //DataSource = DataSource.OrderByDescending(a => a.SubTime).ToList(); //1.0 首先获取datatable提交过来的参数 string echo = tableParam.sEcho; //用于客户端自己的校验 int dataStart = tableParam.iDisplayStart;//要请求的该页第一条数据的序号 int pageSize = tableParam.iDisplayLength == -1 ? DataSource.Count : tableParam.iDisplayLength;//每页容量(=-1表示取全部数据) string search = tableParam.sSearch; //2.0 根据参数(起始序号、每页容量、参训参数)查询数据 if (!String.IsNullOrEmpty(search)) { var data = DataSource.Where(a => a.Title.Contains(search) || a.Keywords.Contains(search) || a.Contents.Contains(search)) .Skip<Article>(dataStart) .Take(pageSize) .Select(a => new { Id = a.Id, Title = a.Title + " (" + a.SubTime.ToString() + ")", CategoryName = a.Category.Name, ViewCount = a.ViewCount, CommentCount = commentService.GetDataListBy(c => c.CmtArtId == a.Id).Count, Digg = a.Digg, Status = a.Status }).ToList(); //3.0 构造datatable所需要的数据json对象...aaData里面应是一个二维数组:即里面是一个数组[["","",""],[],[],[]] return Json(new { sEcho = echo, iTotalRecords = DataSource.Count(), iTotalDisplayRecords = DataSource.Count(), aaData = data }, JsonRequestBehavior.AllowGet); } else { var data = DataSource.Skip<Article>(dataStart) .Take(pageSize) .Select(a => new { Id = a.Id, Title = a.Title + " (" + a.SubTime.ToString() + ")", CategoryName = a.Category.Name, ViewCount = a.ViewCount, CommentCount = commentService.GetDataListBy(c => c.CmtArtId == a.Id).Count, Digg = a.Digg, Status = a.Status }).ToList(); //3.0 构造datatable所需要的数据json对象...aaData里面应是一个二维数组:即里面是一个数组[["","",""],[],[],[]] return Json(new { sEcho = echo, iTotalRecords = DataSource.Count(), iTotalDisplayRecords = DataSource.Count(), aaData = data }, JsonRequestBehavior.AllowGet); } #endregion } public JsonResult GetArchivesJson(jqDataTableParameter tableParam)
其中dataTables发送的参数被分装在jqDataTableParameter.cs中:
/// <summary> /// 在服务器端,可以通过以下请求参数来获得当前客户端的操作信息 /// jquery $('selector').datatable()插件 参数model /// </summary> public class jqDataTableParameter { /// <summary> /// 1.0 DataTable用来生成的信息 /// </summary> public string sEcho { get; set; } /// <summary> /// 2.0分页起始索引 /// </summary> public int iDisplayStart { get; set; } /// <summary> /// 3.0每页显示的数量 /// </summary> public int iDisplayLength { get; set; } /// <summary> /// 4.0搜索字段 /// </summary> public string sSearch { get; set; } /// <summary> /// 5.0列数 /// </summary> public int iColumns { get; set; } /// <summary> /// 6.0排序列的数量 /// </summary> public int iSortingCols { get; set; } /// <summary> /// 7.0逗号分割所有的列 /// </summary> public string sColumns { get; set; } } public class jqDataTableParameter
后台效果展示:
以上就是对datatable插件的使用说明。
以上所述是小编给大家介绍的利用ASP.NET MVC和Bootstrap快速搭建个人博客之后台dataTable数据列表,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章: