js a標籤 href與button onclick 不同

js a標籤 href與button onclick 不同,在方法調用上存在差異

使用herf與button的onclick導出Excel,在兩個不同的form中,使用同一個form的參數

--HTML--

<div title="數據展示表格" id="tab_records" style='width: 99%;'>
        <div id="toolbar">
            <form id="searchform" class="layui-form" style="font-size: 12px;">
                <div>
                    組織機構 <input id="sm_like_orgname" name="sm_like_orgname" />
                </div>

                <div>
                    關鍵字 <input id="sm_like_keyword" name="sm_like_keyword" />
                </div>
                日期
                <div class="layui-input-inline">
                    <input type="text" id="sm_to1_createtime" name="sm_to1_createtime" class="date"
                         />
                </div>
                -
                <div class="layui-input-inline">
                    <input type="text" id="sm_to2_createtime" name="sm_to2_createtime" class="date"  />
                </div>
                <div class="queryBtnClass">
                    <a href="javascript:getData();"> <i
                        class="fa fa-search #575D66 "></i> 查詢
                    </a>
                    <a href="javascript:exports()"> <i class="fa fa-plus #575D66 "></i>
                        導出Excel
                    </a>
                </div>
                <input type="hidden" id="sm_orderby" name="sm_orderby" value="" />
            </form>
        </div>
    </div>

    <div id="div_form" align="center" style="display: none;">
        <form action=""  class="layui-form">
            <div style="margin-top: 15px;text-align: right;margin-right: 45px;">
                <input  type="hidden" id="exportReportOrgid" >
                <button class="layui-btn" οnclick="exportReport()">導出報表</button>
            </div>
            <div class="layui-form">
                <table class="layui-table" >
                    <caption style="font-size: 18px;" id="titleReport"></caption>
                    <caption style="font-size: 14px;text-align: right;margin-right: 20px;"  id="timeReport"></caption>
                    <thead>
                        <tr>
                            <th>計劃名稱</th>
                            <th>企業名稱</th>
                            <th>檢查事由</th>
                            <th>檢查結果</th>
                            <th>整改情況</th>
                            <th>負責人</th>
                            <th>督辦人</th>
                            <th>督辦負責人</th>
                            <th>整改意見</th>
                            <th>整改結果</th>
                            <th>驗收情況</th>
                            <th>行政處罰</th>
                            <th>案卷號</th>
                        </tr>
                    </thead>
                    <tbody id="report">
                        <!-- <tr>
                            <td rowspan="2">2019年安全生產監督檢查計劃</td>
                            <td>安河橋有限公司</td>
                            <td>根據檢查計劃進行檢查</td>
                            <td>發現了企業存在不安全記錄</td>
                            <td>整改中</td>
                            <td>張三</td>
                            <td>張三</td>
                            <td>張三是</td>
                            <td>增加滅火器</td>
                            <td>符合整改情況</td>
                            <td>合格</td>
                            <td>已處罰</td>
                            <td>1201254852</td>
                        </tr> -->
                    </tbody>
                </table>
            </div>
        </form>
    </div>

-- js --

//導出 Excel數據
var exports = function(){
    var url = basePath + '/jd/situationanalysis/sendout';
    var formData = com.weikun.bfw.common.form.serializeForm('searchform');
    $.ajax({
        type:"post",
        url:url,
        data:formData,
        dataType:"json",
        complete:function(res){
            console.log(res.responseText);
            if(res.status == "200"){
                var url1 = basePath + '/jd/situationanalysis/download?name='+res.responseText;
                window.location.href = url1;
            }
        }
    });
    
}
//導出 Excel數據 某個組織機構的報表
var exportReport = function(){
    var name;
    var url = basePath + '/jd/situationanalysis/sendoutByOrg';
    var formData = com.weikun.bfw.common.form.serializeForm('searchform');
    var orgid = $('#exportReportOrgid').val();
    formData.sm_eq_orgid = orgid;
    $.ajax({
        type:"post",
        url:url,
        data:formData,
        async:false,
        dataType:"json",
        complete:function(XMLHttpRequest, textStatus){
            if(textStatus == "success"){
                name = XMLHttpRequest.responseText;
                name = name.substring(1,name.length-1);
            }
        }
    });
    window.location.href = basePath + '/jd/situationanalysis/downloadByOrg?orgid='+orgid+'&name='+name; 
    window.event.returnValue=false;
}

-- Action --

 

/**
     * 導出數據
     * @param sm
     * @param model
     * @param session
     * @param request
     * @throws Exception
     */
    @RequestMapping("sendout")
    @ResponseBody
    public String sendout(SituationanalysisSM sm, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String msg = "OK";
        long count = 0l;
        String name = "";
        List<Situationanalysis> list = new ArrayList<Situationanalysis>();
        try {
            list = situationanalysisService.listAll(sm, null, model);
            count = (long) list.size();
            //生成excel並下載
            name = situationanalysisService.createExcel(sm,list,request,response);
            
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return name;
    }

/**
     * 導出數據
     * @param sm
     * @param model
     * @param session
     * @param request
     * @throws Exception
     */
    @RequestMapping("sendoutByOrg")
    @ResponseBody
    public void  sendoutByOrg(SituationanalysisSM sm, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String msg = "OK";
        long count = 0l;
        String name = "";
        List<Situationanalysis> list = new ArrayList<Situationanalysis>();
        try {
            list = situationanalysisService.listByOrgid(sm, model);
            count = (long) list.size();
            //生成excel並下載
            name = situationanalysisService.createExcelByOrg(sm, list, request, response);
            response.setContentType("text/html;charset=UTF-8");//這些設置必須要放在getWriter的方法之前,
            response.getWriter().print(JSON.toJSONString(name));
            
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章