2016-09-22 143 views
1

我有几个数据表包含大量数据,所以我使用服务器端处理来提供数据以提高性能。一般来说这些工作绝对好。但是,试图在表格上过滤时会出现问题。它似乎并不尊重我的LINQ声明中的where子句,我为什么会感到不知所措。JQuery Datatables服务器端处理和过滤器

我的一个数据表initisalisation的例子是这样的:

$('#link-list').dataTable({ 
     'bServerSide': true, 
     'sAjaxSource': '@Url.Action("LazyLoadComms", "Communication")', 
     'bProcessing': true, 
     async: false, 
     'aoColumns': [ 
      { 
       'mDataProp':'Id' 
      }, 
      { 
       'mDataProp': 'Customer' 
      }, 
      { 
       'mDataProp': 'Receiver' 
      }, 
      { 
       'mDataProp': 'PartNo' 
      }, 
      { 
       'mDataProp': 'DateOpened' 
      } 
     ], 
     bAutoWidth: false, 
     bLengthChange: false, 
     pageLength: 10, 
     'order': [[4, 'desc']] 
    }); 

而服务器端的方法如下:

public ActionResult LazyLoadComms(JqueryDataTableParams param) 
    { 
     var communications = _uow.CommunicationService.Get().ToList(); 

     IEnumerable<Communication> filteredComms; 
     if (!string.IsNullOrEmpty(param.sSearch)) 
     { 
      filteredComms = communications.Where(c => !string.IsNullOrEmpty(c.Customer.Name) ? c.Customer.Name.ToLower().Contains(param.sSearch.ToLower()) : false 
                || !string.IsNullOrEmpty(c.Receiver) ? c.Receiver.ToLower().Contains(param.sSearch.ToLower()) : false 
                || !string.IsNullOrEmpty(c.PartNo) ? c.PartNo.ToLower().Contains(param.sSearch.ToLower()) : false); 
     } 
     else 
     { 
      filteredComms = communications; 
     } 
     var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]); 

     Func<Communication, string> orderingFunction = (c => sortColumnIndex == 0 ? c.CommunicationId.ToString() : 
                   sortColumnIndex == 1 ? c.Customer.Name : 
                   sortColumnIndex == 2 ? c.Receiver : 
                   sortColumnIndex == 3 ? c.PartNo : 
                   c.DateOpened.ToLongDateString()); 

     var sortDirection = Request["sSortDir_0"]; 
     if (sortDirection == "asc") 
      filteredComms = filteredComms.OrderBy(orderingFunction); 
     else 
      filteredComms = filteredComms.OrderByDescending(orderingFunction); 

     var displayedComms = filteredComms 
           .Skip(param.iDisplayStart) 
           .Take(param.iDisplayLength) 
           .Select(c => new 
           { 
            Id = c.CommunicationId, 
            Customer = c.Customer.Name, 
            Receiver = c.Receiver, 
            PartNo = c.PartNo, 
            DateOpened = c.DateOpened.ToShortDateString() + " " + c.DateOpened.ToShortTimeString() 
           }); 

     var json = Json(new 
     { 
      sEcho = param.sEcho, 
      iTotalRecords = communications.Count(), 
      iTotalDisplayRecords = filteredComms.Count(), 
      aaData = displayedComms 
     }, 
          JsonRequestBehavior.AllowGet); 

     return json; 
    } 

他们似乎并不很一致。正如在这个例子中,它永远不会返回正确的零件号码,并且它是否返回与输入相匹配的其他列。

输入总是一个没有空格的单词,并转换为小写,所以它应该匹配但不能正确返回。

任何帮助,非常感谢。

很多谢谢!

回答

2

请替换下面的代码。这可能是Speed的问题。如果有任何错误,请分享:

filteredComms = communications.Where(c => (!string.IsNullOrEmpty(c.Customer.Name) && c.Customer.Name.ToLower().Contains(param.sSearch.ToLower())) 
    || (!string.IsNullOrEmpty(c.Receiver) && c.Receiver.ToLower().Contains(param.sSearch.ToLower())) 
    || (!string.IsNullOrEmpty(c.PartNo) && c.PartNo.ToLower().Contains(param.sSearch.ToLower()))); 
+0

但是这段代码是过滤和过滤是我需要修复的问题。它会返回数据罚款,如果我不做任何过滤,但它时,即时尝试按列过滤 – DaRoGa

+0

你有没有尝试过@Dot净学习者建议? – markpsmith

+0

我可以将其删除,并将所有记录返回给我。但那不是重点。问题的关键在于,当对该段代码中的列进行im过滤时,它并没有考虑所有列 – DaRoGa