[英]how to assign json object to the datatable?
通過查詢,我獲取了使用setter方法分配給對象的數據,並將每個對象添加到列表中,如何將其放入數據表中
這是我的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' }]
}) ;
}
});
請幫我這個問題,如何將對象數據分配給DATATABLE我無法做到response.month
或something else
?
數據表將列與數據綁定。 我想響應是您的json對象的數組。 第一次,您可以在datatables文檔中找到一些解釋。
第二次您綁定數據失敗。 您搜索將第一列綁定到屬性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請求 。 如果您想要,進行排序,分頁或其他操作,它將很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.