Ajax實現資料庫選項組即時檢索

來源:互聯網
上載者:User

瀏覽的 JSP代碼

<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ include file="../common/newtag.jsp" %>
<%@page import="java.util.Calendar"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Map"%>
<%@page import="com.database.operate.DataManageFactory"%>
<%@page import="com.database.sql.CLASSTYPE"%>
<%@page import="com.datacollect.FloatReportVO"%>
<%
String excelnameschecked = request.getParameter("excelname");
List exclenamescheckedlist = new ArrayList();
if (excelnameschecked != null && !"".equals(excelnameschecked)) {
 String[] excelnamescheckedarray = excelnameschecked.split(",");
 for (int i=0; i<excelnamescheckedarray.length; i++) {
  exclenamescheckedlist.add(excelnamescheckedarray[i]);
 }
}

List excelnames = null;
String pyear = request.getParameter("year");
String pmonth = request.getParameter("month");
if(pyear != null && pmonth != null)
try {
 excelnames = DataManageFactory.getOperater().query(CLASSTYPE.REPORTMANAGE,"QUERY_ZD_FALLDATA_REPORTS",null);
} catch (Exception e) {}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>right</title>
    <link href="<%=request.getContextPath()%>/css/main.css" rel="stylesheet" type="text/css"/>
    <link href="<%=request.getContextPath()%>/js/dateJs/calendar.css" rel="stylesheet" type="text/css"/>
</head>
<body onload="getDcstatusByExcelnames();">
<table style="border:#d7d7d7 solid 1px " height="100%" width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td height="18">
            <div style="background:url(../c_img/win_bg.jpg); height:18px;
    border-bottom:#999 solid 1px; margin:auto 0">
                <span style="float:left; margin-left:5px; color:#545487; font-weight:bold; margin-top:2px; padding:0px; display:block">報表列表</span>
                <span style=" float:right; color:#545487; font-weight:bold; margin-right:5px; margin-top:4px;margin-top:2px; padding:0px; display:block; font-size:0.7em;font-size:0.9em;cursor:pointer"></span>
            </div>

        </td>
    </tr>
    <tr height="20">
      <td>
       <div class="tools_div">
        <ul class="in all h">
            帳期選擇 :
      <select id="year">
      <%
      Calendar c = Calendar.getInstance();
      Date curDate =new Date();
      c.setTime(curDate);
    int now = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int startyear = now - 5 ;
    int maxyear = now + 10 ;
    int yearInt = now ;int monthInt = month;
                                if(pyear!=null){ yearInt = Integer.parseInt(pyear);}
                                if(pmonth!=null){ monthInt = Integer.parseInt(pmonth.substring(0,1).equals("0")?pmonth.substring(1) : pmonth);}
      for(int i = startyear ; i <= maxyear ; i++) {%>
       <option value="<%=i%>"  <%if( i == yearInt){ out.print("selected");} %> ><%=i%></option>
      <%}%>
      </select>
      年
      <select id="month">
      <%for(int i = 1 ; i < 10 ; i++){%>
      <option <%if( i == monthInt){ out.print("selected");} %> value="<%="0" + i %>"><%="0" + i %></option>
      <%}%>
      <%for(int i = 10 ; i < 13 ; i++){%>
      <option <%if( i == monthInt){ out.print("selected");} %> value="<%=i%>"><%=i%></option>
      <%}%>
      </select>
      月
           </ul>
           <ul class="in ok h">
              <li><span onclick="openWindow()">確定</span></li>
           </ul>
          </div>
       </td>
      </tr>
    <tr>
        <td id="main_td" width="100%" height="85%">
            <div style="width: 100%;height: 100%;overflow: auto">
            <table id="" class="table_style tr_h" border="0" cellspacing="0" cellpadding="0">
                <colgroup>
                    <col class="vzebra-one" width="20px"/>
                    <col class="vzebra-odd"/>
                    <col class="vzebra-one" width="20px"/>
                </colgroup>
                <tr>
                    <td></td>
                    <td>報表名稱</td>
                    <td>切入</td>
                    <td>狀態</td>                 
                </tr>
        <%
    if(excelnames!=null){
     int rownumber = 1 ;
     for(int i = 0 ; i < excelnames.size(); i++){
      Map excelnamesMap = (Map)excelnames.get(i);
    %>
       <tr>
        <td><input type="checkbox" name="TableNameCheckbox" tableName="<%=excelnamesMap.get("EXCEL_NAME")%>" <%=exclenamescheckedlist.contains((String)excelnamesMap.get("EXCEL_NAME")) ? "checked='checked'" : ""%>></td>
        <td><a href="#" onclick="changed('<%=excelnamesMap.get("EXCEL_NAME")%>');"><%=excelnamesMap.get("EXCEL_NAME")%></a></td>
        <td>0</td>
        <%
         String dcstatus = (String)excelnamesMap.get("COLLECT_STATUS");
         if ("0".equals(dcstatus)) {
          dcstatus = "<font color=\"#FF9900\">未執行</font>";
         }
         if ("1".equals(dcstatus)) {
          dcstatus = "<font color=\"#33CC00\">匯總成功</font>";
         }
         if ("2".equals(dcstatus)) {
          dcstatus = "<font color=\"#FF0000\">匯總失敗</font>";
         }
        %>
                <td><div id="<%=excelnamesMap.get("EXCEL_NAME")%>">123</div></td>
       </tr>
       <%
         }
        }
       %>
                       
            </table>
            </div>
        </td>
    </tr>
        <tr height="30px">
        <td style="background:#e4e4ec;">
<span class="tools_div">
  <ul onclick="selectAll()" style="cursor:hand">
    <%--<li><span>全選</span></li>--%>
      <span><img src="../c_img/44.PNG" alt=""></span>
</ul>
<ul onclick="invertSelect()" style="cursor:hand">
    <%--<li><span>反選</span></li>--%>
    <span><img src="../c_img/55.PNG" alt=""></span>
</ul>
<ul style="cursor:hand">
 <span><input type="button" value="匯總" onclick="collect()" style="height:25px;"/></span>
<ul>
</span>
        </td>
    </tr>
</table>
</body>
<script language="javascript">
    function openWindow(){
    var month = document.getElementById('month').value;
    if(month == null){
    month = "<%=pmonth%>";
   }
    var year = document.getElementById('year').value;
       if(year == null){
    year = "<%=pyear%>";
   }
    window.location.href = './dataCollectLeft.jsp?year=' + year + '&month=' + month;
    }
    function collect(){
     var tableNames = [];
        var chs = document.getElementsByName("TableNameCheckbox");
        var len = chs.length;
        var i = 0;
        for (i = 0; i < len; i++) {
            if (chs[i].checked) {
                tableNames.push(chs[i].tableName);
            }
        }
        if (tableNames.length == 0) {
            alert("請選擇報表");
            return;
        }
        var repNames = tableNames.join(',');
     if (confirm("您確認要對勾選報表進行匯總嗎?"))
           window.location.href = 'collectData.jsp?year=<%=pyear%>&month=<%=pmonth%>&excelname=' + repNames;
           alert("匯總計算需1-3分鐘");
           window.location.href ='dataCollectLeft.jsp?year=<%=pyear%>&month=<%=pmonth%>&excelname=' + repNames;
    }
 function changed(rptname){
     var commend = rptname;
     parent.ed_main.location = "dataCollectMain.jsp?excelname=" + commend + "&year=<%=pyear%>&month=<%=pmonth%>";    
 }
 function selectAll() {
        var chs = document.getElementsByName("TableNameCheckbox");
        var len = chs.length;
        var i = 0;
        for (i = 0; i < len; i++) {
            chs[i].checked = true;
        }
    }

    function invertSelect() {
        var chs = document.getElementsByName("TableNameCheckbox");
        var len = chs.length;
        var i = 0;
        for (i = 0; i < len; i++) {
            chs[i].checked = !(chs[i].checked);
        }
    }
            function go(name, year, month, org) {
            // alert(name);
            parent.ed_main.location = "fetchMain.jsp?name=" + name + "&year=" + year + "&month=" + month + "&org=" + org;
            parent.ed_banner.location = "fetchTop.jsp?name=" + name + "&year=" + year + "&month=" + month + "&org=" + org;
        }
       
//建立ajax對象
var ajax =new function()
{
 this._url = "./dcstatus.jsp";

 //用於建立XMLHttpRequest對象
    this.createXmlHttp=function() {
        var xmlHttp = null;
        //根據window.XMLHttpRequest對象是否存在使用不同的建立方式
        if(window.XMLHttpRequest){
    xmlHttp = new XMLHttpRequest();
  }else if(window.ActiveXObject){
    xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
  }else if(window.ActiveXObject){
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
  }else if(window.ActiveXObject){
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  }
        return xmlHttp;
    }
   
 //得到匯總狀態
 this.getCdstatus = function(exclename)
 {
    var bbname = exclename;
          var xmlHttp = this.createXmlHttp();  //建立XmlHttpRequest對象
          xmlHttp.onreadystatechange = function()
    {
            if (xmlHttp.readyState == 4)
   {
    if(xmlHttp.status == 200){
            var xmldoc = xmlHttp.responseXML;
            var Node = xmldoc.getElementsByTagName("dcstatus");
            document.getElementById(exclename).innerHTML = Node(0).firstChild.nodeValue; //得到XML返回資訊
            setTimeout("ajax.getCdstatus('"+exclename+"')", "5000"); //每隔5秒查詢1次
       } else{
            alert("伺服器故障");
       }
             }
           }

   var url = this._url + "?exclename=" + exclename;
   xmlHttp.open("GET",encodeURI(encodeURI(url)), true);
      xmlHttp.send(null);
 }
}

//根據勾選報表得到匯總狀態
function getDcstatusByExcelnames() {
    var tableNames = [];
    var chs = document.getElementsByName("TableNameCheckbox");
    var len = chs.length;
    var i = 0;
    for (i = 0; i < len; i++) {
        if (chs[i].checked) {
          tableNames.push(chs[i].tableName);
        }
    }
   
    <%
     if (excelnameschecked != null && !"".equals(excelnameschecked)) {
    %>
     tablenamestr = "<%=excelnameschecked%>";
     tableNames = tablenamestr.split(",");
    <%
        }
    %>
      
  for (i=0; i<tableNames.length; i++) {
   ajax.getCdstatus(tableNames[i]);  
    }         
}      
</script>
</html>

 

檢索資料庫並產生XML的JSP代碼

<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@page import="com.datacollect.FloatReportVO"%>
<%
 String exclename = request.getParameter("exclename");
 FloatReportVO frvo = new FloatReportVO(exclename);
 String dcstatus = frvo.getCOLLECT_STATUS();
 if ("0".equals(dcstatus)) {
  dcstatus = "<font color=\"#FF9900\">未執行</font>";
 }
 if ("1".equals(dcstatus)) {
  dcstatus = "<font color=\"#33CC00\">匯總成功</font>";
 }
 if ("2".equals(dcstatus)) {
  dcstatus = "<font color=\"#FF0000\">匯總失敗</font>";
 } 
 response.setContentType("text/xml;charset=UTF-8");
    response.setHeader("Cache-Control","no-cache");
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    sb.append("<dcstatus>");
    sb.append("<![CDATA[");
    sb.append(dcstatus);
    sb.append("]]>");
    sb.append("</dcstatus>");
    response.getWriter().write(sb.toString());
%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.