ajax請求到後臺數據,前臺不用拼接字符串,一樣顯示到頁面

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!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>無標題文檔</title>
<script type="text/javascript" src="<%=path%>/js/jquery-1.11.1.min.js"></script>

<style>
</style>
<script>
    $(document).ready(function() {
        //搜索按鈕(全部搜索和按條件搜索)
        $("#searchBtn").click(function(){
            var itemname = $("#itemname").val();
            var year = $("#year option :selected ").val();
            //當輸入框爲空,年份沒選擇,就爲搜索全部數據
            if(itemname =="" && year == undefined){
                $.get("<%=request.getContextPath()%>/searchAll",
                         function(data){
                               $.each(data.tasklist ,function(i,obj){
                                   /* 獲取數據,添加給模板表格 */
                                   $("#id").text(obj.id);
                                   $("#name").text(obj.name);
                                   $("#type").text(obj.type);
                                   $("#cost").text(obj.cost);
                                   $("#state").text(obj.state);
                                   $("#date").text(obj.date);
                                /*複製模板表格,並插入到指定位置*/
                                   $("#temp_tr").clone(true).insertAfter("#is_header");
                               });
                      });
            }
            
        });
    });
</script>

</head>

<body>

<!-- 表格模板---用於處理獲取後臺數據後生成表格, -->
<div id="temp" style="display: none;">
        <table id="temp_table">
            <tr id="temp_tr">
                <td id="id" width="5%" height="25" align="center" bgcolor="#EFFBFE">aa</td>
                <td id="name" align="center" bgcolor="#EFFBFE">bb</td>
                <td id="type" colspan="2" align="center" bgcolor="#EFFBFE">cc</td>
                <td id="cost" align="center" bgcolor="#EFFBFE">dd</td>
                <td id="date" align="center" bgcolor="#EFFBFE">ff</td>
                <td id="state" align="center" bgcolor="#EFFBFE">ee</td>
                <td align="left" bgcolor="#EFFBFE">
                    <div align="left" style="padding-left: 5px">
                        <div align="center">
                            <input name="edit" type="button" value="編輯" /> &nbsp;
                            <input name="delete" type="button" value="刪除"/>
                        </div>
                    </div>
                </td>
            </tr>
        </table>
</div>


<!-- 以下是顯示到頁面的內容-->
    <table width="99%" height="25" border="0" align="center"
        cellpadding="0" cellspacing="0" style="margin-bottom: 4px;">
        <tr>
            <td width="17" background="p_w_picpaths/bj4.gif"><img
                src="Picture/r.gif" width="16" height="16" /></td>
            <td width="466" background="p_w_picpaths/bj4.gif">張宇(學院申報者):你好!歡迎光臨,您上次登錄時間是:2015-09-20
                23:15:15</td>
            <td width="162" align="center" background="p_w_picpaths/bj4.gif"></td>
        </tr>
    </table>
    <table width="99%" border="0" align="center" cellpadding="0"
        cellspacing="0">
        <tr>
            <td width="1%" align="left" background="p_w_picpaths/b2.jpg"><img
                src="Picture/b1.jpg" width="10" height="26" /></td>
            <td width="68%" background="p_w_picpaths/b2.jpg"><table width="124"
                    border="0" align="left" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="20" align="left"><img src="Picture/tz.gif"
                            width="10" height="16" /></td>
                        <td width="104" align="left" class="biao">項目搜索</td>
                    </tr>
                </table></td>
            <td width="31%" align="right" background="p_w_picpaths/b2.jpg"><img
                src="Picture/b3.jpg" width="9" height="26" /></td>
        </tr>
    </table>
    <table width="99%" border="0" align="center" cellpadding="3"
        cellspacing="1" bgcolor="#AEDEF4">
        <tr>
            <td align="center" bgcolor="#E2F7FE">
                <form id="form1" name="form1" method="post"
                    action="<%=path%>/searchByYear">
                    <table width="50%" border="0" cellspacing="1" cellpadding="3">
                        <tr>
                            <td width="10%" height="25" align="center">項目名稱:</td>
                            <td width="14%" align="left"><label> <input
                                    name="itemname" type="text" id="itemname" size="20" />
                            </label></td>
                            <td width="6%" align="center">年份:</td>
                            <td width="14%" align="left"><select name="year" id="year">
                                    <option value="">全部</option>
                                    <option value='2009'>2009</option>
                                    <option value='2010'>2010</option>
                                    <option value='2011'>2011</option>
                                    <option value='2012'>2012</option>
                                    <option value='2013'>2013</option>
                                    <option value='2008'>2008</option>
                                    <option value='2014'>2014</option>
                                    <option value='2015'>2015</option>
                                    <option value='2016'>2016</option>
                                    <option value='2017'>2017</option>
                            </select></td>
                            <td width="8%"><input type="button" id="searchBtn"
                                name="searchBtn" value="搜索" /></td>
                        </tr>
                    </table>
                </form>
            </td>
        </tr>
    </table>

    <table width="50%" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td height="5"></td>
        </tr>
    </table>
    <table width="99%" border="0" align="center" cellpadding="0"
        cellspacing="0">
        <tr>
            <td width="1%" align="left" background="p_w_picpaths/b2.jpg"><img
                src="Picture/b1.jpg" width="10" height="26" /></td>
            <td width="68%" background="p_w_picpaths/b2.jpg"><table width="124"
                    border="0" align="left" cellpadding="0" cellspacing="0">
                    <tr>
                        <td width="20" align="left"><img src="Picture/tz.gif"
                            width="10" height="16" /></td>
                        <td width="104" align="left" class="biao">項目列表</td>
                    </tr>
                </table></td>
            <td width="31%" align="right" background="p_w_picpaths/b2.jpg"><img
                src="Picture/b3.jpg" width="9" height="26" /></td>
        </tr>
    </table>
    <!-- 這裏是獲取後臺數據,並畫出顯示錶格 -->
    <table id="is_table" width="99%" border="0" align="center"
        cellpadding="3" cellspacing="1" bgcolor="#AEDEF4"
        style="border: 1px solid #AEDEF4">

        <tr id="is_header">
            <td width="5%" height="25" align="center" bgcolor="#EFFBFE">ID</td>
            <td align="center" bgcolor="#EFFBFE">項目名稱</td>
            <td colspan="2" align="center" bgcolor="#EFFBFE">項目類型</td>
            <td align="center" bgcolor="#EFFBFE">申請經費</td>
            <td align="center" bgcolor="#EFFBFE">提交時間</td>
            <td align="center" bgcolor="#EFFBFE">狀態</td>
            <td align="center" bgcolor="#EFFBFE">管理操作</td>
        </tr>
<!-- 克隆的模板表格,粘貼到這裏 -->
        <tr>
            <td height="25" bgcolor="#FFFFFF">&nbsp;</td>
            <td colspan="6" bgcolor="#FFFFFF"><span class='pagelink'>首頁</span>
                <span class='pagelink'>上一頁</span> <span class='pagelink'>下一頁</span>
                <span class='pagelink'>尾頁</span> &nbsp;&nbsp;轉到第&nbsp; <input
                id='jumpSelect' type='text' size='2' value='1' />頁</td>
            <td bgcolor="#FFFFFF">

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