JQuery Ajax動(dòng)態(tài)生成Table表格
前言:
本示例大概功能是前臺(tái)通過JQuery的Ajax調(diào)用一般處理程序(Handler),獲取表格需要顯示的信息,然后轉(zhuǎn)換成json格式返回給前臺(tái),前臺(tái)獲取到數(shù)據(jù)后循環(huán)構(gòu)建表格的行,最好把行附加到表里。
目標(biāo):
a 熟悉簡(jiǎn)單JQuery Ajax的使用
b 了解如何構(gòu)造基本的Json格式的數(shù)據(jù)(構(gòu)建Json也可以通過第三方的dll)
c 熟悉下handler的基本用法
1 簡(jiǎn)單效果圖

2 前臺(tái)代碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DialogAjax.aspx.cs" Inherits="JQueryTest_DialogAjax" %>
<!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 runat="server">
<title></title>
<link href="../JQueryUi/jquery-ui-1.8.5.custom.css" rel="stylesheet" type="text/css" />
<script src="../JQueryUi/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="../JQueryUi/jquery-ui-1.8.5.custom.min.js" type="text/javascript"></script>
<style type="text/css">
#divTb
{
width:800px;
border:1px solid #aaa;
margin:0 auto;
}
.even{background:#CCCCCC;}
.odd{background:#FFFFFF;}
</style>
<script type="text/javascript">
//獲取發(fā)布模塊類型
function getModuleInfo() {
$.ajax({
type: "GET",
dataType: "json",
url: "../Handler/TestHandler.ashx?Method=GetModuleInfo",
//data: { id: id, name: name },
success: function(json) {
var typeData = json.Module;
$.each(typeData, function(i, n) {
var tbBody = ""
var trColor;
if (i % 2 == 0) {
trColor = "even";
}
else {
trColor = "odd";
}
tbBody += "<tr class='" + trColor + "'><td>" + n.ModuleNum + "</td>" + "<td>" + n.ModuleName + "</td>" + "<td>" + n.ModuleDes + "</td></tr>";
$("#myTb").append(tbBody);
});
},
error: function(json) {
alert("加載失敗");
}
});
}
$(function() {
getModuleInfo();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="divTb">
<table id="myTb" style=" width:100%">
</table>
</div>
</form>
</body>
</html>
3 Handler代碼
<%@ WebHandler Language="C#" Class="TestHandler" %>
using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using DataDAL;
using DataEnity;
public class TestHandler : IHttpHandler {
HttpRequest Request;
HttpResponse Response;
public void ProcessRequest (HttpContext context) {
//不讓瀏覽器緩存
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
context.Response.AddHeader("pragma", "no-cache");
context.Response.AddHeader("cache-control", "");
context.Response.CacheControl = "no-cache";
context.Response.ContentType = "text/plain";
Request = context.Request;
Response = context.Response;
string method = Request["Method"].ToString();
System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(method);
methodInfo.Invoke(this, null);
}
public void GetModuleInfo()
{
StringBuilder sb = new StringBuilder();
string jsonData = string.Empty;
List<Module> lsModule = ModuleDAL.GetModuleList();
sb.Append("{\"Module\":[");
for (int i = 0; i < lsModule.Count; i++)
{
jsonData = "{\"ModuleNum\":" + "\"" + lsModule[i].ModuleNum + "\"" + ",\"ModuleName\":" + "\"" + lsModule[i].ModuleName + "\"" + ",\"ModuleDes\":" + "\"" + lsModule[i].ModuleDes + "\"" + "},";
sb.Append(jsonData);
}
if (lsModule.Count > 0)
sb = sb.Remove(sb.Length - 1, 1);
sb.Append("]}");
Response.Write(sb);
}
public bool IsReusable
{
get {
return false;
}
}
}
以上代碼超簡(jiǎn)單吧,JQuery Ajax動(dòng)態(tài)生成Table表格的內(nèi)容就全部完成了,希望對(duì)大家有所幫助。
- jQuery實(shí)現(xiàn)獲取table表格第一列值的方法
- jQuery對(duì)table表格進(jìn)行增刪改查
- 基于JQuery的動(dòng)態(tài)刪除Table表格的行和列的代碼
- JQuery 動(dòng)態(tài)生成Table表格實(shí)例代碼
- 使用jQuery操作HTML的table表格的實(shí)例解析
- jQuery實(shí)現(xiàn)Table表格隔行變色及高亮顯示當(dāng)前選擇行效果示例
- jQuery實(shí)現(xiàn)table表格checkbox全選的方法分析
- jQuery+CSS實(shí)現(xiàn)的table表格行列轉(zhuǎn)置功能示例
- jQuery實(shí)現(xiàn)table表格信息的展開和縮小功能示例
- jQuery動(dòng)態(tài)操作表單示例【基于table表格】
- jQuery實(shí)現(xiàn)為table表格動(dòng)態(tài)添加或刪除tr功能示例
相關(guān)文章
Ajax實(shí)現(xiàn)省市區(qū)三級(jí)級(jí)聯(lián)(數(shù)據(jù)來自mysql數(shù)據(jù)庫(kù))
這篇文章主要為大家詳細(xì)介紹了Ajax實(shí)現(xiàn)省市區(qū)三級(jí)級(jí)聯(lián),數(shù)據(jù)來自mysql數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Ajax+Struts2實(shí)現(xiàn)驗(yàn)證碼驗(yàn)證功能實(shí)例代碼
這篇文章主要介紹了Ajax+Struts2實(shí)現(xiàn)驗(yàn)證碼驗(yàn)證功能實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
用ajax實(shí)現(xiàn)在單擊事件下加載一個(gè)DIV層的腳本
用ajax實(shí)現(xiàn)在單擊事件下加載一個(gè)DIV層的腳本...2007-11-11
在layer彈出層中通過ajax返回html拼接字符串填充數(shù)據(jù)的方法
這篇文章主要介紹了在layer彈出層中通過ajax返回html拼接字符串填充數(shù)據(jù)的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
$.ajax傳JSON數(shù)據(jù)到后臺(tái)的注意事項(xiàng)小結(jié)
這篇文章主要介紹了$.ajax傳JSON數(shù)據(jù)到后臺(tái)的注意事項(xiàng),需要的朋友可以參考下2014-05-05
JavaScript基于Ajax實(shí)現(xiàn)不刷新在網(wǎng)頁(yè)上動(dòng)態(tài)顯示文件內(nèi)容
這篇文章主要介紹了JavaScript基于Ajax實(shí)現(xiàn)不刷新在網(wǎng)頁(yè)上動(dòng)態(tài)顯示文件內(nèi)容,可實(shí)現(xiàn)實(shí)時(shí)顯示服務(wù)器上txt文件內(nèi)容的功能,是Ajax基本應(yīng)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03

