2016-09-22 142 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