Asp.net MVC 中利用jquery datatables 實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示功能
1、Controller中的方法代碼如下:
由于方法中的存儲(chǔ)過程沒有帶分頁(yè)參數(shù),所以還可以有繼續(xù)優(yōu)化的空間。
/// <summary>
/// 獲取測(cè)點(diǎn)列表
/// </summary>
/// <returns></returns>
[HttpPost]
public JsonResult GetMeasurePointList(string TreeID, string TreeType, int sEcho, int iDisplayStart, int iDisplayLength)
{
DataTable dtResult = new DataTable();
string sql = string.Format("EXEC P_GET_ZXJG_TagList '{0}','{1}'", TreeID, TreeType);
dtResult = QuerySQL.GetDataTable(sql);
dtResult.Columns.Add("XuHao", typeof(string));
dtResult.Columns.Add("StrValueTime", typeof(string));
for (int i = 0; i < dtResult.Rows.Count; i++)
{
dtResult.Rows[i]["XuHao"] = (i + 1).ToString();
dtResult.Rows[i]["StrValueTime"] = Convert.ToDateTime(dtResult.Rows[i]["F_ValueTime"]).ToString("yyyy-MM-dd HH:mm:ss");
}
int iTotalRecords = 0;
int iTotalDisplayRecords = 0;
List<DataRow> queryList = dtResult.AsEnumerable().ToList();
iTotalRecords = queryList.Count();
queryList = queryList.Skip(iDisplayStart).Take(iDisplayLength).ToList();
iTotalDisplayRecords = queryList.Count();
var temp = from p in queryList
select new
{
XuHao = p.Field<string>("XuHao").ToString(),
F_Description = p.Field<string>("F_Description").ToString(),
StrValueTime = p.Field<string>("StrValueTime").ToString(),
F_Value = p.Field<decimal>("F_Value").ToString(),
F_Unit = p.Field<string>("F_Unit").ToString(),
F_AlmLow = p.Field<decimal>("F_AlmLow").ToString(),
F_AlmUp = p.Field<decimal>("F_AlmUp").ToString()
};
return Json(new
{
draw = sEcho,
recordsFiltered = iTotalRecords,
recordsTotal = iTotalDisplayRecords,
data = temp.ToList()
}, JsonRequestBehavior.AllowGet);
}
2、cshtml視圖頁(yè)面中代碼如下:
function InitData() {
var dataTable = $('#tbMeasurePointList').DataTable({
"scrollY": "hidden",
"scrollCollapse": false,
"dom": 'tr<"bottom"lip><"clear">',
language: {
lengthMenu: '',//左上角的分頁(yè)大小顯示。
search: '<span class="label label-success">搜索:</span>',//右上角的搜索文本,可以寫html標(biāo)簽
loadingRecords: '數(shù)據(jù)加載中...',
paginate: {
//分頁(yè)的樣式內(nèi)容。
previous: "上一頁(yè)",
next: "下一頁(yè)",
first: "",
last: ""
},
zeroRecords: "暫無數(shù)據(jù)",//table tbody內(nèi)容為空時(shí),tbody的內(nèi)容。
//下面三者構(gòu)成了總體的左下角的內(nèi)容。
info: "<span class='pagesStyle'>總共<span class='recordsStyle'> _TOTAL_ 條,計(jì) _PAGES_ </span>頁(yè),當(dāng)前顯示 _START_ -- _END_ 條記錄 </span>",//左下角的信息顯示,大寫的詞為關(guān)鍵字。初始_MAX_ 條
infoEmpty: "0條記錄",//篩選為空時(shí)左下角的顯示。
infoFiltered: ""http://篩選之后的左下角篩選提示,
},
"lengthChange": false,
"ordering": false,
"iDisplayLength": 10,
"searching": false,
destroy: true, //Cannot reinitialise DataTable,解決重新加載表格內(nèi)容問題
"serverSide": true,
"sAjaxSource": "@Url.Action("GetMeasurePointList", "OnlineMonitor")",
"fnServerData": function (sSource, aoData, fnCallback) {
aoData.push({ "name": "TreeID", "value": $("#hidTreeID").val() });
aoData.push({ "name": "TreeType", "value": $("#hidTreeType").val() });
$.ajax({
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});
},
"aoColumns": [
{ "mDataProp": "XuHao", "width": "50" },
{ "mDataProp": "F_Description", "width": "400" },
{ "mDataProp": "StrValueTime", "width": "200" },
{ "mDataProp": "F_Value", "width": "100" },
{ "mDataProp": "F_Unit", "width": "100" },
{ "mDataProp": "F_AlmLow", "width": "100" },
{ "mDataProp": "F_AlmUp", "width": "100"}
],
"createdRow": function (row, data, index) {
$(row).children('td').eq(0).attr('style', 'text-align: center;');
$(row).children('td').eq(1).attr('style', 'text-align: left;');
$(row).children('td').eq(2).attr('style', 'text-align: center;');
$(row).children('td').eq(3).attr('style', 'text-align: right;');
$(row).children('td').eq(4).attr('style', 'text-align: center;');
$(row).children('td').eq(5).attr('style', 'text-align: right;');
$(row).children('td').eq(6).attr('style', 'text-align: right;');
}
});
}
3、實(shí)際顯示效果如下圖所示:

以上所述是小編給大家介紹的Asp.net MVC 中利用jquery datatables 實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
asp.net core 2.0 webapi集成signalr(實(shí)例講解)
下面小編就為大家分享一篇asp.net core 2.0 webapi集成signalr的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11
VS2005 水晶報(bào)表在時(shí)部署時(shí)遇到的問題
前幾天在服務(wù)器上部署一個(gè)B/S程序的時(shí)候,程序中的水晶報(bào)表部分出了些問題,報(bào)錯(cuò):Server Error in '/' Application.2010-02-02
asp.net CommunityServer中的wwwStatus
最近不得不對(duì)這個(gè)論壇進(jìn)行研究,以適應(yīng)后面的發(fā)展,現(xiàn)在同事研究太吃力了,以后改些東西,估計(jì)又要...打開web項(xiàng)目中的communityserver.config發(fā)現(xiàn)一個(gè)有趣的地方。2009-03-03
關(guān)于.NET/C#/WCF/WPF 打造IP網(wǎng)絡(luò)智能視頻監(jiān)控系統(tǒng)的介紹
本篇文章小編將為大家介紹,關(guān)于.NET/C#/WCF/WPF 打造IP網(wǎng)絡(luò)智能視頻監(jiān)控系統(tǒng)的介紹。需要的朋友參考下2013-04-04
.NetCore使用ImageSharp進(jìn)行圖片的生成
ImageSharp是對(duì)NetCore平臺(tái)擴(kuò)展的一個(gè)圖像處理方案,以往網(wǎng)上的案例多以生成文字及畫出簡(jiǎn)單圖形、驗(yàn)證碼等方式進(jìn)行探討和實(shí)踐,今天我分享一下所在公司項(xiàng)目的實(shí)際應(yīng)用案例,導(dǎo)出微信二維碼圖片,圓形頭像,感興趣的朋友一起看看吧2022-06-06
asp.net 在DNN模塊開發(fā)中遇到的resx怪問題
最近我遇到2個(gè)小問題,在此記錄一下。這是我發(fā)的帖子2008-11-11
.NET Core API之格式化輸出對(duì)象OutputFormatter
這篇文章介紹了.NET Core API之格式化輸出對(duì)象OutputFormatter,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
asp.net下將純真IP數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)中的代碼
純真IP數(shù)據(jù)庫(kù)包含最新的IP信息,通過IP我們可以查詢?cè)L問者的來路,地理位置!但下載下來的IP數(shù)據(jù)無法被我們直接調(diào)用,所以我們需要編寫代碼將IP寫入到數(shù)據(jù)庫(kù)中供我們使用!2011-01-01
.NET下通過HttpListener實(shí)現(xiàn)簡(jiǎn)單的Http服務(wù)
這篇文章主要為大家詳細(xì)介紹了.NET下通過HttpListener實(shí)現(xiàn)簡(jiǎn)單Http服務(wù)的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-09-09

