Javascript 使用Jquery分页的最有效方法&;历史插件在一起

Javascript 使用Jquery分页的最有效方法&;历史插件在一起,javascript,jquery,asp.net-mvc-3,pagination,history,Javascript,Jquery,Asp.net Mvc 3,Pagination,History,在我的ASP.NET MVC 3应用程序中,我想使用Jquery的分页插件使我的论坛 所以我想,用Ajax做所有页面更改,并想使用历史插件来操作浏览器的“后退”和“下一步”按钮 最有效的javascript/jquery代码是什么 我在一个月前写了这段代码,但现在看起来我很难看..检查一下 $(document).ready(function () { $.history.init(function (hash) { if (hash != "")

在我的ASP.NET MVC 3应用程序中,我想使用Jquery的
分页
插件使我的论坛

所以我想,用Ajax做所有页面更改,并想使用
历史
插件来操作浏览器的“后退”和“下一步”按钮

最有效的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>');
        }
    }
}
$(文档).ready(函数(){
$.history.init(函数(散列){
如果(哈希!=“”){
NavigateToPage(hash.substring(0,hash.indexOf(“page”)),hash.substring(hash.lastIndexOf(“”)+1));
}否则{
if(firstTimeOpen==true){
firstTimeOpen=false;
}
其他的
{
窗口。位置。替换(“/论坛”);
}
}
});
$(“.more按钮”)。单击(函数(){
currentForumId=$(“#分页td”).parent().prev().attr(“id”).substring(5);
$.history.load($(this).parents(“tr”).attr(“id”).substring(5)+“page_1”);
});
});
功能导航页面(forumId,第页){
lastForumId=当前FORUMID;
currentForumId=forumId;
var elem=$(“#论坛”+论坛);
var elemStr=“#论坛”+forumId;
if($(“#分页td”).parent().prev().attr(“id”).substring(5)!=forumId)
{
forumChanged=真;
进行更改(元素,第页);
}
其他的
{
if($(“.pagination”).html()==null)
{
if(elem.find(“a”).attr(“id”)>@MyProject.Constants.THREAD\u COUNT)
{
$(“#分页td”).pagination(元素查找(“a”).attr(“id”){
每个页面的项目数:@MyProject.Constants.THREAD\u COUNT,
回调:handlePaginationClick
});
}
}
}
$.get('/Forum/ForumThreads',{id:forumId,beginNumber:page-1,count:@MyProject.Constants.THREAD_count},函数(数据){
移除元素(elem.prev(),@MyProject.Constants.THREAD\u COUNT);
前元素(数据);
隐藏元素();
$(“tr:not(“+elemStr+”):hidden”)。show(300);
});
正确分页(第页);
}
函数删除元素(元素)
{
if(element.hasClass(“forum desc”))返回false;
var preveElement=element.prev();
元素。移除();
去除元素(前元素);
}
函数更改(元素,第页)
{
var元素=elem.prev();
如果(forumChanged==true)
{
$.get('/Forum/ForumThreads',{id:lastForumId,beginNumber:0,count:@MyProject.Constants.THREAD\u VISIBLE\u count},函数(数据){
删除元素($(“#分页td”).parent().prev().prev());
$(“#分页td”).parent().prev().before(数据);
$(“#分页td”).parent().prev().hide();
$(“#分页td”).parent().remove();
元素后(“”);
if(elem.find(“a”).attr(“id”)>@MyProject.Constants.THREAD\u COUNT)
{
$(“#分页td”).pagination(元素查找(“a”).attr(“id”){
每个页面的项目数:@MyProject.Constants.THREAD\u COUNT,
回调:handlePaginationClick
});
}
$(“tr:hidden”).show(300);
正确分页(第页);
$(“#分页td”).parent().prev().hide();
});
}
}
函数handlePaginationClick(新页面索引、分页容器)
{
$.history.load(currentForumId+“page_+(new_page_index+1));
返回false;
}
功能更正分页(第页){
$(“.pagination span.current:not(.prev):not(.next)”).replacetwith(“”);
$(“.pagination a:not(.prev):not(.next)”).eq(第1页)。替换为(“”+$(“.pagination a:not(.prev):not(.next)”).eq(第1页)。html()+“”);
if($(“.pagination span.current:not(.prev):not(.next)”).next().html()=“next”){
$(“.pagination span.prev”)。替换为(“”);
$(“.pagination a.next”).replace为('next');
}
否则{
if($(“.pagination span.current:not(.prev):not(.next)”).prev().html()=“prev”){
$(“.pagination a.prev”)。替换为('prev');
$(“.pagination span.next”)。替换为(“”);
}
否则{
$(“.pagination span.prev”)。替换为(“”);
$(“.pagination span.next”)。替换为(“”);
}
}
}
页面中有论坛的线程,我使用一个分页(到活动线程)。。。单击“其他更多”按钮,当前线程将更改为该线程

所以在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>

@foreach(模型中的var论坛)
{
@if(forum.Key.forum.Description!=null)
{
- 
@forum.Key.forum.Description
}
for(int index=0;index
@if(forum.Value[index].LastPostDate!=DateTime.MinValue)
{
@forum.Value[索引].LastPostDate
}
}
如果(forum.Key.ThreadCount>@MyProject.Constants.THREAD\u VISIBLE\u COUNT)
{
}
if(forum.Key.forum.Id==Model.Keys.FirstOrDefault().forum.Id)
{
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>');
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>');
#pagination-td { border-bottom: none; }
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>');
    }
}
 }
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