利用AJAX后台查询数据库返回json,前台生成表格

2 篇文章 0 订阅
1 篇文章 0 订阅

后台代码

public class DetailAjaxAction extends FrameAjaxCrassAction {

    @Override
    protected String doExecute(DataManager dm, PrintWriter out, HttpServletRequest request, SecurityForm form)
            throws Exception {
        String sid = request.getParameter("sid");


        String sql="select * from v_user_tables where nm_sid='"+sid+"'"; 
        DataObject dbo_result=dm.findByPrimaryKey(sql,null);
        String tableName=dbo_result.getString("TABLE_NAME");
        String sql1="select column_name from user_tab_columns where table_name='"+tableName+"'"; 
        String sql2="select * from  "+tableName;

        Collection colResult = dm.find(sql1, null);
        Collection colResult2 = dm.find(sql2, null);
        JSONArray jsa = new JSONArray();
        JSONArray jsa2 = new JSONArray();
        JSONObject jo2 = new JSONObject();
        try {
            jsa = JSONUtil.converToJsa(colResult);
            jsa2 = JSONUtil.converToJsa(colResult2);
            jo2.element("head",jsa);
            jo2.element("data",jsa2);
        } catch (Exception e) {
            // TODO: handle exception
        }
        out.write(jo2.toString());
        return null;
    }

}

前台

<body style="overflow: auto; width: 100%;" >
    <div>
        <html:form action="<%=url %>" styleId="dbaForm">
            <table id="xccs" class="tableContentStyle" width="98%" border=1
                cellpadding="5" cellspacing="0" align=center>
                <thead>
                <tr id="table"></tr>
                <tbody id="table2"></tbody>     
            </table>
        </html:form>
    </div>
    <script type="text/javascript">
    $(function() {
            $.post("dba/table/detailAjax.do?sid=<%=sid%>",function(result){

                var html='';
                var result=$.parseJSON(result);
                console.info(result);
                var head=result.head;
                var data=result.data;
                $.each(head, function(i,obj) {

                    html+='<th align="center"  nowrap>'+obj.COLUMN_NAME+'</th>';  //先循环表格标题
                });
                    $("#table").html(html);
                    html='';
                    $.each(data, function(j,dataobj) {
                        html+='<tr>';
                        $.each(head, function(i,obj) {
                            var col_nm=obj.COLUMN_NAME;  //先循环列,再循环行
                            html+='<td align="center" nowrap >'+dataobj[col_nm]+'</td>';   //json_key值为变量时,用中括号(dataobj[col_nm])表示
                        });
                    html+='</tr>'
                    });
                    html+=''
                    $("#table2").html(html);
              });
    });
    </script>
</body>
</html>
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值