天天看點

ExtJs學習筆記(3)_GridPanel[XML做資料源]

這一節,将學習到除了用JSON做GridPanel的資料源外,還可以使用XML

 一。靜态示例

1.xml檔案内容:

<?xml version="1.0" encoding="UTF-8"?>

<Data>

  <Items>   

    <TotalResults>203</TotalResults>

    <TotalPages>21</TotalPages>

    <Item>

      <ASIN>0446355453</ASIN>     

      <Author>Jimmy.Yang</Author>

      <Manufacturer>Warner Books</Manufacturer>

      <ProductGroup>Book</ProductGroup>

      <Title>Master of the Game</Title>      

    </Item>

      <ASIN>0446613657</ASIN>          

      <Author>Sidney Sheldon</Author>

      <Title>Are You Afraid of the Dark?</Title>      

  </Items>

</Data>

2.ExtJs調用頁面

<!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"/>

    <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" />

     <script type="text/javascript" src="../adapter/ext/ext-base.js"></script>

    <script type="text/javascript" src="../ext-all.js"></script> 

    <title>ExtJs_Grid_Xml</title>

</head>

<body>

<script type="text/javascript"> 

    Ext.onReady(function() {

        var store = new Ext.data.Store({           

            url: 'GridData.xml',

            reader: new Ext.data.XmlReader(

                { record: 'Item' },                

                ["ASIN","Author", "Manufacturer", "ProductGroup", "Title"])

        });

        var grid = new Ext.grid.GridPanel({

            store: store,

            columns: [

                { id: "ASIN", header: "出版号", width: 120, dataIndex: 'ASIN', sortable: true },

                { header: "作者", width: 120, dataIndex: 'Author', sortable: true },

                { header: "書名", width: 180, dataIndex: 'Title', sortable: true },

                { header: "制作商", width: 115, dataIndex: 'Manufacturer', sortable: false },

                { header: "産品組", width: 100, dataIndex: 'ProductGroup', sortable: false }],

            renderTo: 'example-grid',

            viewConfig: { columnsText: '顯示列', sortAscText: '升序', sortDescText: '降序' },

            width: 640,

            height: 100

        store.load();

    });

</script>

<div id="example-grid"></div>

</body>

</html>

運作效果如下圖:

二。結合WCF動态讀取

1.WCF端關鍵代碼

定義一個可序列化的類(當然也可以是Linq to Sql中自動生成的類,不過要手動加DataContract和DataMember标記,以滿足WCF的資料契約要求)

 [DataContract]

    public class Book 

    {

        [DataMember]

        public string ISBN;

        public string Title;

        public string Author;

        public string Publisher;

    }

傳回Xml資料的方法,注意格式要設定成WebMessageFormat.Xml

[OperationContract]

        [WebInvoke(ResponseFormat = WebMessageFormat.Xml, Method = "GET", UriTemplate = "GetXmlData")]

        public Book[] GetXmlData() 

        {

            List<Book> _List = new List<Book>();

            _List.Add(new Book() { ISBN = "00001", Title = "c#深入程式設計(第四版)", Author = "Alien", Publisher = "北京出版社" });

            _List.Add(new Book() { ISBN = "00002", Title = "ExtJs完全教程", Author = "Mike", Publisher = "上海出版社" });

            return _List.ToArray();

        }

2.前端ExtJs代碼

        var store = new Ext.data.Store({

        url: 'MyService.svc/GetXmlData',

                { record: 'Book' },

                ["Author", "ISBN", "Publisher", "Title"])

                { id: "ISBN", header: "出版号", width: 120, dataIndex: 'ISBN', sortable: true },

                { header: "出版社", width: 115, dataIndex: 'Publisher', sortable: false }],                

除了把url由xxx.xml,改成了"MySerivce.svc/GetXmlData"其它的幾乎沒變化,運作之後,用Web Development Helper插件監測到GetXmLData傳回的内容為:

<Book>

<Author>Alien</Author>

<ISBN>00001</ISBN>

<Publisher>北京出版社</Publisher>

<Title>c#深入程式設計(第四版)</Title>

</Book>

<Author>Mike</Author>

<ISBN>00002</ISBN>

<Publisher>上海出版社</Publisher>

<Title>ExtJs完全教程</Title>

</ArrayOfBook>

繼續閱讀