【问题标题】:DataTable Quick search not working on server sideDataTable快速搜索在服务器端不起作用
【发布时间】:2016-11-26 13:00:20
【问题描述】:

我试图在我的项目中用 json 填充数据表 一切都很好,除了快速搜索输入没有过滤

我的代码是

$('.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);
        }

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

【问题讨论】:

    标签: jquery ajax asp.net-mvc datatables


    【解决方案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);
    

    【讨论】:

    • 还有其他解决方案吗,因为我有很多表格,而且每个表格都有很多字段
    • 您可以编写一个 linq 查询并使用连接从多个表中获取数据
    • var v = (from l in db.level select l);您可以编写一个 linq 查询并使用连接从多个表中获取数据,就像上面对单个表所做的那样
    • 如果您觉得有帮助,别忘了标记为正确答案
    猜你喜欢
    • 2014-10-06
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 2020-12-01
    • 2015-10-25
    相关资源
    最近更新 更多