bootstrap table 服务器分页

时间:2023-03-10 01:05:36
bootstrap table 服务器分页

1.封装MODEL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MODEL.FormatModel
{
public class TableModel<T>
{
public T rows { get; set; }
public int total{get;set;}

}
}

2.方法

public ActionResult GetContentList()
{
int currentPage = HttpContext.Request.Params["offset"] == null ? 1 : int.Parse(HttpContext.Request.Params["limit"]);
// 每页行数
int showCount = HttpContext.Request.Params["limit"] == null ? 10 : int.Parse(HttpContext.Request.Params["limit"]);
if (currentPage != 0)
{// 获取页数
currentPage = currentPage / showCount;
}
currentPage += 1;

List<MODEL.Content> allModelList = OperateContext.Current.BLLSession.IContentBLL.Get(c => 1 == 1).ToList().OrderByDescending(c => c.PostDate).ToList();
int rows = allModelList.Count / showCount;
int total = allModelList.Count;
List<MODEL.Content> modelList = allModelList.Take(showCount * currentPage).Skip(showCount * (currentPage - 1)).ToList();

List<MODEL.Content> contentList = new List<MODEL.Content>();
MODEL.Content content = null;
for (int i = 0; i < modelList.Count; i++)
{
content = modelList[i].ToPo();
if (!string.IsNullOrWhiteSpace(content.Catalog))
{

string cId = content.Catalog;
MODEL.Catalog catalogModel = OperateContext.Current.BLLSession.ICataLogBLL.Get(c => c.ID == cId).FirstOrDefault();
if (catalogModel != null)
{
content.Catalog = catalogModel.Name;
}
else
{
content.Catalog = "";
}
}
else
{
content.Catalog = "";
}
contentList.Add(content);

}
//System.Text.StringBuilder sb = new StringBuilder();
//System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();

//jss.Serialize(contentList, sb);
//string contentListJsonStr = "\"rows\":\"" + sb.ToString() + "\",\"total\":\"" + total + "\"";
MODEL.FormatModel.TableModel<List<MODEL.Content>> tableModel = new MODEL.FormatModel.TableModel<List<MODEL.Content>>();
tableModel.rows = contentList;
tableModel.total = total;
return Json(tableModel, JsonRequestBehavior.AllowGet);
}

3.前端

<table data-toggle="table" id="table" data-toolbar="#toolbar" data-url="/Admin/Site/GetContentList" data-show-refresh="true" data-show-toggle="true" data-show-columns="true" data-search="true" data-pagination="true" data-side-pagination="server" data-page-list="[5, 10, 20, 50, 100, 200]" data-sort-name="name" data-sort-order="desc">
<thead>
<tr>
<th data-field="state" data-checkbox="true" class="col-sm-1"></th>
<th data-field="ID" data-visible="false">ID</th>
<th data-field="Title" data-sortable="true" class="col-sm-3">标题</th>
<th data-field="Abbreviations" data-sortable="true" class="col-sm-3">缩略语</th>
<th data-field="Catalog" data-sortable="true" class="col-sm-1">栏目</th>
<th data-field="IsPublish" data-sortable="true" class="col-sm-1">是否发布</th>
<th data-field="Poster" data-sortable="true" class="col-sm-1">发布人</th>
<th data-field="PostDate" data-sortable="true" class="col-sm-1">发布时间</th>
<th data-field="Operation" data-formatter="actionFormatter" data-events="actionEvents" class="col-sm-1">操作</th>
</tr>
</thead>
</table>