<table id="myDataTable" class="display"> <thead> <tr> <th> 标识 </th> <th> 公司名称 </th> <th> 地址 </th> <th> 城市 </th> </tr> </thead> <tbody> </tbody> </table> <input type="button" id="btnTest" value="根据条件重新响应后台Ajax" />
<script type="text/javascript"> var tbl; $(function () { tbl = $(‘#myDataTable‘).dataTable({ "bServerSide": true, "sAjaxSource": "Home/AjaxHandler", //mvc后台ajax调用接口。 ‘bPaginate‘: true, //是否分页。 "bProcessing": true, //当datatable获取数据时候是否显示正在处理提示信息。 ‘bFilter‘: false, //是否使用内置的过滤功能。 ‘bLengthChange‘: true, //是否允许用户自定义每页显示条数。 ‘sPaginationType‘: ‘full_numbers‘, //分页样式 "aoColumns": [ { "sName": "ID", "bSearchable": false, "bSortable": false, "fnRender": function (oObj) { return ‘<a href=\"Details/‘ + oObj.aData[0] + ‘\">View</a>‘; } //自定义列的样式 }, { "sName": "COMPANY_NAME" }, { "sName": "ADDRESS" }, { "sName": "TOWN" } ] });
//Ajax重新load控件数据。(server端) $("#btnTest").click(function () { var oSettings = tbl.fnSettings(); oSettings.sAjaxSource = "Home/AjaxHandler2"; alert(oSettings.sAjaxSource); tbl.fnClearTable(0); tbl.fnDraw();
}); }); </script>
public class DataTableParameter { /// <summary> /// DataTable请求服务器端次数 /// </summary> public string sEcho { get; set; }
/// <summary> /// 过滤文本 /// </summary> public string sSearch { get; set; }
/// <summary> /// 每页显示的数量 /// </summary> public int iDisplayLength { get; set; }
/// <summary> /// 分页时每页跨度数量 /// </summary> public int iDisplayStart { get; set; }
/// <summary> /// 列数 /// </summary> public int iColumns { get; set; }
/// <summary> /// 排序列的数量 /// </summary> public int iSortingCols { get; set; }
/// <summary> /// 逗号分割所有的列 /// </summary> public string sColumns { get; set; } }
public JsonResult AjaxHandler(DataTableParameter param) { return Json(new { sEcho = param.sEcho, iTotalRecords = 50, iTotalDisplayRecords = 50, aaData = new List<object> { new string[] {"1","公司信息","地址信息","城市信息"}, new string[] {"1","公司信息","地址信息","城市信息"}, new string[] {"1","公司信息","地址信息","城市信息"}, new string[] {"1","公司信息","地址信息","城市信息"}, new string[] {"1","公司信息","地址信息","城市信息"}, new string[] {"1","公司信息","地址信息","城市信息"}, new string[] {"1","公司信息","地址信息","城市信息"}, new string[] {"1","公司信息","地址信息","城市信息"}, new string[] {"1","公司信息","地址信息","城市信息"}, new string[]{"1","公司信息","地址信息","城市信息"} } }, JsonRequestBehavior.AllowGet);
}
生成 html 代码
<table id="myDataTable" class="display"> <thead> <tr> <th style="width: 239px;" class="sorting_disabled"> 标识 </th> <th style="width: 366px;" class="sorting"> 公司名称 </th> <th style="width: 239px;" class="sorting">
地址 </th> <th style="width: 239px;" class="sorting"> 城市 </th> </tr> </thead> <tbody><tr class="odd"><td class=" sorting_1"><a href="Details/1">View</a></td><td>公司信息</td><td>地址信息</td><td>城市信息</td></tr><tr class="even"><td class=" sorting_1"><a href="Details/1">View</a></td><td>公司信息</td><td>地址信息</td><td>城市信息</td></tr><tr class="odd"><td class=" sorting_1"><a href="Details/1">View</a></td><td>公司信息</td><td>地址信息</td><td>城市信息</td></tr><tr class="even"><td class=" sorting_1"><a href="Details/1">View</a></td><td>公司信息</td><td>地址信息</td><td>城市信息</td></tr><tr class="odd"><td class=" sorting_1"><a href="Details/1">View</a></td><td>