jquery与json的结合.docx

上传人:b****1 文档编号:2069808 上传时间:2023-05-02 格式:DOCX 页数:14 大小:17.63KB
下载 相关 举报
jquery与json的结合.docx_第1页
第1页 / 共14页
jquery与json的结合.docx_第2页
第2页 / 共14页
jquery与json的结合.docx_第3页
第3页 / 共14页
jquery与json的结合.docx_第4页
第4页 / 共14页
jquery与json的结合.docx_第5页
第5页 / 共14页
jquery与json的结合.docx_第6页
第6页 / 共14页
jquery与json的结合.docx_第7页
第7页 / 共14页
jquery与json的结合.docx_第8页
第8页 / 共14页
jquery与json的结合.docx_第9页
第9页 / 共14页
jquery与json的结合.docx_第10页
第10页 / 共14页
jquery与json的结合.docx_第11页
第11页 / 共14页
jquery与json的结合.docx_第12页
第12页 / 共14页
jquery与json的结合.docx_第13页
第13页 / 共14页
jquery与json的结合.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

jquery与json的结合.docx

《jquery与json的结合.docx》由会员分享,可在线阅读,更多相关《jquery与json的结合.docx(14页珍藏版)》请在冰点文库上搜索。

jquery与json的结合.docx

jquery与json的结合

jquery于json的结合

通过AJAX异步减少网络内容传输,而JSON则可以把传输内容缩减到纯数据;然后利用jQuery内置的AJAX功能直接获得JSON格式的数据;在客户端直接绑定到数据控件里面,从而达到最优。

1.设计htm页面:

DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http:

//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

//www.w3.org/1999/xhtml">

test2


value="<"/>"/>

value=">>"/>

 

订单ID

/

客户ID

雇员ID

/

订购日期

/

发货日期

/

货主名称

/

货主地址

/

货主城市

/

更多信息

0px;position:

absolute;top:

0px;background-color:

red">

LOADING....

注:

ID属性比较重要,用于数据绑定。

2.使用jQuery编写AJAX请求文件

varpageIndex=1

varpageCount=0;

$(function(){

GetPageCount();//取得分页总数

pageCount=parseInt($("#pagecount").val());//分页总数放到变量pageCount里

$("#load").hide();//隐藏loading提示

$("#template").hide();//隐藏模板

ChangeState(0,1);//设置翻页按钮的初始状态

bind();//绑定第一页的数据

//第一页按钮click事件

$("#first").click(function(){

pageIndex=1;

ChangeState(0,1);

bind();

});

//上一页按钮click事件

$("#previous").click(function(){

pageIndex-=1;

ChangeState(-1,1);

if(pageIndex<=1)

{

pageIndex=1;

ChangeState(0,-1);

}

bind();

});

//下一页按钮click事件

$("#next").click(function(){

pageIndex+=1;

ChangeState(1,-1);

if(pageIndex>=pageCount)

{

pageIndex=pageCount;

ChangeState(-1,0);

}

bind(pageIndex);

});

//最后一页按钮click事件

$("#last").click(function(){

pageIndex=pageCount;

ChangeState(1,0);

bind(pageIndex);

});

});

//AJAX方法取得数据并显示到页面上

functionbind()

{

$("[@id=ready]").remove();

$("#load").show();

$.ajax({

type:

"get",//使用get方法访问后台

dataType:

"json",//返回json格式的数据

url:

"Handler.ashx",//要访问的后台地址

data:

"pageIndex="+pageIndex,//要发送的数据

complete:

function(){$("#load").hide();},//AJAX请求完成时隐藏loading提示

success:

function(msg){//msg为返回的数据,在这里做数据绑定

vardata=msg.table;

$.each(data,function(i,n){

varrow=$("#template").clone();

row.find("#OrderID").text(n.OrderID);

row.find("#CustomerID").text(n.CustomerID);

row.find("#EmployeeID").text(n.EmployeeID);

row.find("#OrderDate").text(ChangeDate(n.OrderDate));

if(n.RequiredDate!

==undefined)row.find("#ShippedDate").text(ChangeDate(n.RequiredDate));

row.find("#ShippedName").text(n.ShipName);

row.find("#ShippedAddress").text(n.ShipAddress);

row.find("#ShippedCity").text(n.ShipCity);

row.find("#more").html("

id="+n.OrderID+"&pageindex="+pageIndex+"> More");

row.attr("id","ready");//改变绑定好数据的行的id

row.appendTo("#datas");//添加到模板的容器中

});

$("[@id=ready]").show();

SetPageInfo();

}

});

}

functionChangeDate(date)

{

returndate.replace("-","/").replace("-","/");

}

//设置第几页/共几页的信息

functionSetPageInfo()

{

$("#pageinfo").html(pageIndex+"/"+pageCount);

}

//AJAX方法取得分页总数

functionGetPageCount()

{

$.ajax({

type:

"get",

dataType:

"text",

url:

"Handler.ashx",

data:

"getPageCount=1",

async:

false,

success:

function(msg){

$("#pagecount").val(msg);

}

});

}

//改变翻页按钮状态

functionChangeState(state1,state2)

{

if(state1==1)

{

document.getElementById("first").disabled="";

document.getElementById("previous").disabled="";

}

elseif(state1==0)

{

document.getElementById("first").disabled="disabled";

document.getElementById("previous").disabled="disabled";

}

if(state2==1)

{

document.getElementById("next").disabled="";

document.getElementById("last").disabled="";

}

elseif(state2==0)

{

document.getElementById("next").disabled="disabled";

document.getElementById("last").disabled="disabled";

}

}

3.利用JSON三方控件在服务器端获取JSON格式数据

<%@WebHandlerLanguage="C#"Class="jQueryJSON.Handler"%>

usingSystem;

usingSystem.Data;

usingSystem.Web;

usingSystem.Collections;

usingSystem.Web.Services;

usingSystem.Web.Services.Protocols;

usingSystem.Configuration;

usingSystem.Data.SqlClient;

usingSystem.Text;

usingSystem.Xml;

usingNetServ.Net.Json;

namespacejQueryJSON

{

///

///$codebehindclassname$的摘要说明

///

[WebService(Namespace="http:

//tempuri.org/json/")]

[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]

publicclassHandler:

IHttpHandler

{

readonlyintPageSize=int.Parse(ConfigurationManager.AppSettings["PageSize"]);

publicvoidProcessRequest(HttpContextcontext)

{

context.Response.ContentType="text/plain";

//不让浏览器缓存

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";

stringresult="";

if(context.Request.Params["getPageCount"]!

=null)result=GetPageCount();

if(context.Request.Params["pageIndex"]!

=null)

{

stringpageindex=context.Request.Params["pageIndex"];

//if(context.Cache.Get(pageindex)!

=null)

//result=context.Cache.Get(pageindex).ToString();

//else

//{

//result=GetPageData(context.Request.Params["pageIndex"]);

//context.Cache.Add(

//pageindex,

//result,

//null,

//DateTime.Now.AddMinutes

(1),

//System.Web.Caching.Cache.NoSlidingExpiration,

//System.Web.Caching.CacheItemPriority.Default,

//null);

//}

result=GetPageData(context.Request.Params["pageIndex"]);

}

context.Response.Write(result);

}

privatestringGetPageData(stringp)

{

intPageIndex=int.Parse(p);

stringsql;

if(PageIndex==1)

sql="selecttop"+PageSize.ToString()+"*fromOrdersorderbyOrderIDdesc";

else

sql="selecttop"+PageSize.ToString()+"*fromOrderswhereOrderIDnotin(selecttop"+((PageIndex-1)*PageSize).ToString()+"OrderIDfromOrdersorderbyOrderIDdesc)orderbyOrderIDdesc";

stringdbfile=ConfigurationManager.ConnectionStrings["conn"].ToString();

SqlConnectionconn=newSqlConnection(dbfile);

SqlDataAdapterda=newSqlDataAdapter(sql,conn);

DataTabledt=newDataTable("table");

da.Fill(dt);

returnDataTableJson(dt);

}

privatestringGetPageCount()

{

stringdbfile=ConfigurationManager.ConnectionStrings["conn"].ToString();

SqlConnectionconn=newSqlConnection(dbfile);

SqlCommandcmd=newSqlCommand("selectcount(*)fromOrders",conn);

conn.Open();

introwcount=Convert.ToInt32(cmd.ExecuteScalar());

conn.Close();

return((rowcount+PageSize-1)/PageSize).ToString();

}

privatestringDataTable2Json(DataTabledt)

{

StringBuilderjsonBuilder=newStringBuilder();

jsonBuilder.Append("{\"");

jsonBuilder.Append(dt.TableName);

jsonBuilder.Append("\":

[");

for(inti=0;i

{

jsonBuilder.Append("{");

for(intj=0;j

{

jsonBuilder.Append("\"");

jsonBuilder.Append(dt.Columns[j].ColumnName);

jsonBuilder.Append("\":

\"");

jsonBuilder.Append(dt.Rows[i][j].ToString());

jsonBuilder.Append("\",");

}

jsonBuilder.Remove(jsonBuilder.Length-1,1);

jsonBuilder.Append("},");

}

jsonBuilder.Remove(jsonBuilder.Length-1,1);

jsonBuilder.Append("]");

jsonBuilder.Append("}");

returnjsonBuilder.ToString();

}

privatestringDataTableJson(DataTabledt)

{

JsonWriterwriter=newJsonWriter();

JsonObjectcontent=newJsonObject();

JsonArrayOrders=newJsonArray();

JsonObjectOrder;

JsonObjectOrderItem=newJsonObject();

for(inti=0;i

{

Order=newJsonObject();

for(intj=0;j

{

Order.Add(dt.Columns[j].ColumnName,dt.Rows[i][j].ToString());

}

Orders.Add(Order);

}

content.Add(dt.TableName,Orders);

content.Write(writer);

writer=newIndentedJsonWriter();

content.Write(writer);

returnwriter.ToString();

}

publicboolIsReusable

{

get

{

returnfalse;

}

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2