国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

使用dwr時動態(tài)生成table的一個小技巧

2019-11-14 14:49:49
字體:
供稿:網(wǎng)友

  這篇隨筆是我在07年寫的,因為當(dāng)時用了自己建設(shè)的blog,后來停止使用了,今天看到備份數(shù)據(jù)庫還在,恢復(fù)出來放到這里。留著記錄用。

  我在使用DWR時,試了很多次都無法在動態(tài)生成的table中的一個或多個td中進(jìn)行動態(tài)鏈接,后來才發(fā)現(xiàn)原來在cellfuncs中覆蓋了我想定義的輸出,不過無意中讓我發(fā)現(xiàn)了可以用下面的方法來處理

方法如下:

DWRUtil.addRows(id, array, cellfuncs, [options]);
這個函數(shù)估計調(diào)用的人很多,而且現(xiàn)在支持直接將對象傳給這個函數(shù),這就方便了許多(以前只能是數(shù)組),而且cellfuncs的功能也很強(qiáng)大,一般都會省略options參數(shù)(這個參數(shù)只用做定義CSS或其它特殊使用,我這里就是在這個參數(shù)里做動態(tài)鏈接的處理)。

下面就是處理代碼:


DWRUtil.addRows('testid', myObject, [
     function(data) { return data.name; },
     function(data) {
   //return data.id;
  //通常都會在這里直接返回處理的數(shù)據(jù),如果要實現(xiàn)動態(tài)多鏈接,
//那么這里就什么都別輸出(當(dāng)然也可以輸出,但這里所有的html標(biāo)記都會直接顯示在頁面上,
//無法進(jìn)行解析)
      //doNothing
     }],
     {rowCreator:function(options) {
      return document.createElement("tr");
     },
     cellCreator:function(options) {
      if(options.cellNum==1){
       var td = document.createElement("td");
       td.setAttribute("align","center");
       var thtml = "<a href='#' onclick=/"javascript:mydel(
'"+options.rowData.testId+"');/">刪除</a>";
       td.innerHTML = thtml;
       return td;
      }else{
       return document.createElement("td");
      }
     }
     });

cellfuncs中的處理相當(dāng)重要,當(dāng)直接返回值時,會影響[options]參數(shù)中的處理內(nèi)容(比如options中要

加入的TD內(nèi)容會無效,當(dāng)然,td.setAttribute("align","center")等是有效的)。

對DWRUtil.addRows(id, array, cellfuncs, [options])的補(bǔ)充:
其中:
id是table元素的id,最好使用tbody
array數(shù)據(jù),從1.1開始支持對象
cellfuncs: 函數(shù)數(shù)組,從傳遞過來的行數(shù)據(jù)中提取單元格數(shù)據(jù)。
如:
var cellfuncs = [
function(data) { return data; },
function(data) { return data.toUpperCase(); },
function(data) {
var input = document.createElement("input");
input.setAttribute("type", "button");
input.setAttribute("value", "DOM Test");
input.setAttribute("onclick", "alert('" + data + "');");
return input;
},
function(data) { return "<input type='button' value='innerHTML Test'
 onclick='alert(/"" + data + "/");'>"; }
]


注意:這里定義的數(shù)組size表示td的數(shù)量,面data就是array的引用,如果使用convert轉(zhuǎn)換過bean,那么可以直接調(diào)用屬性。

[options]:這個最為有用,也是這里所要說的重點,包含兩個對象
rowCreator: 一個用來創(chuàng)建行的函數(shù)(例如,你希望個tr加個css). 默認(rèn)是返回一個document.createElement("tr")
cellCreator: 一個用來創(chuàng)建單元格的函數(shù)(例如,用th代替 td). 默認(rèn)返回一個document.createElement("td")
一般都用不到這個參數(shù),但對于有特殊要求的朋友來說,這就成了重點
定義的例子如下:


var custoptions = {rowCreator:function(options) {
      return document.createElement("tr");
     },
     cellCreator:function(options) {
      if(options.cellNum==1){
       var td = document.createElement("td");
       td.setAttribute("align","center");
       var thtml = "<a href='#' onclick=/"Javascript:mydel
('"+options.rowData.testId+"');/">刪除</a>";
       td.innerHTML = thtml;
       return td;
      }else{
       return document.createElement("td");
      }
     }
     }


其中 options 參數(shù)的屬性可用的為:
rowData: the element value from the array (the same for all cells in a row)
rowIndex: the key (if map) or index (if array) from the collection
rowNum: The row number counting from 0 in this section (so if you are using tbody, it counts rows in the tbody and not the whole table)
data: The 'computed' data value for the cell (cellCreators only)
cellNum: The cell number that we are altering counting from 0 (cellCreators only)
我在上面用到了rowNum、rowData屬性
最后還是說一句,DWR太偉大了,讓我這個不懂JS的人也可以輕松上陣。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹阳市| 工布江达县| 乡城县| 昭通市| 高台县| 台北县| 广水市| 永定县| 厦门市| 三河市| 临潭县| 万源市| 永安市| 广灵县| 锦州市| 叙永县| 盈江县| 公安县| 西和县| 阿荣旗| 斗六市| 革吉县| 富宁县| 进贤县| 云林县| 容城县| 惠州市| 苍溪县| 正蓝旗| 综艺| 仪征市| 永兴县| 于田县| 山丹县| 冕宁县| 宁海县| 收藏| 定兴县| 鲁山县| 临汾市| 弥勒县|