0

我正在使用带有服务器端处理的数据表。

我的问题是分页菜单中的“下一步”按钮工作不正确。

如果选择第 1 页,Next 工作正常;如果选择了第 2 页,按下一步将使表格加载第 102 页(iDisplayStart 参数设置为 1010 而不是 20)。

这是我的初始化代码:

 oTable = $('#solutionsTable').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bSort": false,
        "oLanguage": {
            "sSearch": "Search all columns:"
        },
        "bProcessing": true,            
        "bServerSide": true,
        "sAjaxSource": '@Url.Action("AsyncGetUserData")',           
        "bDeferRender": true,
        "aoColumns": [                                  
            //User
            { "mData": function(source, type, val){
                var returnString = "<b>Name: </b>" +"<span>" + source.userName + "</span>" +    
                    "<br/><b>Type: </b>" + "<span>" + source.roleName + "</span><br/>" +
                    "<b>Status: </b>" + "<span>" + source.isActive + "</span>";
                return returnString;
            }},

    ...................................

            //Options
            { "mData": function(source, type, val){
                var editUrl = '@Url.Action("Account", "CMS")' + '?userId=' + source.userID;

                var returnString = "<a href=" + editUrl + ">Edit</a>" + " / " +
                    "<a href=\"#\" onclick=\"postDelete('" + source.userID +"')\">Delete</a>";
                return returnString;
            }}
        ]
    }).fnSetFilteringDelay();

这是我的控制器动作:

public ActionResult AsyncGetUserData(jQueryDataTableParamModel param)
    {           
        var allUsers = CMSHelper.GetUsers(countries: User.IsInRole("Account Admin") ? CMSHelper.GetAdminCountries(User.Identity.Name).Select(c => c.Key).ToList() : null);
        IEnumerable<UserModel> filteredResults = null;
        string filterLowerCase = "";
        string userFilter = "";
        string activityFilter = "";
        string contactFilter = "";
        string workFilter = "";
        string addressFilter = "";

        if (!String.IsNullOrWhiteSpace(param.sSearch))
            filterLowerCase = param.sSearch.ToLower();
        if (!String.IsNullOrWhiteSpace(Request["sSearch_0"].ToString()))
            userFilter = Request["sSearch_0"].ToString().ToLower();
        if (!String.IsNullOrWhiteSpace(Request["sSearch_1"].ToString()))
            activityFilter = Request["sSearch_1"].ToString();
        if (!String.IsNullOrWhiteSpace(Request["sSearch_2"].ToString()))
            contactFilter = Request["sSearch_2"].ToString().ToLower();
        if (!String.IsNullOrWhiteSpace(Request["sSearch_3"].ToString()))
            workFilter = Request["sSearch_3"].ToString().ToLower();
        if (!String.IsNullOrWhiteSpace(Request["sSearch_4"].ToString()))
            addressFilter = Request["sSearch_4"].ToString().ToLower();

        if (!String.IsNullOrWhiteSpace(param.sSearch))
            filteredResults = allUsers.Where(f => f.userName.ToLower().Contains(filterLowerCase)
                || f.firstName.ToLower().Contains(filterLowerCase)
                || f.lastName.ToLower().Contains(filterLowerCase)
                || (f.City == null ? "N/A" : f.City).ToLower().Contains(filterLowerCase)
                || (f.company == null ? "N/A" : f.company).ToLower().Contains(filterLowerCase)
                || f.countryName.ToLower().Contains(filterLowerCase)
                || f.email.ToLower().Contains(filterLowerCase)
                || (f.job == null ? "N/A" : f.job).ToLower().Contains(filterLowerCase)
                || f.LastLogin.ToString().Contains(filterLowerCase)
                || f.LoginsNum.ToString().Contains(filterLowerCase)
                || (f.phone == null ? "N/A" : f.phone).ToLower().Contains(filterLowerCase)
                || (f.Postcode == null ? "N/A" : f.Postcode).ToLower().Contains(param.sSearch)
                || (f.Street == null ? "N/A" : f.Street).ToLower().Contains(filterLowerCase)
                || f.type.ToLower().Contains(filterLowerCase)
                || (f.isActive == true ? "active" : "inactive").Contains(filterLowerCase)).Select(f => f);
        else
            filteredResults = allUsers;

        if (!String.IsNullOrWhiteSpace(userFilter))
            filteredResults = filteredResults.Where(f => f.userName.ToLower().Contains(userFilter)
                || f.type.ToLower().Contains(userFilter)
                || (f.isActive == true ? "active" : "inactive").Contains(userFilter));

        if (!String.IsNullOrWhiteSpace(activityFilter))
            filteredResults = filteredResults.Where(f => f.LoginsNum.ToString().Contains(activityFilter)
                || f.LastLogin.ToString().Contains(activityFilter));

        if (!String.IsNullOrWhiteSpace(contactFilter))
            filteredResults = filteredResults.Where(f => f.firstName.ToLower().Contains(contactFilter)
                || f.lastName.ToLower().Contains(contactFilter)
                || f.email.ToLower().Contains(contactFilter)
                || (string.IsNullOrEmpty(f.phone) ? "N/A" : f.phone).ToLower().Contains(contactFilter));

        if (!String.IsNullOrWhiteSpace(workFilter))
            filteredResults = filteredResults.Where(f => (f.company == null ? "N/A" : f.company).ToLower().Contains(workFilter)
                || (f.job == null ? "N/A" : f.job).ToLower().Contains(workFilter));

        if (!String.IsNullOrWhiteSpace(addressFilter))
            filteredResults = filteredResults.Where(f => f.countryName.ToLower().Contains(addressFilter)
                || (f.City == null ? "N/A" : f.City).ToLower().Contains(addressFilter)
                || (f.Street == null ? "N/A" : f.Street).ToLower().Contains(addressFilter)
                || (f.Postcode == null ? "N/A" : f.Postcode).ToLower().Contains(addressFilter));           

        var displayedUsers = filteredResults.Skip(param.iDisplayStart).Take(param.iDisplayLength);

        var result = from u in displayedUsers
                     select new
                     {
                         userID = Convert.ToString(u.userId),
                         userName = u.userName,                             
                         roleName = u.type,
                         isActive = u.isActive == true ? "Active" : "Inactive",
                         firstName = u.firstName,
                         lastName = u.lastName,
                         email = u.email,
                         phone = (u.phone == null ? "N/A" : u.phone),
                         postcode = (u.Postcode == null ? "N/A" :u.Postcode),
                         street = (u.Street == null ? "N/A" : u.Street),
                         company = (u.company == null ? "N/A" : u.company),                             
                         job = (u.job == null ? "N/A" : u.job),                             
                         countryName = u.countryName,
                         city = (u.City == null ? "N/A" : u.City),
                         loginsNum = u.LoginsNum.ToString(),
                         lastLogin = u.LastLogin.ToString()
                     };

        return Json(new
        {
            sEcho = int.Parse(param.sEcho),
            iTotalRecords = allUsers.Count(),
            iTotalDisplayRecords = filteredResults.Count(),               
            aaData = result
        }, JsonRequestBehavior.AllowGet);           
    }

我搜索了数据表论坛,可以找到类似的东西。如果您之前遇到过此问题或有任何想法可能是什么原因,请告诉我。谢谢。

4

1 回答 1

0

我发现了问题:我正在实现 fnLengthChange,它弄乱了 iDisplayStart 参数;我需要它以编程方式更改显示为使用 cookie 的状态 svae 机制的一部分的条目数;我删除了 fnLengthChange 实现并使用本地存储使用状态保存,现在它可以完美运行。

http://datatables.net/blog/localStorage_for_state_saving

于 2013-09-24T09:09:40.727 回答