2016-11-26 91 views
0

我試圖填補JSON數據表在我的項目 一切除了不filtringDataTable中快速搜索無法正常工作在服務器端

我的代碼是

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

還有沒有其他的解決辦法,因爲我有很多表和FO他們每個人我有許多領域 –

+0

你可以寫一個LINQ查詢和使用聯接從多個表 –

+0

VAR獲取數據v =(從db.level中的l選擇l); \t 您可以編寫一個linq查詢並使用連接從多個表中獲取數據,如同您對單個表所做的那樣 –