.net MVC模式下easyui datagrid控件分页

此参照一位仁兄代码,稍作修改

视图代码:

<div id="tab" class="easyui-tabs" data-options="tools:'#tab-tools'" style="width:800px;height:400px">
<div title="****" style="padding:0px;">
<div class="easyui-tabs" data-options="fit:true,plain:true" id="easyuitab">
<div title="****" style="padding:0px;">
<table id="grid" class="easyui-datagrid" style="height:600px"
data-options="iconCls: 'icon-ok',
pageSize: 20,//默认显示行数
animate: true,
collapsible: true,
url:'/Json/Dt',
singleSelect:true,
method: 'get',
idField: 'id',
treeField: 'name',
pagination:true"//必须设置

>
<thead>
<tr>
<th data-options="field:'ID',width:80" hidden="hidden">ID</th>
<th data-options="field:'**',width:80">**</th>
<th data-options="field:'**',width:80">**</th>
<th data-options="field:'**',width:80">**</th>
<th data-options="field:'**',width:80">**</th>
<th data-options="field:'**',width:200">**</th>
<th data-options="field:'**',width:150">**1</th>
<th data-options="field:'**',width:150">**2</th>
<th data-options="field:'**',width:100">**</th>
<th data-options="field:'**',width:80">**</th>
<th data-options="field:'**',width:100">**</th>
<th data-options="field:'**',width:100">**</th>
<th data-options="field:'**',width:300">**</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>

 

public class PageRows
{
public int PageSize { get; set; }
public int PageIndex { get; set; }
}

视图datagrid会自动传两个参数进来page(页码,第几页)跟rows(每页显示行数)

然后再接收total跟rows两个参数来进行分页(这两个参数是固定的不需要后台去定义,只要把值赋给它们返回就行)

控制

public class JsonController : Controller
{

  

/// <summary>
/// 分页
/// </summary>
/// <param name="page"></param>
/// <param name="rows"></param>
/// <returns></returns>
public JsonResult Dt(int page, int rows)
{
SqliteConnection sq = new SqliteConnection();


var temp = new PageRows()
{
PageIndex = page,
PageSize = rows
};
int totalNum = 0;
var datalist = sq.LoadPage(temp, out totalNum);//访问数据层,传入temp ,指定一个返回参数 
var result = from dal in datalist select new { dal.ID,dal.**, dal.**, dal.**, dal.**, dal.**, dal.**, dal.**,
dal.**, dal.**, dal.**, dal.**, dal.Address };遍历把需要传人视图datagrid的字段放入result
var jsonResult = new { total = totalNum, rows = result };给total,rows赋值(datagrid分页用这两个参数分页)
JsonResult json = new JsonResult { Data = jsonResult, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
return json;//返回json格式的数据
}

}

数据

/// <summary>
/// 分页
/// </summary>
/// <param name="param"></param>
/// <param name="total"></param>
/// <returns></returns>

public IEnumerable<DataList> LoadPage(PageParam param, out int total)
{
string dt = string.Format("Select * from Information");
SqliteConnection sq = new SqliteConnection();
DataSet ds = sq.Search(dt);//查询数据库
List<DataList> dataList = new List<DataList>();
foreach (DataRow row in ds.Tables[0].Rows)
{
try
{
DataList d = new DataList();
d.ID = Convert.ToInt32(row["Id"]);
d.**= row["**"].ToString();
d.**= row["**"].ToString();
d.**= row["**"].ToString();
d.**= row["**"].ToString();
d.**= row["**"].ToString();
d.**= row["**"].ToString();
d.**= row["**"].ToString();
d.**= row["**"].ToString();
d.**= row["**"].ToString();
d.**= row["**"].ToString();
d.**= row["**"].ToString();
d.**= row["**"].ToString();
dataList.Add(d);
}
catch (Exception ex)
{

}
}
total = dataList.Count();
var result = dataList.OrderBy(d => d.Id)

.Skip(param.PageSize * (param.PageIndex - 1))//跳过总行数*页数 返回剩余的行数的数据

.Take(param.PageSize);//获取剩余行数中的指定行数的数据
return result;
}
}
把查询数据库方法补上全照搬功能即可实现

posted on 2014-06-24 16:41  ChrisPaul  阅读(816)  评论(0编辑  收藏  举报