对javascript中的ajax滚动内容进行排序
Sorting on ajax scrolling content in javascript
我有大约1000条记录,我想在div中显示。我以20条为一组显示它们,当你滚动到底部加载下20条记录。我正在对范围属性上的记录进行排序,但是当我在ajax调用的成功函数中滚动并加载新的20条记录时,div的排序条件不适用于这些div。
这些新记录以串行顺序显示,而不进行排序。我能够对当前页面中的记录进行排序,但如果您排序然后滚动到底部并加载新记录-这些记录在滚动时不会排序。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
</head>
<body>
<div id="container">
<div class="content" data-name="Peter" data-price="1000" data-location="US"><span class="productPriceForSorting">1000</span</div><br />
<div class="content" data-name="Willy" data-price="1200" data-location="Mexico"><span class="productPriceForSorting">1200</span</div><br />
<div class="content" data-name="Peter" data-price="2000" data-location="US"><span class="productPriceForSorting">2000</span</div><br />
<div class="content" data-name="Willy" data-price="800" data-location="Mexico"><span class="productPriceForSorting">800</span</div><br />
<div class="content" data-name="Willy" data-price="1300" data-location="Mexico"><span class="productPriceForSorting">1300</span</div><br />
<div class="content" data-name="Peter" data-price="800" data-location="US"><span class="productPriceForSorting">800</span</div><br />
</div>
</div>
<div id="prod"></div>
<button id="asc">sort by price asd</button>
下面是我的ajax滚动记录
<script type="text/javascript">
$(document).ready(function() {
var page=1;
<%String name1=(String)session.getAttribute("name");%>
var name2="<%=name1%>";
$(window).scroll(function(e)
{if ($(window).scrollTop()+ $(window).height() == $(document).height())
{page++;
$.ajax({ type : "Get",
url : "Someservlet",
datatype : "JSON",
contentType : 'application/json',
data : {pagenumber : page,
Pname : name2},
success : function(data) {
var data1 = data[0],
var len = data1.length;
for (var i = 0; i < len; i++) {
var name = "<div class=content data-name=" + data1[i].name + '' + " data-location=" + data1[i].location + ">"
+ "Peter" +"</div>"+"<div class=content data-name=" + data1[i].name + '' + " data-location=" + data1[i].location + ">"
+ "Willy" +"</div>";
$(name).appendTo("#prod");
}
}
});
}
});
});
</script>
下面是对
排序的代码<script type="text/javascript">
function sortByPrice(a, b) {
return $(a).find('.productPriceForSorting').text() > $(b).find(
'.productPriceForSorting').text();
}
function reorderEl1(el) {
var container = $('#container');
container.html('');
el.each(function() {
$(this).appendTo(container);
});
}
$(document).ready('#asc').click(function() {
reorderEl1($('.content').sort(sortByPrice));
});
</script>
请帮助我的家伙,如何排序滚动元素一旦排序被点击?
您的AJAX调用生成的记录如下:
var name = "<div class=content data-name=" + data1[i].name + '' + " data-location=" + data1[i].location + ">"
+ "Peter" +"</div>"+"<div class=content data-name=" + data1[i].name + '' + " data-location=" + data1[i].location + ">"
+ "Willy" +"</div>";
但是你的排序函数正在寻找不是由AJAX添加的.productPriceForSorting
元素。这就是你需要改变的。排序函数没有检测到.productPriceForSorting
,所以没有比较。
你的排序函数也没有正确比较,它应该是:
function sortByPrice(a, b) {
return parseInt($(a).find('.productPriceForSorting').text()) > parseInt($(b).find('.productPriceForSorting').text());
}
下面是固定排序的工作演示。Ajax是为您准备的。它一定含有<span class='"productPriceForSorting'">"+data1[i].price+"</span>
或类似的东西。
相关文章:
- 幻灯片滚动javascript不起作用
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 使用promise和mongoose对文档进行排序
- 将视口底部滚动到元素底部
- jQuery Lazy加载动画滚动
- 无限滚动,动态数据按上次修改状态排序
- JQuery UI在移动设备上可排序禁用滚动
- JQuery UI拖动到可排序状态而不滚动
- 执行列排序后,dgrid 无法保留滚动位置
- 当我使用视图而不是使用Famous的表面进行排序时,ScrollViewContainer不会滚动
- 使用ng可排序禁用页面滚动
- 在“多选”中对选项进行排序时,滚动条不会移动
- jQuery UI可排序:滚动整个页面以及容器
- jquery可排序,IE,和无限滚动
- 如何使动态JSP表按升序和降序可滚动和排序?
- jQueryUI可排序连接列表-当项目滚动到新列表中时,未触发Over事件
- 可排序分区内的滚动条
- 表排序器(莫蒂叉)/恢复正文滚动POS - 正确
- 对javascript中的ajax滚动内容进行排序
- 如何显示排序只有25个元素在无限滚动