0
通過查詢我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
?
謝謝你爲我工作,但在選擇時發送Ajax請求,第一次選擇只爲第二次選擇工作,它給出錯誤'DataTables警告:表id = tblData - 無法重新初始化DataTable。有關此錯誤的更多信息,請參閱http://datatables.net/tn/3' –