简体   繁体   English

JQuery Datatables服务器端处理和过滤器

[英]JQuery Datatables Server Side Processing and Filter

I have a couple of datatables which contain a lot of data so i am using server side processing in order to provide the data in order to increase performance. 我有几个包含大量数据的数据表,因此我正在使用服务器端处理以便提供数据以提高性能。 Generally these work absolutely fine. 通常,这些工作绝对正常。 However, the issue comes when trying to filter on the tables. 但是,尝试在表上进行筛选时会出现问题。 It doesnt seem to respect the where clauses in my LINQ statement and i am at a loss as to why. 它似乎不尊重我的LINQ语句中的where子句,而我对此却一无所知。

One of my datatable initisalisation examples looks like this: 我的数据表感染案例之一如下所示:

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

And the server side method is as follows: 而服务器端的方法如下:

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;
    }

They do not seem very consistent. 他们似乎不太一致。 As in this example it never returns correct part numbers and is on and off as to whether it returns other columns that match the input also. 像本例一样,它从不返回正确的零件号,并且是否打开还与输入匹配的其他列。

The input is always one word without spaces and is converted to lower case so it should match but does not return correctly. 输入始终是一个不带空格的单词,并将其转换为小写,因此它应该匹配但不能正确返回。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Many Thanks!!!! 非常感谢!!!!

Please replace below code. 请替换下面的代码。 This might be issue of Speed. 这可能是速度问题。 Please share if any error you got: 如果您遇到任何错误,请分享:

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())));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM