DataTable快速搜索不在服务器端工作

时间:2016-11-26 13:00:20

标签: jquery ajax asp.net-mvc datatables

我试图用我的项目中的json填充数据表 一切都很好,除了快速搜索输入而不是filtring

我的代码是

$('.DT').each(function()
{
    oTable = $(this).DataTable({
        "sPaginationType": "bootstrap",
        "processing": true,
        "serverSide": true,
        "orderMulti": true,
        "sDom":  "<'row separator bottom'<'col-md-5'T><'col-md-3'l><'col-md-4'f>r>t<'row'<'col-md-6'i><'col-md-6'p>>",                      
        "sScrollY": "500px",
        "sScrollX": "100%",
        "sScrollXInner": "100%",
        "bScrollCollapse": true,                    
        "ajax": {
            "url": '/Level/LoadData',
            "type": "POST"
        },
        "columns":  [
            { "data": "level_description", "name": "level_description", "autoWidth": true }, //index 1
            { "data": "monthly_amount", "name": "monthly_amount", "autoWidth": true }, //index 1
            { "data": "transport_amount", "name": "transport_amount", "autoWidth": true }
        ]
    });             
}

我的控制器

 public ActionResult LoadData()
        {
            //jQuery DataTables Param
            var draw = Request.Form.GetValues("draw").FirstOrDefault();

            //Find paging info
            var start = Request.Form.GetValues("start").FirstOrDefault();
            var length = Request.Form.GetValues("length").FirstOrDefault();

            //Find order columns info
            var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
            var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();

            int pageSize = length != null ? Convert.ToInt32(length) : 0;
            int skip = start != null ? Convert.ToInt16(start) : 0;
            int recordsTotal = 0;

            var v = (from l in db.level select l);

            if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
            {
                v = v.OrderBy(sortColumn + " " + sortColumnDir);
            }
            recordsTotal = v.Count();
            var data = v.Skip(skip).Take(pageSize).ToList();

            return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data }, JsonRequestBehavior.AllowGet);
        }

我不认为如果我使用服务器端快速serch将无法正常工作 我不知道我是否错过了什么

enter image description here

1 个答案:

答案 0 :(得分:1)

you have missed the search parameter

 string search = Request.Form.GetValues("search[value]").FirstOrDefault();
        v = v.Where(p =>p.level_description.ToString().ToLower().Contains(search.ToLower()) ||  
                        p.monthly_amount.ToLower().Contains(search.ToLower()) ||  
                        p.transport_amount.ToString().ToLower().Contains(search.ToLower());

    var data = v.Skip(skip).Take(pageSize).ToList();

    return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data }, JsonRequestBehavior.AllowGet);