TWaver - 專注UI技術

          http://twaver.servasoft.com/
          posts - 171, comments - 191, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
          TWaver HTML5發布已有一段時間,使用的客戶也是逐漸增加,于是我也迫不及待地申請了一個試用版來寫一個小網頁,最近正在寫到數據查詢,表格顯示的功能。表格組件在HTML5中是提供的,查看TWaver提供的Demo,表格的使用還是比較多的,于是參考了其中的一個Demo,新建一個表格,并給表格賦值。很快一張表格就生成了。



          但是想想,如果數據庫中有幾千甚至幾萬條數據,一下子顯示出來也是不現實的,立馬就想要了分頁。查看TWaver的API,并沒有發現表格中提供了分頁的功能。算了,還是自己來擴展,想想TWaver Java中分頁的功能,HTML5實現起來應該也不算太難,我們需要定義一個PagedTablePane,panel中包含表格和分頁欄,分頁欄參考了TWaver Java的那種:



          仔細看看上面的分頁條,其實也不是那么復雜,幾個分頁按鈕加上分頁的信息,于是很快就模仿了一個類似的分頁欄,先上圖:



          界面實現起來還是比較容易的,主要的是按鈕的操作和分頁信息的顯示,我們需要定義幾個變量:currentPage(當前頁)、countPerPage(每頁的條數)、pageCount(頁數)、count(總數),定義了這幾個變量就可以將上圖中分頁的信息表示出來了。

          另外需要注意,分頁按鈕上也需要做一些判斷,比如當前已經是第一頁了,那么“首頁”和“上一頁”的按鈕就應該顯示灰色,不可操作的狀態;同理如果當前是最后一頁,那么后面兩個按鈕也需要呈不可操作狀態,我們來看下相關代碼:

           1 if(this.pageCount < 2) {
           2             this.btnFirstPage.disabled = true;
           3             this.btnPreviousPage.disabled = true;
           4             this.btnNextPage.disabled = true;
           5             this.btnLastPage.disabled = true;
           6         } else {
           7             this.btnFirstPage.disabled = false;
           8             this.btnPreviousPage.disabled = false;
           9             this.btnNextPage.disabled = false;
          10             this.btnLastPage.disabled = false;
          11             if(this.currentPage == 0) {
          12                 this.btnFirstPage.disabled = true;
          13                 this.btnPreviousPage.disabled = true;
          14             }
          15             if(this.currentPage == this.pageCount - 1) {
          16                 this.btnNextPage.disabled = true;
          17                 this.btnLastPage.disabled = true;
          18             }
          19         }

          按鈕除了需要判斷顯示狀態之外,還需要加鼠標點擊的監聽,這里我們需要后臺分頁,因此,每次點擊按鈕時,都需要調用后臺的方法查詢出相應的數據,這樣也可以減少前臺一次加載大量數據的壓力。

           1 this.btnFirstPage = util.addButton(toolbar, '<<', function () {
           2         self.currentPage = 0;
           3         self.search();
           4     });
           5     this.btnPreviousPage = util.addButton(toolbar, '<', function () {
           6         self.currentPage --;
           7         self.search();
           8     });
           9     this.btnNextPage = util.addButton(toolbar, '>', function () {
          10         self.currentPage ++;
          11         self.search();
          12     });
          13     this.btnLastPage = util.addButton(toolbar, '>>', function () {
          14         self.currentPage = self.pageCount -1;
          15         self.search();
          16     });

          另外下拉條也需要加交互事件:

          1 cmbCountPerPage.onchange = function () {
          2         var value = parseFloat(cmbCountPerPage.value);
          3         self.countPerPage = value;
          4         self.search();
          5     };

          上面代碼中的search都是調用后臺的方法,這里就不再給出了。至此,分頁的功能就差不多了,加上分頁后的效果:



          細心的朋友還會看到上面的表頭上有些列是可點的,有些是不可點擊的。我在這里加上了后臺排序的功能,如果這一列可以排序就為可點擊狀態,反之則為不可點擊狀態。

          HTML5中的表格默認是可以進行排序的,不過這種排序也是在當前頁進行排序,還不是真正意義上的后臺排序,其實分頁后也只有當前頁的數據在databox中,后面頁的數據都需要通過實時查找才能獲取放置到databox中。因此點擊排序按鈕時我們需要將TWaver默認的處理方式給屏蔽掉,加上自己的處理就可以了,具體實現如下:

          先重寫table上的getCurrentSortFunction:

          1 getCurrentSortFunction: function () {
          2         return this.getSortFunction();
          3     }

          然后在onColumnSorted方法中進行自己的業務處理:

           1 this.table.onColumnSorted = function (column) {
           2   self.currentPage = 0;
           3   if (column) {
           4      self.dataPane._sortBy = column.getClient('sortProperty');
           5      self.dataPane._orderAsc = column.getSortDirection() === 'asc';
           6   } else {
           7      self.dataPane._sortBy = null;
           8   }
           9   self.search();
          10  };

          這里的sortBy和orderAsc是自己定義的兩個變量,在search方法中會調用此變量,將其傳入后臺進行排序。最后,給出完整的PagedTablePane供大家參考: 

            1 var PagedTablePane = function (dataPane) {
            2     PagedTablePane.superClass.constructor.call(this);
            3     this.dataPane = dataPane;
            4     var toolbar = document.createElement('div');
            5     this.setCenter(new twaver.controls.TablePane(dataPane.table));
            6     this.setTop(toolbar);
            7     this.setTopHeight(25);
            8     this.countPerPage = 20;
            9     var self = this;
           10     var isStorageSupport = typeof(Storage) !== "undefined";
           11  if(isStorageSupport) {
           12         var storageName = dataPane.getPageNumberStorageName();
           13         if(localStorage.getItem(storageName)){
           14             self.countPerPage = parseFloat(localStorage.getItem(storageName));
           15         }
           16  }
           17     var cmbCountPerPage = document.createElement('select');
           18     var items = ['20', '50', '100', '500', '1000'];
           19     for(var i=0,item; i<items.length; i++) {
           20         item = items[i];
           21         var option = document.createElement('option');
           22         option.appendChild(document.createTextNode(item));
           23         option.setAttribute('value', item);
           24         cmbCountPerPage.appendChild(option);
           25     }
           26     cmbCountPerPage.onchange = function () {
           27         var value = parseFloat(cmbCountPerPage.value);
           28         self.countPerPage = value;
           29         if(isStorageSupport) {
           30             var storageName = dataPane.getPageNumberStorageName();
           31             localStorage.setItem(storageName,value);
           32         }
           33         self.search();
           34     };
           35     cmbCountPerPage.value = this.countPerPage;
           36     toolbar.appendChild(cmbCountPerPage);
           37    
           38     this.btnFirstPage = util.addButton(toolbar, '<<', function () {
           39         self.currentPage = 0;
           40         self.search();
           41     });
           42     this.btnPreviousPage = util.addButton(toolbar, '<', function () {
           43         self.currentPage --;
           44         self.search();
           45     });
           46     this.btnNextPage = util.addButton(toolbar, '>', function () {
           47         self.currentPage ++;
           48         self.search();
           49     });
           50     this.btnLastPage = util.addButton(toolbar, '>>', function () {
           51         self.currentPage = self.pageCount -1;
           52         self.search();
           53     });
           54     this.label = document.createElement('label');
           55     toolbar.appendChild(this.label);
           56    
           57     this.table = dataPane.table;
           58     this.currentPage = 0;
           59    
           60     this.table.onColumnSorted = function (column) {
           61   self.currentPage = 0;
           62   if (column) {
           63      self.dataPane._sortBy = column.getClient('sortProperty');
           64      self.dataPane._orderAsc = column.getSortDirection() === 'asc';
           65   } else {
           66      self.dataPane._sortBy = null;
           67   }
           68   self.search();
           69  };
           70 };
           71 
           72 twaver.Util.ext('PagedTablePane', twaver.controls.BorderPane, {
           73     search: function () {
           74        util.invoke(thisthis.handleSearch, {
           75              moduleName:this.dataPane._moduleName,
           76              method: util.getCountMethod(this.dataPane._method),
           77              params: this.dataPane.getParams(),
           78              paramTypes:this.dataPane._paramType
           79         });
           80     },
           81     handleSearch: function (count) {
           82         this.jsonObject = JSON.parse(count);
           83         this.count = this.jsonObject[0];
           84         this.pageCount = Math.floor(this.count/this.countPerPage);
           85         if(this.count % this.countPerPage) {
           86             this.pageCount ++;
           87         }
           88         if(this.currentPage >= this.pageCount) {
           89             this.currentPage = this.pageCount -1;
           90         }
           91         this.label.innerHTML = jQuery.i18n.prop('paged.page',this.count,this.currentPage + 1,this.pageCount);
           92         if(this.pageCount < 2) {
           93             this.btnFirstPage.disabled = true;
           94             this.btnPreviousPage.disabled = true;
           95             this.btnNextPage.disabled = true;
           96             this.btnLastPage.disabled = true;
           97         } else {
           98             this.btnFirstPage.disabled = false;
           99             this.btnPreviousPage.disabled = false;
          100             this.btnNextPage.disabled = false;
          101             this.btnLastPage.disabled = false;
          102             if(this.currentPage == 0) {
          103                 this.btnFirstPage.disabled = true;
          104                 this.btnPreviousPage.disabled = true;
          105             }
          106             if(this.currentPage == this.pageCount - 1) {
          107                 this.btnNextPage.disabled = true;
          108                 this.btnLastPage.disabled = true;
          109             }
          110         }
          111         this.dataPane.initData();
          112     }
          113 });


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


          網站導航:
           
          主站蜘蛛池模板: 泰和县| 南江县| 盘锦市| 磴口县| 昌吉市| 富民县| 杭州市| 华阴市| 苍山县| 家居| 新巴尔虎左旗| 密山市| 高密市| 富裕县| 扎赉特旗| 丹寨县| 大足县| 仁寿县| 长垣县| 安徽省| 丹阳市| 寿阳县| 从江县| 竹山县| 鸡泽县| 左贡县| 枣阳市| 安塞县| 林口县| 九寨沟县| 大石桥市| 莱阳市| 绥阳县| 吐鲁番市| 滁州市| 玉溪市| 资阳市| 庆城县| 灵武市| 民县| 罗田县|