最有效的使用Jquery分页的方法&历史插件一起
The most efficient way to use Jquery pagination & history plugins together
我想用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代码混合这两个插件
总的来说,这看起来是一个不错的方法。随着你对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()
来清理。我不太明白你的目标是什么,但我想你是在找前一行。我不能确切地告诉你想隐藏和删除什么,但使用close()可能会有所帮助,并动画隐藏,等待它完成之前删除的东西。
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; }
Dylan,我认为用closest()
替换parent().prev()
不是什么大问题…大代码有这么大的问题……例如,看看这个函数
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
让我们自己检查一下……
相关文章:
- 如何在Angular2中使用jQuery插件
- 可以前端maven插件使用节点,npm已经安装
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- 我的jQuery插件参数没有正确启动,遇到了问题
- 如何在elfinder插件(一个文件管理器插件)上获得上传前事件
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- TableExport jquery插件:文件名和扩展名问题
- 如何在使用selectBoxIt JQuery插件时检测选项更改
- jQuery插件-从插件中调用公共方法
- 压缩phonegap中ios的图像插件
- jQuery粘性插件可变顶部间距
- Jquery表单验证插件-如果选中复选框,如何在提交时执行某些操作
- 高亮显示与数组字符串一起使用时文本插件中断
- 如何在历史js插件中添加问号参数而不是正斜杠
- 如何改变URL(哈希),当用户滚动或点击链接与jquery历史插件
- Jquery历史插件不包括脚本
- 最有效的使用Jquery分页的方法&历史插件一起
- 隐藏inAppBrowser插件Cordova 3+中的历史按钮
- 在更改选项卡之前运行一些内容'的历史记录出现在Firefox插件中
- jQuery历史插件,谷歌网站地图和SEO