如何将json对象分配给数据表?
问题描述:
通过查询我fectched通过setter方法分配给对象的数据,并添加每个对象到列表中如何把它放到DataTable中如何将json对象分配给数据表?
这里是我的Java代码
try {
ps=cn.prepareStatement(" select month_year,count(*),sum(booking_amount),count(distinct user_id) from DAILYBOOKINGREPORT2 where yearr=? group by month_year order by MONTH_YEAR DESC");
ps.setString(1,y);
rs=ps.executeQuery();
while(rs.next())
{
String month_year="";
int no_of_tickets=0,no_of_agents=0;
float booking_amount=0;
int perDay_tickets=0;
String s="";
month_year=rs.getString(1);
System.out.println(month_year);
no_of_tickets=rs.getInt(2);
booking_amount=rs.getFloat(3);
no_of_agents=rs.getInt(4);
s= month_year.substring(4,6);
int last_num=Integer.parseInt(s);
if(last_num%2==0)
{
perDay_tickets= no_of_tickets/30;
}
else{
perDay_tickets= no_of_tickets/31;
}
MonthlyBeans obj=new MonthlyBeans();
// System.out.println("no of tickets"+no_of_tickets);
// System.out.println("perday"+perDay_tickets);
float avg_pricing=booking_amount/ no_of_tickets;
int per_agent=no_of_tickets/ no_of_agents;
//System.out.println("avg"+avg_pricing);
//System.out.println("per agent"+per_agent);
obj.setMonth(month_year);
obj.setUnique_users(no_of_agents);
obj.setNo_of_tickets(no_of_tickets);
obj.setBooking_amount(booking_amount);
obj.setPerAgent(per_agent);
obj.setPerday(perDay_tickets);
obj.setAvg_pricing(avg_pricing);
list.add(obj);
}
result = gson.toJson(list);
}
我的JSON对象容貌这样
Object
No_of_tickets: 253
avg_pricing :1034.0747
booking_amount:261620.89
month:"201405"
perday:15
perAgent:16
unique_users:15
这里是我的html
<table class="table-striped table-bordered" id="tblData"">
<thead >
<tr>
<th>month</th>
<th>No_of_tickets</th>
<th>booking_amount</th>
<th>avg_pricing</th>
<th>unique_users</th>
<th>perday</th>
<th>perAgent</th>
</tr>
</thead>
</table>
这里是我的javascript
$.ajax({
url:"Monthly_report",
data:"&t4="+ $("#slc").val(),
dataType:"json",
type:"get",
success:function(response){
console.log(response);
$('#tblData').dataTable({
data : response,
columns : [ { 'response' : 'response.month' },
{ 'response' : 'response.No_of_tickets' },
{ 'response' : 'response.booking_amount' },
{ 'response' : 'response.avg_pricing' },
{ 'response' : 'response.unique_users' },
{ 'response' : 'response.perday' }]
}) ;
}
});
请帮我这个我怎么可以分配对象数据到数据表我没有能够做到这一点response.month
或something else
?
答
数据表绑定列与数据。我想应答是你的json对象的数组。 第一次你可以在datatables docs找到一些解释。
第二次您无法绑定您的数据。您搜索将您的第一列绑定到属性response.month。但是您想要将其绑定到月。
我的目的,你这个解决方案:
$('#tblData').dataTable({
data : response,
columns : [
{'data' : "month" },
{'data' : "No_of_tickets" },
{'data' : "booking_amount" },
{'data' : "avg_pricing" },
{'data' : "unique_users" },
{'data' : "perday" }
]
});
我希望这可以帮助你。
注意:您可以管理ajax request with datatables。如果你想要,排序,分页或其他的东西,它会很有用。
谢谢你为我工作,但在选择时发送Ajax请求,第一次选择只为第二次选择工作,它给出错误'DataTables警告:表id = tblData - 无法重新初始化DataTable。有关此错误的更多信息,请参阅http://datatables.net/tn/3' –