繁体   English   中英

如何将json对象分配给数据表?

[英]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.monthsomething 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM