AjaxExtJs学习笔记(21)-使用XTemplate结合WCF显示数据

个人觉得,XTemplate是ExtJs中尽灵敏的用来展示数据的零部件,有接触类似aspx中的Repeater控件,显示数据的模版完全可由用户因html方式来定制.

 

先期叫一个合法的静态示例(稍微改变了下),代码并无复杂,关键之地方,我一度注释了

 1Ajax 1    <script type=”text/javascript”>
 2Ajax 2Ajax 3        Ext.onReady(function() Ajax 4{
 3Ajax 5
 4Ajax 6Ajax 7            var data = Ajax 8{
 5Ajax 9                name: ‘Jack Slocum’,
 6Ajax 10                title: ‘Lead Developer’,
 7Ajax 11                company: ‘Ext JS, LLC’,
 8Ajax 12                email: ‘jack@extjs.com’,
 9Ajax 13                address: ‘4 Red Bulls Drive’,
10Ajax 14                city: ‘Cleveland’,
11Ajax 15                state: ‘Ohio’,
12Ajax 16                zip: ‘44102’,
13Ajax 17                drinks: [‘Red Bull’, ‘Coffee’, ‘Water’],
14Ajax 18                kids: [
15Ajax 19Ajax 20    Ajax 21{ name: ‘Sara Grace’, age: 3 },
16Ajax 22Ajax 23    Ajax 24{ name: ‘Zachary’, age: 2 },
17Ajax 25Ajax 26    Ajax 27{ name: ‘John James’, age: 0 }
18Ajax 28]
19Ajax 29            };
20Ajax 30
21Ajax 31            var tpl = new Ext.XTemplate(
22Ajax 32‘<p>Name: {name}</p>’,
23Ajax 33‘<p>Title: {title}</p>’,
24Ajax 34‘<p>Company: {company}</p>’,
25Ajax 35‘<p>[Kids:] ‘,
26Ajax 36‘<tpl for=”kids”>’,//表明这里循环读博kids节的多寡
27Ajax 37‘<p>  {name}</p>’,
28Ajax 38‘</tpl></p>’,
29Ajax 39‘<p>[Drinks:] ‘,
30Ajax 40‘<tpl for=”drinks”>’,
31Ajax 41‘<p>  {#}.{.}</p>’, //表明这里循环读取drinks节的数,当数没有性能名称时,用”.”代表数量
32Ajax 42‘</tpl></p>’
33Ajax 43);
34Ajax 44            tpl.overwrite(Ext.getBody(), data);
35Ajax 45
36Ajax 46        });
37Ajax 47    </script>   

 

运作结果图:
Ajax 48

 

连片下我们看哪与服务器端的WCF做交互

1.首先定义一个用于信息传输的实业类(实际开支被,可以是其余可序列化的class,包括linq
to sql生成的类似)

 

Code
using System.Collections.Generic;
using System.Runtime.Serialization;


namespace Ajax_WCF
{
    [DataContract]
    public class Person
    {
        [DataMember]
        public string name { set; get; }
        [DataMember]
        public string title { set; get; }
        [DataMember]
        public string company { set; get; }
        [DataMember]
        public string email { set; get; }
        [DataMember]
        public string address { set; get; }
        [DataMember]
        public string city { set; get; }
        [DataMember]
        public string state { set; get; }
        [DataMember]
        public string zip { set; get; }
        [DataMember]
        public List<AbstractData> drinks { set; get; }
        [DataMember]
        public List<KidData> kids { set; get; }
    }


    [DataContract]
    public class AbstractData
    {
        [DataMember]
        public string name { set; get; }
    }


    [DataContract]
    public class KidData : AbstractData
    {
        [DataMember]
        public int age { set; get; }
    }
}

2.新建一个”启用了Ajax的WCF服务”
a.又以svc手动修改一下,注意下面的高亮部分如手动加上去:

<%@ ServiceHost Language=”C#” Debug=”true”
Service=”Ajax_WCF.MyService” CodeBehind=”MyService.svc.cs” Factory=”System.ServiceModel.Activation.WebServiceHostFactory”%>
b.web.config中,也参照下面修改
 <behavior name=”Ajax_WCF.MyServiceAspNetAjaxBehavior”>
      <webHttp />
      <!–<enableWebScript/>–>
    </behavior>

c.于wcf中追加一个方式,代码如下:

Code
[OperationContract]
        [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetPerson", Method = "*")]
        public Person GetPerson() {
            System.Threading.Thread.Sleep(3000);//为演示loading效果,停3秒
            return new Person() {
                name = "Jack Slocum",
                title = "Lead Developer",
                company = "Ext JS, LLC",
                email = "jack@extjs.com",
                address = "4 Red Bulls Drive",
                city = "Cleveland",
                state = "Ohio",
                zip = "44102",
                drinks = new List<AbstractData> { 
                new AbstractData(){name="Red Bull"},
                new AbstractData(){name="Coffee"},
                new AbstractData(){name="Water"}
                },
                kids = new List<KidData> { 
                new KidData(){name="Red Bull",age=3},
                new KidData(){name="Coffee",age=2},
                new KidData(){name="Water",age=0}
                }
            };
        }

这边以简单起见,直接new并初始化了一个Person对象,然后回到吗JSON格式

 

3.末尾修改一下前方静态页的代码,完整页面代码如下:

 

Code
<!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 >
    <title>XTemplate示例</title>
    <link rel="stylesheet" type="text/css" href="js/ext2.2/resources/css/ext-all.css"/> 
    <script type="text/javascript" src="js/ext2.2/adapter/ext/ext-base.js"></script> 
    <script type="text/javascript" src="js/ext2.2/ext-all.js"></script> 
    <style type="text/css">
        body{font-size:9pt;padding:20px;}
        .red{color:red;}
        h1{padding-bottom:10px;}
    </style>
</head>
<body>
<script type="text/javascript">
    Ext.onReady(function() {        

        var tpl = new Ext.XTemplate(
        '<p>Name: {name}</p>',
        '<p>Title: {title}</p>',
        '<p>Company: {company}</p>',
        '<p>[Kids:] ',
        '<tpl for="kids">',
        '<p>  {name}</p>',
        '</tpl></p>',
        '<p>[Drinks:] ',
        '<tpl for="drinks">',
        '<p>  {#}.{name}</p>', 
        '</tpl></p>'
        );

        var demo = Ext.get("demo");
        demo.dom.innerHTML = "数据加载中,请稍候";
        demo.dom.className = "red";

        Ext.Ajax.request({
            url: "MyService.svc/GetPerson",
            method: "GET",
            success: function(request) {
                demo.dom.className = "";
                var data = Ext.util.JSON.decode(request.responseText);
                tpl.overwrite("demo", data);
            },
            failure: function() {
                alert("failure!");
            }
        });       

    });
    </script>   
    <h1>XTemplate 示例</h1>
    <div id="demo" style="border:1px solid #ccc;padding:10px;width:300px"></div>
</body>


</html>

解释一下:利用ExtJs的Ajax对象,请求MyServices.svc/GetPerson方法,获取Json字符串,其它地方了一致,需要留意的凡:

a.返回的字符串,需要用Ext.Util.JSON.decode转换成JSON对象

b.以显得出loading加载效果,在调用Ajax的Request之前,用demo.com.innerHTML和demo.com.className先把目标div设置成”加载中”的字样

运作效果如下:
Ajax 49
转载请注明来源”菩提树下的杨过”http://www.cnblogs.com/yjmyzz/archive/2008/09/09/1287767.html
Ajax 50
立反过来运行时,增加了”数据加载中”的意义,加载成功后及才的静态示例显示结果完全相同

 

相关文章