當柳上原的風吹向天際的時候...

          真正的快樂來源于創造

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          http://www.cnblogs.com/evilyang/archive/2012/02/17/2355218.html

          一、使用場景

             服務端獲得的DataTable轉化為Json格式后傳遞給客戶端dojo,dojo將json數據直接綁定在dojox.grid.DataGrid上

          二、基本用法

          1.客戶端頁面DataToJson.aspx返回一個Json數據

          復制代碼
              private void Json()
              {
                  DataTable dt = this.GetData();
                  string str = JsonHelper.DateTableToJson(dt);
                  Response.Write(str);
                  Response.End();
              }
          復制代碼
           2.利用ajax接受json數據

          dojox.grid.DataGrid憑借dojo.data.ItemFileWriteStore可以輕松具有ajax功能

          使用dojo.grid.DataGrid首先做如下準備工作

          a.引入樣式表

          <link rel="Stylesheet" href="dojo-re/dojox/grid/resources/soriaGrid.css" />
          b.引入所需庫
          dojo.require("dojo.parser");
          dojo.require("dijit.form.Button"); 
          dojo.require("dojox.grid.DataGrid");

          dojo.require("dojo.data.ItemFileWriteStore");

          dojo.require("dojox.layout.FloatingPane");
          c.編寫代碼
          復制代碼
          <script type="text/javascript">
                  function Grid1() {
                      var data = new dojo.data.ItemFileWriteStore({
                          url: "DataToJson.aspx"
                      });
                      var structure = [
                          { name: "用戶名", field: "userName", width: "120px" },
                          { name: "密碼", field: "userPwd", width: "120px" },
                          { name: "電子郵件", field: "email", width: "150px;" },
                          { name: "博客", field: "blog", width: "150px" },
                          { name: "生日", field: "birthday", width: "120px" },
                          { name: "年齡", field: "age", width: "80px" },
                          { name: "備注", field: "description", width: "120px" }
                      ];
                      var grid = new dojox.grid.DataGrid({
                      store: data,
                      structure:structure
                      },"grid1");
                      grid.startup();
                  }
                  function ShowFloatingPane() {
                      var floatingPane = dijit.byId("dFloatingPane");
                      floatingPane.show();
                      Grid1();
                  }
              </script>
          復制代碼

           所需HTML

          復制代碼
             <div >
                  <div data-dojo-type="dojox.layout.FloatingPane" id="dFloatingPane"
                     title
          ="A floating pane" data-dojo-props="resizable:true, dockable:true, title:'A floating pane'"
                     style
          ="position:absolute;top:150px;left:400px;width:600px;height:400px; visibility:hidden">
                       <div id="grid1" style="width:450px; height:350px"></div>
                  </div>
              </div>
          <div data-dojo-type="dijit.form.Button" data-dojo-props="label:'Show me', onClick:ShowFloatingPane"></div>
           
          復制代碼

           d.運行結果如下:

           

           三、繼續完善DataGrid功能

          1,增加搜索條件

          query:{userName:"evilyang",id:"*"},

           2,隱藏一列,不顯示

           {name:"密碼",field:"userPwd",width:"100px",hidden:"true"}

          3,為某一列增加一個樣式名

           <style type="text/css">
              .name{ font-style:italic; font-size:14px; color:Red;}
              </style>
          { name: "用戶名", field: "userName", width: "120px" ,classes:"name"}
           4,為某一列直接增加一個樣式
          { name: "電子郵件", field: "email", width: "150px;",styles:"text-align:center;" },
          5,固定前兩列

          更改structure結構,加入noscroll屬性

          復制代碼
          var structure = [{
                          noscroll: true,
                          cells: [
                          { name: "用戶名", field: "userName", width: "80px", classes: "name" },
                          { name: "密碼", field: "userPwd", width: "80px", hidden: "true" },
                          { name: "電子郵件", field: "email", width: "150px;", styles: "text-align:center;" }    
                          ]
                      }, {
                          cells: [
                          { name: "博客", field: "blog", width: "120px" },
                          { name: "生日", field: "birthday", width: "120px" },
                          { name: "年齡", field: "age", width: "50px" },
                          { name: "備注", field: "description", width: "120px" }
                          ]
                      }];
          復制代碼

           6,cell中的樣式設置默認模式

          defaultCell:{width:"80px",styles:"text-align:center;"},
           這樣設置完后,每一列的屬性就不必單獨設置了

          7, 其他屬性

          selectionMode: "extended", //none,single,multiple
          loadingMessage: "請等待,數據正在加載中......",
           errorMessage: "對不起,你的請求發生錯誤!",
           columnReordering:true//此屬性設置為true,可以拖拽標題欄,更換列順序

          new dojox.grid.cells.RowIndex({ name: "編號", width: "20px" })//加入自編號

          四、數據顯示高級功能

          1, RowClick事件

          復制代碼
          grid.on("RowClick", function(evt) {
                          var idx = evt.rowIndex,
                              item = this.getItem(idx),
                              store = this.store;
                              content = dojo.byId("content");
                              content.innerHTML="you have clicked on rows " + store.getValue(item, "id");
                    }, true);
          復制代碼

           2,SelectionChanged事件

          復制代碼
          grid.on("SelectionChanged",dojo.hitch(grid, reportSelection), true);
          function reportSelection() {
                      var items = this.selection.getSelected(),
                                  msg = "你選擇了以下數據";
                      var tmp = dojo.map(items, function(item) {
                          return this.store.getValue(item, "id");
                      }, this);
                      var content = dojo.byId("content");
                      content.innerHTML = msg + tmp.join(",");
                     
                  }
          復制代碼

          五、顯示效果如下圖:


          posted on 2013-02-18 17:09 何楊 閱讀(3937) 評論(0)  編輯  收藏 所屬分類: Dojo

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 扬中市| 土默特右旗| 水富县| 桦南县| 徐州市| 加查县| 平山县| 沈阳市| 定南县| 卓尼县| 西贡区| 霍林郭勒市| 阿图什市| 固阳县| 四子王旗| 库伦旗| 阿拉善盟| 定兴县| 琼结县| 西峡县| 连平县| 泰兴市| 裕民县| 清远市| 林甸县| 峨眉山市| 临朐县| 云林县| 安丘市| 通渭县| 林甸县| 南岸区| 孝义市| 灵宝市| 礼泉县| 双辽市| 大丰市| 汪清县| 宜丰县| 荥阳市| 县级市|