使用jQuery分页和历史记录插件的最有效方法

5
在我的ASP.NET MVC 3应用程序中,我想使用Jquery的“pagination”插件来制作我的论坛...
所以,我想要用Ajax进行所有页面更改,并想要使用“history”插件来处理浏览器的后退和下一个按钮...
对于这个问题,最有效的javascript/jquery代码是什么?
我一个月前写了这段代码,但现在它看起来很丑..请检查一下
$(document).ready(function () {

         $.history.init(function (hash) {

            if (hash != "") {
                NavigateToPage(hash.substring(0,hash.indexOf("page")),hash.substring(hash.lastIndexOf("_")+1));
            } else {
                    if(firstTimeOpen==true){
                        firstTimeOpen=false;
                    }
                    else
                    {
                        window.location.replace("/Forum");
                    }
            }

        });

        $(".more-button").click(function () {

            currentForumId=$("#pagination-td").parent().prev().attr("id").substring(5);
            $.history.load($(this).parents("tr").attr("id").substring(5)+"page_1");

            });


        });


    function NavigateToPage(forumId,page) {
            lastForumId=currentForumId;
            currentForumId=forumId;
            var elem=$("#forum"+forumId);
            var elemStr="#forum"+forumId;
            if($("#pagination-td").parent().prev().attr("id").substring(5)!=forumId)
            {
            forumChanged=true;
            MakeChanges(elem,page);
            }
            else
            {
                if($(".pagination").html()==null)
                {
                    if(elem.find("a").attr("id")>@MyProject.Constants.THREAD_COUNT)
                    {
                   $("#pagination-td").pagination(elem.find("a").attr("id"), {
                   items_per_page: @MyProject.Constants.THREAD_COUNT,
                    callback: handlePaginationClick
                    });
                    }
                }
            }


          $.get('/Forum/ForumThreads', { id: forumId, beginNumber: page - 1, count: @MyProject.Constants.THREAD_COUNT }, function (data) {
                    RemoveElements(elem.prev(), @MyProject.Constants.THREAD_COUNT);
                    elem.before(data);
                    elem.hide();
                    $("tr:not("+elemStr+"):hidden").show(300);

          });
          CorrectPagination(page);

    }

    function RemoveElements(element) 
    {
        if (element.hasClass("forum-desc")) return false;
        var prevElement=element.prev();
        element.remove();
        RemoveElements(prevElement);

    }

    function MakeChanges(elem,page)
    {

            var element=elem.prev();
            if(forumChanged==true)
            {

            $.get('/Forum/ForumThreads', { id: lastForumId, beginNumber:0, count: @MyProject.Constants.THREAD_VISIBLE_COUNT }, function (data) {
                    RemoveElements($("#pagination-td").parent().prev().prev());
                    $("#pagination-td").parent().prev().before(data);

                    $("#pagination-td").parent().prev().hide();
                    $("#pagination-td").parent().remove();
                    elem.after('<tr style="display:none"><td id="pagination-td" colspan="3" style="border-bottom:none;"></td></tr>');
                    if(elem.find("a").attr("id")> @MyProject.Constants.THREAD_COUNT)
                       {
                               $("#pagination-td").pagination(elem.find("a").attr("id"), {
                               items_per_page: @MyProject.Constants.THREAD_COUNT,
                               callback: handlePaginationClick
                               });
                       }
                        $("tr:hidden").show(300);
                        CorrectPagination(page);
                        $("#pagination-td").parent().prev().hide();

            });
            }
    }

    function handlePaginationClick(new_page_index,pagination_container) 
    {
         $.history.load(currentForumId+"page_"+(new_page_index+1));
         return false;
    }
function CorrectPagination(page) {
    $(".pagination span.current:not(.prev):not(.next)").replaceWith('<a href="#">' + $(".pagination span.current:not(.prev):not(.next)").html() + '</a>');

    $(".pagination a:not(.prev):not(.next)").eq(page - 1).replaceWith('<span class="current">' + $(".pagination a:not(.prev):not(.next)").eq(page - 1).html() + "</span>");

    if ($(".pagination span.current:not(.prev):not(.next)").next().html() == "Next") {
        $(".pagination span.prev").replaceWith('<a class="prev" href="#">Prev</a>');
        $(".pagination a.next").replaceWith('<span class="current next">Next</span>');

    }
    else {
        if ($(".pagination span.current:not(.prev):not(.next)").prev().html() == "Prev") {
            $(".pagination a.prev").replaceWith('<span class="current prev" >Prev</span>');
            $(".pagination span.next").replaceWith('<a class="next" href="#">Next</a>');

        }
        else {
            $(".pagination span.prev").replaceWith('<a class="prev" href="#">Prev</a>');
            $(".pagination span.next").replaceWith('<a class="next" href="#">Next</a>');
        }
    }
}

网页中有许多论坛线程,我使用一个分页器(用于激活线程)... 通过点击其他“更多”按钮,当前线程将更改为该线程。

因此,在html中我有这个:

<table id="forum-table" class="border-top">
@foreach (var forum in Model)
{

    <tr class="forum-desc">
    <td class="forum-name" colspan="4"><a class="label-h4 purple" href="/Forum/Forums/@forum.Key.Forum.Id">@forum.Key.Forum.Name</a>
    @if (forum.Key.Forum.Description != null)
    {
        <span> - </span>
        <span>@forum.Key.Forum.Description</span>
    }</td>
    </tr>

    for(int index = 0; index < forum.Value.Count; index++)
    {
            <tr>
            <td class="thread-pic"><img src="/img/forum/thread-icon.png" alt="" /></td>
            <td class="thread-name">
            <a href="/Forum/Thread/@forum.Value[index].Thread.Id" class="blue linked">@forum.Value[index].Thread.Title</a>
            </td>
            <td class="thread-post-count">
            <span>Posts:</span>@forum.Value[index].PostCount
            </td>
            <td class="thread-information">
            <span>by </span><a class="blue" href="/Home/UserProfile/@forum.Value[index].LastPostUserName">@forum.Value[index].LastPostUserName</a> <br />
            @if (forum.Value[index].LastPostDate != DateTime.MinValue)
            {
                @forum.Value[index].LastPostDate
            }
            </td>
            </tr>
            }

            if (forum.Key.ThreadCount > @MyProject.Constants.THREAD_VISIBLE_COUNT)
            {
                <tr id="forum@(forum.Key.Forum.Id)">
                <td class="more-td">
                <a href="javascript:void" class="blue linked more-button" id="@forum.Key.ThreadCount">more</a>
                </td>
                </tr>
            }
            if (forum.Key.Forum.Id == Model.Keys.FirstOrDefault().Forum.Id)
            {
               <tr style="display:none">
               <td id="pagination-td" colspan="3" style="border-bottom:none;"></td>
               </tr>
            }
            if (forum.Key.ThreadCount == 0)
            {
                <tr>
                <td colspan="4"><span>No threads available in this forum</span></td>
                </tr>
            }

}
</table>

所以我希望能得到一段最短(高效)的javascript/jquery代码,用于混合这两个插件。

1
你能否发布一下你已经尝试过的代码,这样我们就有了一个共同的讨论基础,并且可以看看它如何改进(如果需要)? - Darin Dimitrov
当然,我现在会编辑我的问题))) - Chuck Norris
我刚刚编辑了我的问题... - Chuck Norris
2个回答

4

总体来看,这似乎是一种不错的方法。随着您更加熟练使用jQuery,您将学会链式调用更多功能并使用高级后代和相邻选择器进行更改,从而需要更少的代码。

大多数函数有点冗长,但这并不是件坏事,因为它显示了您的工作。

这个块还有很大的改进空间。

RemoveElements($("#pagination-td").parent().prev().prev());
$("#pagination-td").parent().prev().before(data);

$("#pagination-td").parent().prev().hide();
$("#pagination-td").parent().remove();
elem.after('<tr style="display:none"><td id="pagination-td" colspan="3" style="border-bottom:none;"></td></tr>');

您可以尝试使用closest()来清除,而不是使用.parent().prev()。我无法确定您的目标是什么,但我认为您正在寻找前一个表格行。我无法确定您试图隐藏和删除什么内容,但使用closest()可能会有所帮助,并且在隐藏和等待完成之后再删除某些内容时进行动画处理。
RemoveElements($("#pagination-td").closest(".thread"));
$("#pagination-td").closest(".thread").before(data);

$("#pagination-td").closest(".thread").hide('fast', function() {
   $("#pagination-td").closest(".itemToRemove").remove(); });

$('#formTable tr:last').after('<tr class="hidden"><td id="pagination-td" colspan="3"></td></tr>');

现在将您的内联CSS移动到样式表中。
#pagination-td { border-bottom: none; }

2
迪伦,我认为将parent().prev()更改为closest()并不是什么大问题...在大型代码中存在如此大的问题...例如,请看这个函数。
function CorrectPagination(page) {
$(".pagination span.current:not(.prev):not(.next)").replaceWith('<a href="#">' + $(".pagination span.current:not(.prev):not(.next)").html() + '</a>');

$(".pagination a:not(.prev):not(.next)").eq(page - 1).replaceWith('<span class="current">' + $(".pagination a:not(.prev):not(.next)").eq(page - 1).html() + "</span>");

if ($(".pagination span.current:not(.prev):not(.next)").next().html() == "Next") {
    $(".pagination span.prev").replaceWith('<a class="prev" href="#">Prev</a>');
    $(".pagination a.next").replaceWith('<span class="current next">Next</span>');

}
else {
    if ($(".pagination span.current:not(.prev):not(.next)").prev().html() == "Prev") {
        $(".pagination a.prev").replaceWith('<span class="current prev" >Prev</span>');
        $(".pagination span.next").replaceWith('<a class="next" href="#">Next</a>');

    }
    else {
        $(".pagination span.prev").replaceWith('<a class="prev" href="#">Prev</a>');
        $(".pagination span.next").replaceWith('<a class="next" href="#">Next</a>');
    }
}
 }

在那个问题中,我找到了一个更有效的解决方案...看看这个

jquery历史插件是否可以设置当前页面?

有一个相同的功能,但代码更少。

var panel = jQuery(this);
// Attach control functions to the DOM element 
 this.selectPage = function(page_id){ pageSelected(page_id);}

然后

$("#Pagination")[0].selectPage(9); //0-based, 9 = page 10

让我们来检查一下自己... 对于其他功能也可能有更短的方式...


谢谢Artur Keyan...那真的很有帮助...我很乐意听取您对我的JavaScript代码功能的建议... - Chuck Norris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接