【发布时间】:2018-03-23 07:07:15
【问题描述】:
我正在尝试使用 jQuery 动态生成列并将其添加到表中,即使在 javascript 控制台上未检测到错误,也不会出现列。
我想做的过程
get JSON for the table -> generate the HTML -> remove existing columns to update -> append the new data to the table
## 预期生成的 HTML
<table id="buy_order" class="table">
<thead>
<tr>
<th scope="col">Price of buy orders(BTC)</th>
<th scope="col">Amount of buy orders(<%=@currency_id%>)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="table-default">
<div class="parent">
<div class="overlay bg-danger" style="width:10%"> </div>
0.003
</div>
</td>
<td class="table-default">1</td>
</tr>
</tbody>
</table>
JSON
[["0.003",1]]
第一个是价格,第二个是金额
javascript
<script>
$(function(){
setInterval(function(){
$(function() {
$.getJSON("http://localhost:3000/buys/order_book?currency_id=ryu", function(data){
var buy_order = "";
for (i = 0; i < data.length; i++) {
buy_order += "<tr>\n";
for (j = 0; j < data[i].length; j++) {
width =data[i][1]*10;
buy_order += $("<td>").addClass("table-default");
buy_order += $("<div>").addClass("parent")+$(($("<div>").addClass("overlay bg-success"))).css("width",width+"%")+" "+"</div>"+data[i][j]+"</div>";
buy_order += "</td>\n";
}
buy_order += "</tr>\n";
console.log(buy_order)
}
buy_order=$(buy_order).hide().fadeIn(1000);
$("#buy_order").empty();
$("#buy_order").append(buy_order);
});
});
},5000);
});
</script>
谢谢
【问题讨论】:
-
这个json响应[["0.003",1]]的响应是固定的还是会改变的?
-
它可以改变或不变
-
能否请您更改完整的 JSON。你也可以改变JSON格式吗?
-
我可以,但是您希望我提供什么结构的 JSON?是不是像 {["price",0.003,"amount":1]}
-
您可以做的是创建一个提供以下输出的数组。 ( [0] => { "价格" => 0.003, "数量" => 1 }, [1] => { "价格" => 0.006, "数量" => 2 })
标签: javascript jquery html erb