Note that there are some explanatory texts on larger screens.

plurals
  1. POThe most efficient way to use Jquery pagination & history plugins together
    primarykey
    data
    text
    <p>In my ASP.NET MVC 3 application I want to use Jquery's <code>pagination</code> plugin to make my forum...</p> <p>So I want, to do all page changes with Ajax, and want to use <code>history</code> plugin for working browser's back and next buttons...</p> <p>What the most efficient javascript/jquery code for that?</p> <p>I wrote this code month ago, but now it seems me ugly..Check that</p> <pre><code>$(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")&gt;@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('&lt;tr style="display:none"&gt;&lt;td id="pagination-td" colspan="3" style="border-bottom:none;"&gt;&lt;/td&gt;&lt;/tr&gt;'); if(elem.find("a").attr("id")&gt; @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('&lt;a href="#"&gt;' + $(".pagination span.current:not(.prev):not(.next)").html() + '&lt;/a&gt;'); $(".pagination a:not(.prev):not(.next)").eq(page - 1).replaceWith('&lt;span class="current"&gt;' + $(".pagination a:not(.prev):not(.next)").eq(page - 1).html() + "&lt;/span&gt;"); if ($(".pagination span.current:not(.prev):not(.next)").next().html() == "Next") { $(".pagination span.prev").replaceWith('&lt;a class="prev" href="#"&gt;Prev&lt;/a&gt;'); $(".pagination a.next").replaceWith('&lt;span class="current next"&gt;Next&lt;/span&gt;'); } else { if ($(".pagination span.current:not(.prev):not(.next)").prev().html() == "Prev") { $(".pagination a.prev").replaceWith('&lt;span class="current prev" &gt;Prev&lt;/span&gt;'); $(".pagination span.next").replaceWith('&lt;a class="next" href="#"&gt;Next&lt;/a&gt;'); } else { $(".pagination span.prev").replaceWith('&lt;a class="prev" href="#"&gt;Prev&lt;/a&gt;'); $(".pagination span.next").replaceWith('&lt;a class="next" href="#"&gt;Next&lt;/a&gt;'); } } } </code></pre> <p>There are threads of forum in page and I use one pagination(to active thread)... By clicking other more button, the current thread is changed to that thread.</p> <p>So in the html I have this</p> <pre><code>&lt;table id="forum-table" class="border-top"&gt; @foreach (var forum in Model) { &lt;tr class="forum-desc"&gt; &lt;td class="forum-name" colspan="4"&gt;&lt;a class="label-h4 purple" href="/Forum/Forums/@forum.Key.Forum.Id"&gt;@forum.Key.Forum.Name&lt;/a&gt; @if (forum.Key.Forum.Description != null) { &lt;span&gt; - &lt;/span&gt; &lt;span&gt;@forum.Key.Forum.Description&lt;/span&gt; }&lt;/td&gt; &lt;/tr&gt; for(int index = 0; index &lt; forum.Value.Count; index++) { &lt;tr&gt; &lt;td class="thread-pic"&gt;&lt;img src="/img/forum/thread-icon.png" alt="" /&gt;&lt;/td&gt; &lt;td class="thread-name"&gt; &lt;a href="/Forum/Thread/@forum.Value[index].Thread.Id" class="blue linked"&gt;@forum.Value[index].Thread.Title&lt;/a&gt; &lt;/td&gt; &lt;td class="thread-post-count"&gt; &lt;span&gt;Posts:&lt;/span&gt;@forum.Value[index].PostCount &lt;/td&gt; &lt;td class="thread-information"&gt; &lt;span&gt;by &lt;/span&gt;&lt;a class="blue" href="/Home/UserProfile/@forum.Value[index].LastPostUserName"&gt;@forum.Value[index].LastPostUserName&lt;/a&gt; &lt;br /&gt; @if (forum.Value[index].LastPostDate != DateTime.MinValue) { @forum.Value[index].LastPostDate } &lt;/td&gt; &lt;/tr&gt; } if (forum.Key.ThreadCount &gt; @MyProject.Constants.THREAD_VISIBLE_COUNT) { &lt;tr id="forum@(forum.Key.Forum.Id)"&gt; &lt;td class="more-td"&gt; &lt;a href="javascript:void" class="blue linked more-button" id="@forum.Key.ThreadCount"&gt;more&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; } if (forum.Key.Forum.Id == Model.Keys.FirstOrDefault().Forum.Id) { &lt;tr style="display:none"&gt; &lt;td id="pagination-td" colspan="3" style="border-bottom:none;"&gt;&lt;/td&gt; &lt;/tr&gt; } if (forum.Key.ThreadCount == 0) { &lt;tr&gt; &lt;td colspan="4"&gt;&lt;span&gt;No threads available in this forum&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; } } &lt;/table&gt; </code></pre> <p>So I want shortest(efficient) javascript/jquery code for mixing this two plugins.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload