![](/img/trans.png)
[英]sort json by sorting arraylist in javascript/jquery
[英]How to sorting with json & jquery?
我使用ajax creat html,像這樣
html += "<div class=\"cloumns rentprice\">" + data[i].car_price + "</div>";
我想從此動態html捕獲json的索引使用值,
然后排序
我的排序代碼是這樣的
$("#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 get data[i].car_price
,該怎么辦?有任何想法嗎?
此代碼使用“ rentprice”類獲取第一項(eq(0)):
$(".rentprice").eq(0).text()
此代碼對每個項目都執行以下操作:
$.each($(".rentprice"), function(){
alert($(this).text());
});
我建議您使用庫來處理數據可視化,如strockoutjs.com 。 它為數據分類和編輯提供了更好,更穩定的解決方案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.