简体   繁体   English

C#ASP MVC5数据表单个搜索列如何过滤&&和||

[英]C# ASP MVC5 Datatable Individual Search column how to filter && and ||

Trying to achieve 试图实现

I have managed to pass the values of all the individual columns to server using AjaxGet . 我已设法使用AjaxGet将所有单个列的值传递给server In the following, pic or example would be t and v . 在下文中, pic或示例将是tv At the server, I will pass the value to a filter method. 在服务器上,我将值传递给filter方法。 Now, I am trying to figure out how to properly filter t and v . 现在,我想弄清楚如何正确过滤tv Currently, after v is inputted, I will filter v and then if t is inputted, ideally it should filter t for LOGIN_ID column and v for NAME column. 目前,在输入v之后,我将过滤v ,然后如果输入t ,理想情况下它应该过滤tLOGIN_ID列, vNAME列。 However, I am getting v or t results. 但是,我得到了vt结果。 Please help. 请帮忙。

在此输入图像描述

CODE

private List<DataItem> FilterData(ref int recordFiltered, int start, int length, string search, string searchLOGIN_ID, string searchNAME, string searchDT_EDIT, int sortColumn, string sortDirection)

    {
        List<DataItem> _data = CreateData();

        List<DataItem> list = new List<DataItem>();

        if (!string.IsNullOrEmpty(searchLOGIN_ID))
        {
            foreach (DataItem dataItem in _data)
            {
                if (
                    dataItem.LOGIN_ID.ToUpper().Contains(searchLOGIN_ID.ToUpper())
                    )
                {
                    list.Add(dataItem);
                }
            }
        }

        if (!string.IsNullOrEmpty(searchNAME))
        {
            foreach (DataItem dataItem in _data)
            {
                if (
                    dataItem.NAME.ToUpper().Contains(searchNAME.ToUpper())
                    )
                {
                    list.Add(dataItem);
                }
            }
        }

        if (!string.IsNullOrEmpty(searchDT_EDIT))
        {
            foreach (DataItem dataItem in _data)
            {
                if (
                    dataItem.DT_EDIT.ToUpper().Contains(searchDT_EDIT.ToUpper())
                    )
                {
                    list.Add(dataItem);
                }
            }
        }

        if (!string.IsNullOrEmpty(searchLOGIN_ID) && !string.IsNullOrEmpty(searchNAME) && !string.IsNullOrEmpty(searchDT_EDIT))
        {
            foreach (DataItem dataItem in _data)
            {
                if (
                    dataItem.LOGIN_ID.ToUpper().Contains(search.ToUpper()) ||
                    dataItem.NAME.ToString().Contains(search.ToUpper()) ||
                    dataItem.DT_EDIT.ToString().Contains(search.ToUpper())
                    )
                {
                    list.Add(dataItem);
                }
            }
        }

        if (!string.IsNullOrEmpty(searchLOGIN_ID) && !string.IsNullOrEmpty(searchNAME))
        {
            foreach (DataItem dataItem in _data)
            {
                if (
                    dataItem.LOGIN_ID.ToUpper().Contains(searchLOGIN_ID.ToUpper()) ||
                    dataItem.NAME.ToUpper().Contains(searchNAME.ToUpper())
                    )
                {
                    list.Add(dataItem);
                }
            }
        }

        if (!string.IsNullOrEmpty(searchLOGIN_ID) && !string.IsNullOrEmpty(searchDT_EDIT))
        {
            foreach (DataItem dataItem in _data)
            {
                if (
                    dataItem.LOGIN_ID.ToUpper().Contains(searchLOGIN_ID.ToUpper()) ||
                    dataItem.DT_EDIT.ToUpper().Contains(searchDT_EDIT.ToUpper())
                    )
                {
                    list.Add(dataItem);
                }
            }
        }

        if (!string.IsNullOrEmpty(searchNAME) && !string.IsNullOrEmpty(searchDT_EDIT))
        {

            foreach (DataItem dataItem in _data)
            {
                if (
                    dataItem.NAME.ToUpper().Contains(searchNAME.ToUpper()) ||
                    dataItem.DT_EDIT.ToUpper().Contains(searchDT_EDIT.ToUpper())
                    )
                {
                    list.Add(dataItem);
                }
            }
        }

        if (!string.IsNullOrEmpty(search))
        {
            foreach (DataItem dataItem in _data)
            {
                if (
                    dataItem.LOGIN_ID.ToUpper().Contains(search.ToUpper()) ||
                    dataItem.NAME.ToString().Contains(search.ToUpper()) ||
                    dataItem.DT_EDIT.ToString().Contains(search.ToUpper())
                    )
                {
                    list.Add(dataItem);
                }
            }
        }

        if(string.IsNullOrEmpty(search) && string.IsNullOrEmpty(searchLOGIN_ID) && string.IsNullOrEmpty(searchNAME) && string.IsNullOrEmpty(searchDT_EDIT))
        {
            list = _data;
        }

        // simulate sort
        //=== sortColumn need to change additional column
        if (sortColumn == 1)
        {   // sort LOGIN_ID
            list.Sort((x, y) => SortString(x.LOGIN_ID, y.LOGIN_ID, sortDirection));
        }
        else if (sortColumn == 2)
        {   // sort NAME
            list.Sort((x, y) => SortString(x.NAME, y.NAME, sortDirection));
        }
        else if (sortColumn == 3)
        {   // sort DT_CREATE
            list.Sort((x, y) => SortDateTime(x.DT_EDIT, y.DT_EDIT, sortDirection));
        }

        recordFiltered = list.Count;

        // get just one page of data
        list = list.GetRange(start, Math.Min(length, list.Count - start));

        return list;
    }

public ActionResult AjaxGetJsonData(int draw, int start, int length)
    {
        var searchLOGIN_ID = Request.QueryString["columns[1][search][value]"];
        var searchNAME = Request.QueryString["columns[2][search][value]"];
        var searchDT_EDIT = Request.QueryString["columns[3][search][value]"];
        string search = Request.QueryString["search[value]"];

        int sortColumn = -1;
        string sortDirection = "asc";
        if (length == -1)
        {
            length = TOTAL_ROWS;
        }
        if (Request.QueryString["order[0][column]"] != null)
        {
            sortColumn = int.Parse(Request.QueryString["order[0][column]"]);
        }
        if (Request.QueryString["order[0][dir]"] != null)
        {
            sortDirection = Request.QueryString["order[0][dir]"];
        }

        DataTableData dataTableData = new DataTableData();
        dataTableData.draw = draw;
        dataTableData.recordsTotal = TOTAL_ROWS;
        int recordsFiltered = 0;
        dataTableData.data = FilterData(ref recordsFiltered, start, length, search, searchLOGIN_ID, searchNAME, searchDT_EDIT, sortColumn, sortDirection);
        dataTableData.recordsFiltered = recordsFiltered;

        return Json(dataTableData, JsonRequestBehavior.AllowGet);
    }

GITHUB Github上

https://github.com/BROMVC5/BROSTANDARD https://github.com/BROMVC5/BROSTANDARD

private List FilterData(ref int recordFiltered, int start, int length, string search, string searchLOGIN_ID, string searchNAME, string searchDT_EDIT, int sortColumn, string sortDirection) private List FilterData(ref int recordFiltered,int start,int length,string search,string searchLOGIN_ID,string searchNAME,string searchDT_EDIT,int sortColumn,string sortDirection)

        {
            List<DataItem> _data = CreateData();        

            if (!string.IsNullOrEmpty(searchLOGIN_ID))
            {
                _data = _data.Where(x => x.LOGIN_ID.ToUpper().Contains(searchLOGIN_ID.ToUpper()).ToList();
            }

            if (!string.IsNullOrEmpty(searchNAME))
            {
              _data = _data.Where(x => x.NAME.ToUpper().Contains(searchNAME.ToUpper()).ToList();            
            }

            if (!string.IsNullOrEmpty(searchDT_EDIT))
            {
             _data = _data.Where(x => x.DT_EDIT.ToUpper().Contains(searchDT_EDIT.ToUpper()).ToList();
            }

            if (!string.IsNullOrEmpty(search))
            {
             _data = _data.Where(x => x.LOGIN_ID.ToUpper().Contains(search.ToUpper()) ||
                        x.NAME.ToString().Contains(search.ToUpper()) ||
                        x.DT_EDIT.ToString().Contains(search.ToUpper()).ToList();            
            }
             list = _data;

            // past this point this is your original code :)
            // simulate sort
            //=== sortColumn need to change additional column
            if (sortColumn == 1)
            {   // sort LOGIN_ID
                list.Sort((x, y) => SortString(x.LOGIN_ID, y.LOGIN_ID, sortDirection));
            }
            else if (sortColumn == 2)
            {   // sort NAME
                list.Sort((x, y) => SortString(x.NAME, y.NAME, sortDirection));
            }
            else if (sortColumn == 3)
            {   // sort DT_CREATE
                list.Sort((x, y) => SortDateTime(x.DT_EDIT, y.DT_EDIT, sortDirection));
            }

            recordFiltered = list.Count;

            // get just one page of data
            list = list.GetRange(start, Math.Min(length, list.Count - start));

            return list;
        }

Edit : OK can't format it. 编辑:确定无法格式化它。 sorry Hanz ... 对不起汉兹......

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

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