如何使用json和jquery进行排序
How to sorting with json & jquery?
>我使用ajax creat html,像这样
html += "<div class='"cloumns rentprice'">" + data[i].car_price + "</div>";
我想捕获 json 的值用于索引表单这个动态 html,
然后排序,
我的排序代码是这样的
$("#rentPrice").click(function(){
var $this=$(this);
$("#rentPrice").not($this)
if ($this.hasClass("up")){
$this.removeClass().addClass("down");
}else{
$this.removeClass().addClass("up");
}
var index = $(".rentprice").text();
var direction =$this.attr("class");
sort(index,direction);
show(json);
});
function sort(index, direction){
json.sort(function(a,b){
var keys = Object.keys(a);
if (direction == "up"){
if (a[keys[index]] > b[keys[index]]){
return 1;
}else if (a[keys[index]] < b[keys[index]]){
return -1;
}else{
return 0;
}
}else{
if (a[keys[index]] < b[keys[index]]){
return 1;
}else if (a[keys[index]] > b[keys[index]]){
return -1;
}else{
return 0;
}
}
});
}
});
,但答案是错误的,因为var index = $(".rentprice").text();
无法获得索引data[i].car_price
,
我不知道怎么get data[i].car_price
表单html,怎么办?知道吗?
此代码获取带有 "rentprice" 类的第一项 ( eq(0) ):
$(".rentprice").eq(0).text()
此代码对每个项目执行一些操作:
$.each($(".rentprice"), function(){
alert($(this).text());
});
我建议您使用库来处理像 knockoutjs.com 这样的数据可视化。它为数据排序和编辑提供了更好、更稳定的解决方案
相关文章:
- jQuery UI可排序-多连接列表拖动
- jquery Onclick函数带有导致双击的回调排序函数
- 使用Jquery Issue垂直重新排序Divs
- 免费的JQuery表排序插件(带搜索)
- 如何删除项目的可排序 JQuery
- 只对表的几行进行排序 - jquery/ javascript
- 排序 Jquery 返回的 HTML 元素
- 在可排序 - jQuery 中按标题查找元素
- 给定两个连接的可排序 JQuery 列表,我怎么知道元素被放在哪个容器上
- 可通过Ajax从回显列表中排序jQuery列表
- 重新排序JQuery Masonry中的框
- I'm试图制作一个带有可排序jquery函数的add-remove输入框来对动态创建的输入框进行排序
- 按字母顺序排序jquery结果
- 组排序JQuery列表元素
- 按字母顺序排序Jquery列表
- 用余烬排序jQuery UI后删除项目
- 排序jQuery's自动完成结果
- 带有按钮的可排序jQuery UI列表
- 是否可以对同位素进行列排序?(jQuery库)
- 重新排序jQuery "sortable"问题