jQuery数据表下一步按钮工作不正常

时间:2013-09-24 07:18:35

标签: jquery asp.net-mvc datatables

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

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

如果选择了第1页,则下一步工作正常;如果选择了第2页,则按next将使表加载页面为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);           
    }

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

1 个答案:

答案 0 :(得分:0)

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

http://datatables.net/blog/localStorage_for_state_saving