1. 程式人生 > >json資料傳到前臺並解析展示成列表

json資料傳到前臺並解析展示成列表

因為某些原因,專案中突然需要做自己做個ajax非同步獲取資料後動態向表格中新增資料的頁面,網上找了半天都沒有 看到現成的,決定自己寫個例子

1、HTML頁面

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>檔案櫃取件列表</title>
    <script type="text/javascript" src="/js/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="/js/ai/ai-lib.js"></script>
    
</head>

<body>
<div class="main pusher">
    <form class="form_style">
        <div class="fields">
            <div class="div_1">
                <label for="lbl">取件標題</label>
                <input name="FileTitle" type="text" id="FileTitle">
            </div>

            <div class="div_l">
                <label>取件發文單位</label>
                <input name="comeDept" id="comeDept" placeholder="" type="text">
            </div>

            <div class="div_l">
                <label>投箱時間</label>
                <input type="text" maxlength="50" name="sendTime" id="sendTime" placeholder="">
            </div>
            <div class="div_l">
                <label>列印人單位</label> <input type="text" maxlength="50" name="printDept" id="printDept">     
            </div>
            <div>
                <label>列印人姓名</label>
               <input type="text" maxlength="50" name="printUser" id="printUser">
                <input type="button" value="查詢" id="btSearch" class="btn_search"/>
            </div>
        </div>
    </form>

    <div class="table-container">
        <table class="ui nine column table celled table-result" id="table-result">
            <thead>
            <tr>
                <th>hotelSeq</th>
                <th>檔案標題</th>
                <th>條碼編號</th>
                <th>發文單位</th>
                <th>緊急程度</th>
                <th>份數</th>
                <th>密級</th>
                <th>投箱時間</th>
                <th>備註</th>
            </tr>
            </thead>
            <tbody id="tbody-result">
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

2、filebox-print.jsp
$(function () {
    $('#btSearch').click(function () {
        var FileTitle= $('#FileTitle').val();
        var comeDept= $('#comeDept').val();
        var sendTime= $('#sendTime').val();
        var printDept = $('#printDept').val();
        var printUser=$('#printUser').val();
        var tbody=window.document.getElementById("tbody-result");

        $.ajax({
            type: "post",
            dataType: "json",
            url: "<%=path%>/filebox/fileBox!getToDoFileBoxList.action",
            data: {
                FileTitle: FileTitle,
                comeDept: comeDept,
                sendTime: sendTime,
                printDept: printDept,
                printUser:printUser 
            },
            success: function (msg) {
                if (msg.ret) {
                    var str = "";
                    var data = msg.data;

                    for (i in data) {
                        str += "<tr>" +
                        "<td>" + data[i].fileTitle+ "</td>" +
                        "<td>" + data[i].fileCode+ "</td>" +
                        "<td>" + data[i].comeDept + "</td>" +
                        "<td>" + data[i].fileEmerg+ "</td>" +
                        "<td>" + data[i].alreadyCount+ "</td>" +
                        "<td>" + data[i].fileSecret+ "</td>" +
                        "<td>" + data[i].sendTime + "</td>" +
                        "<td>" + data[i].remark+ "</td>" +
                        "<td>" + data[i].fileboxId+ "</td>" +
                        "</tr>";
                    }
                    tbody.innerHTML = str;
                }
            },
            error: function () {
                alert("查詢失敗")
            }
        });
    });
});
做完之後感覺瞬間就踏實了不少,這個禮拜可以好好休息了。俺接觸這個又學到了一點前臺的知識了。