JQuery Datatables服务器端处理示例
HTML
<table class="table table-striped table-bordered table-hover" id="table_report">
<thead>
<tr role="row">
<th class="table-checkbox">
选择
</th>
<th>图片</th>
<th>商品名称</th>
<th>商品编码</th>
<th>商品类型</th>
<th>价格</th>
<th>会员价格</th>
</tr>
</thead>
<tbody></tbody>
<tfoot>
<tr role="row">
<th class="table-checkbox">
<input type="checkbox" class="group-checkable" data-set="#table_report .checkboxes" />
</th>
<th>图片</th>
<th>商品名称</th>
<th>商品编码</th>
<th>商品类型</th>
<th>价格</th>
<th>会员价格</th>
</tr>
</tfoot>
</table>
<script type="text/javascript">
$(function () {
var table = $('#table_report');
var oTable = table.dataTable({
"processing": true,
"serverSide": true,
//"stateSave": true, // save datatable state(pagination, sort, etc) in cookie.
"pagingType": "bootstrap_full_number",
"language": {
"aria": {
"sortAscending": ": 以升序排列此列",
"sortDescending": ": 以降序排列此列"
},
"loadingRecords": "数据载入中...",
"emptyTable": "表中数据为空",
"info": "显示第 _START_ 至 _END_ 项结果,共 _TOTAL_ 项",
"infoEmpty": "无数据",
"infoFiltered": "(由 _MAX_ 项过滤得到)",
"infoPostFix": "",
"infoThousands": ",",
"lengthMenu": "显示 _MENU_ 项结果",
"search": "搜索:",
"zeroRecords": "没有匹配结果",
"paging": {
"first": "首页",
"previous": "上页",
"next": "下页",
"last": "末页"
},
"paginate": {
"previous": "上一页",
"next": "下一页",
"last": "尾页",
"first": "首页"
}
},
"ajax": {
url: "/DiscountInfo/DiscountGoodList/" + $("#GoodId").val(),
contentType: "application/json",
type: "POST",
data: function (d) {
var x = JSON.stringify(d);
//console.log(x);
return x;
},
},
"columns": [
{
"data": "Id", "render": function (data, type, full, meta) {
return '<input type="checkbox" class="checkboxes" value="' + data + '"/>';
}
},
{ "data": "ImgPath", "name": "图片", "orderable": false ,"render": function (data, type, full, meta) {
return '<img src="' + data + '" height="48" width="48" onerror="errorProImg(this)"></img>';
}},
{ "data": "ProName", "name": "商品名称", "orderable": true },
{ "data": "ProNumber", "name": "商品编码", "orderable": true },
{ "data": "CategoryTypeName", "name": "商品类型", "orderable": true },
{ "data": "OrdinaryPrice", "name": "价格", "orderable": true },
{ "data": "VipPrice", "name": "会员价格", "orderable": true }],
"rowCallback": function (row, data) {
$(row).find("input").uniform();
if (data.Selected) {
$(row).addClass('active').find("input").attr("checked", true);
} else { }
$.uniform.update();
},
"lengthMenu": [
[20, 50, 100],
[20, 50, 100] // change per page values here
],
// set the initial value
"pageLength": 20,
//"columnDefs": [{ // set default column settings
// 'targets': [0],
// "searchable": false,
// 'orderable': false
//}],
"order": [
[0, "desc"]// set first column as a default sort by asc
]
});
//单项操作
table.on('change', 'tbody tr .checkboxes', function () {
var checked = $(this).is(":checked");
if (checked) {
$(this).parents('tr').addClass("active");
$.post("/DiscountInfo/UpdateDiscountGoodList", { GoodId: $(this).val(), DiscountId: $("#GoodId").val(), "Selected": true }, function () { });
} else {
$(this).parents('tr').removeClass("active");
$.post("/DiscountInfo/UpdateDiscountGoodList", { GoodId: $(this).val(), DiscountId: $("#GoodId").val(), "Selected": false }, function () { });
}
});
//多项操作
table.find('.group-checkable').on("change", function () {
var set = $(this).attr("data-set");
var checked = $(this).is(":checked");
var list = [];
$(set).each(function () {
if (checked) {
$(this).attr("checked", true);
$(this).parents('tr').addClass("active");
list.push({ GoodId: $(this).val(), DiscountId: $("#GoodId").val(), "Selected": true });
} else {
$(this).attr("checked", false);
$(this).parents('tr').removeClass("active");
list.push({ GoodId: $(this).val(), DiscountId: $("#GoodId").val(), "Selected": false });
}
});
$.uniform.update(set);
$.post("/DiscountInfo/UpdateDiscountGoodLists", { "list": list }, function () { });
});
});
</script>
[HttpPost]
public JsonResult DiscountGoodList(Guid id,datatables table)
{
int total = 0;
var goods = productsService.GetList();
var dGoods = discountgoodsService.GetList().Where(c => c.Deleted == false && c.RuleId == id);
var query = from g in goods
join t in categoryTypeServic.GetList() on g.CategoryTypeId equals t.Id
join dg in dGoods on g.Id equals dg.GoodsId into joinDG
from dept in joinDG.DefaultIfEmpty()
select new GoodListItemViewModel
{
Id = g.Id,
ImgPath = g.ImgPath,
ProName = g.ProName,
ProNumber = g.ProNumber,
VipPrice = g.VipPrice,
OrdinaryPrice = g.OrdinaryPrice,
CategoryTypeId = g.CategoryTypeId,
Selected = dept == null ? false : true,
CategoryTypeName = t.TypeName
};
//数据总数(未过滤)
total = query.Count();
//搜索过滤
if (string.IsNullOrWhiteSpace(table.search.value) == false)
{
query = query.Where(c=>c.ProName.Contains(table.search.value) || c.ProNumber.Contains(table.search.value) || c.CategoryTypeName.Contains(table.search.value));
}
#region 排序
query = query.OrderBy(c => c.Id);
foreach (var item in table.order)
{
if (item.dir == "asc")
{
switch (item.column)
{
case 0:
query = query.OrderBy(c => c.Selected);
break;
case 2:
query = query.OrderBy(c => c.ProName);
break;
case 3:
query = query.OrderBy(c => c.ProNumber);
break;
case 4:
query = query.OrderBy(c => c.CategoryTypeName);
break;
case 5:
query = query.OrderBy(c => c.OrdinaryPrice);
break;
case 6:
query = query.OrderBy(c => c.VipPrice);
break;
default:
query = query.OrderBy(c => c.Selected);
break;
}
}
else {
switch (item.column)
{
case 0:
query = query.OrderByDescending(c => c.Selected);
break;
case 2:
query = query.OrderByDescending(c => c.ProName);
break;
case 3:
query = query.OrderByDescending(c => c.ProNumber);
break;
case 4:
query = query.OrderByDescending(c => c.CategoryTypeName);
break;
case 5:
query = query.OrderByDescending(c => c.ProName);
break;
case 6:
query = query.OrderByDescending(c => c.ProNumber);
break;
default:
query = query.OrderByDescending(c => c.Selected);
break;
}
}
}
#endregion
DatatablesResult<GoodListItemViewModel> jsModel = new DatatablesResult<GoodListItemViewModel>(query, table.start, table.length, table.draw, total);
return Json(jsModel);
}
public class datatables
{
public datatables()
{
this.columns = new List<datatables_column>();
this.order = new List<datatables_order>();
}
public int draw { get; set; }
public List<datatables_column> columns { get; set; }
/// <summary>
/// 排序
/// </summary>
public List<datatables_order> order { get; set; }
/// <summary>
/// 数据开始位置,从0开始
/// </summary>
public int start { get; set; }
/// <summary>
/// 分页大小,-1代表不分页全部返回
/// </summary>
public int length { get; set; }
/// <summary>
/// 全局的搜索条件
/// </summary>
public datatables_search search { get; set; }
}
public class datatables_column
{
public int data { get; set; }
public string name { get; set; }
public bool searchable { get; set; }
public bool orderable { get; set; }
public datatables_search search { get; set; }
}
public class datatables_search
{
public string value { get; set; }
public string regex { get; set; }
}
public class datatables_order
{
public int column { get; set; }
public string dir { get; set; }
}
public class DatatablesResult<T>
{
public DatatablesResult(IQueryable<T> source, int start, int length, int draw, int recordsTotal)
{
this.draw = draw;
this.recordsTotal = recordsTotal;
this.recordsFiltered = source.Count();
this.data = source.Skip(start).Take(length).ToList();
}
public DatatablesResult(IList<T> source, int start, int length, int draw, int recordsTotal)
{
this.draw = draw;
this.recordsTotal = recordsTotal;
this.recordsFiltered = source.Count();
this.data = source.Skip(start).Take(length).ToList();
}
public DatatablesResult(IEnumerable<T> source, int start, int length, int draw, int recordsTotal)
{
this.draw = draw;
this.recordsTotal = recordsTotal;
this.recordsFiltered = source.Count();
this.data = source.Skip(start).Take(length).ToList();
}
public DatatablesResult(IEnumerable<T> source, int recordsFiltered, int draw, int recordsTotal)
{
this.draw = draw;
this.recordsTotal = recordsTotal;
this.recordsFiltered = recordsFiltered;
this.data = source.ToList();
}
public int draw { get; /*private*/ set; }
public int recordsTotal { get; /*private*/ set; }
public int recordsFiltered { get; /*private*/ set; }
public IList<T> data { get; /*private*/ set; }
}

JQuery Datatables服务器端处理示例的更多相关文章
- Jquery DataTables相关示例
一.Jquery-DataTables DataTables 是jquery的一个开源的插件.它具有高度灵活的特性,基于渐进增强的基础,可以为任何表格添加交互.它特性如下: 提供分页,搜索和多列排序: ...
- [jQuery]jQuery DataTables插件自定义Ajax分页实现
前言 昨天在博客园的博问上帮一位园友解决了一个问题,我觉得有必要记录一下,万一有人也遇上了呢. 问题描述 园友是做前端的,产品经理要求他使用jQuery DataTables插件显示一个列表,要实现分 ...
- jquery.datatables中文使用说明
http://www.cnblogs.com/taizhouxiaoba/archive/2009/03/17/1414426.html 本文共四部分:官网 | 基本使用|遇到的问题|属性表 一:官方 ...
- jQuery datatables
jQuery datatables 属性,用例 参考:http://datatables.club/example/ http://blog.csdn.net/mickey_miki/article/ ...
- datatables插件适用示例
本文共四部分:官网 | 基本使用|遇到的问题|属性表 一:官方网站:[http://www.datatables.NET/] 二:基本使用:[http://www.guoxk.com/node/jQu ...
- jQuery dataTables四种数据来源[转]
2019独角兽企业重金招聘Python工程师标准>>> 四种数据来源 对于 dataTables 来说,支持四种表格数据来源. 最为基本的就是来源于网页,网页被浏览器解析为 DOM ...
- ASP.NET MVC+EF在服务端分页使用jqGrid以及jquery Datatables的注意事项
引言: 本人想自己个博客网站出来,技术路线是用ASN.NET MVC5+EF6(Code First)+ZUI+各种Jquery插件,有了这个想法之后就开始选择UI,看了好多bootstrap的模板之 ...
- jQuery DataTables && Django serializer
jQuery DataTables https://www.datatables.net 本文参考的官方示例 http://datatables.net/release-datatables/exam ...
- jquery datatables api (转)
学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ...
随机推荐
- 游戏Loading中的小提示和Loading动画实现
学习unity1年多了,工作也1年了,因为工作需要,有几个月没接触unity Ngui啦. 学的还是不踏实.继续努力吧.由于下周就要进行新游戏的开发,这几天熟悉熟悉NGUI,今天按照现在公司以前的项目 ...
- 网页播放器(jsp、js)
jsp对控件显示 <%@ page language="java" import="java.util.*" pageEncoding="UTF ...
- Android 适配2
Android AutoLayout全新的适配方式 堪称适配终结者 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/4999094 ...
- 无鼠标Windows操作
1.常用tab键,方便跳转 2.打开软件方式: 1.win+1,2,3...依序打开任务栏图标.常用软件可以放在这里: 2.创建quickStart文件夹,配置路径,将所有要用到的功能都放在这里.( ...
- arm v5,v6,v7?
http://blog.csdn.net/woshi_ziyu/article/details/7946862
- U8SDK——游戏接入SDK(只接入抽象框架)
上一篇文章我们说了整个U8 SDK抽象成的设计,那这篇文章,我们就来验证一下,他是否如我们期待的那样,简单灵活. 正如之前所说,对于每个游戏,只需要接入抽象层,而每个渠道SDK的接入,就是该抽象层的一 ...
- iptables Data filtering详解
内容简介防火墙的概述iptables简介iptables基础iptables语法iptables实例案例详解(一)防火墙的简介防火墙是指设置在不同网络或网络安全域之间的一系列部件的组合,它能增强机构内 ...
- linux系统用户以及用户组管理
本系列的博客来自于:http://www.92csz.com/study/linux/ 在此,感谢原作者提供的入门知识 这个系列的博客的目的在于将比较常用的liunx命令从作者的文章中摘录下来,供自己 ...
- jpa更新数据出错
在更新表的一条记录时出错,错误为“java.sql.SQLException: Field 'is_del' doesn't have a default value”,表有一个属性isDel为Boo ...
- hander消息机制原理
基本原理 线程中调用Handler.sendMsg()方法(参数是Message对象),将需要Main线程处理的事件 添加到Main线程的MessageQueue中,Main线程通过MainLoope ...