JQuery数据表服务器端处理和过滤
本文关键字:过滤 处理 服务器端 数据表 JQuery | 更新日期: 2023-09-27 18:08:27
我有几个包含大量数据的数据表,所以我使用服务器端处理,以便提供数据,以提高性能。一般来说,这些工作绝对没问题。然而,当试图对表进行过滤时,问题就出现了。它似乎不尊重我的LINQ语句中的where子句,我不知道为什么。
我的一个数据表初始化示例如下:
$('#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;
}
他们似乎不太一致。与本例中一样,它永远不会返回正确的零件号,并且对于是否也返回与输入匹配的其他列,它是开或关的。
输入始终是一个不带空格的单词,并被转换为小写,因此它应该匹配但不能正确返回。
感谢您的帮助。
多谢! !
请替换以下代码。这可能是速度的问题。如果有任何错误,请分享:
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())));