Dhtmlx是一個半開源的js框架,說是半開源,因為它的有些腳本是收費的。總體感覺很輕巧,可依賴的東西不多,和ext這樣的龐然大物,dhtmlx應該定位為一個tool更為合適。我自己一直在使用,很喜歡它。因為網上好像沒看見可用的API中文翻譯,今天終于鼓起勇氣來做這件事情,純屬個人愛好,大伙見笑了,其實我的英文不好,大學四年,英語就掛過四次。好,不廢話了,這就開始。
1 dhtmlxgrid
1.1 API
1.1.1 attachEvent(evName , evHandler)
版本:大眾版
參數:
evName 可定義事件名稱
evHandler 用戶自定義處理函數.
用途:對當前grid事件綁定用戶的自定義的處理js函數,這里支持兩種格式定義
一 匿名函數定義
<script>
grid.attachEvent("onRowSelect",function(rowId,cellIndex){
alert("Row with id="+rowId+" was selected");
});
</script>
二 命名函數定義
<script>
grid.attachEvent("onEditCell",doOnEditCell);
function doOnEditCell(stage,rowId,cellIndex,newValue,oldValue){
if ((stage==2)&&(newValue!=oldValue)){
alert("Cell with id="+rowId+" and index="+cellIndex+" was edited");
return true;
}
return true;
}
</script>
這里也支持一個事件綁定多個處理函數的方法
<script>
grid.attachEvent("onCheck",doOnCheck1);
grid.attachEvent("onCheck",doOnCheck2);
function doOnCheck1(rowId,cellIndex,state){
if (state){
alert("Checkbox in the row with id="+rowId+" was checked");
}
}
function doOnCheck2(rowId,cellIndex,state){
if (state){
alert("Checkbox in column with index="+cellIndex+" was checked");
}
}
</script>
執行順序為doOnCheck1 –》doOnCheck2,這里可以用于通過全局js變量實現grid連動效果的實現。如,先onRowSelect獲得當前選中單元格的值,針對當前值,定義一個函數改變當前cell的樣式等,當然這樣的操作也可以一個function中實現,這里定義為分離,可實現兩個函數的被其他地方公共使用。
Grid中可供綁定的事件,參考grid事件介紹。
1.1.2 attachFooter(values, style)
版本:專業版
參數:
values:增加行的每個單元格值,以數組的形式給出,這里支持html的值表示
style:單元格的樣式
用途:
在grid的最后動態新增一行(表腳),注意當前表腳不會隨上下滾動條一起移動,并設置各單元數據和格樣式
可供參考實例:
//數組形式
grid.attachFooter("A,B,C,D");
//數組形式
grid.attachFooter(["A","B","C","D"])
//跨列增加
grid.attachFooter("A,#cspan,C,#cspan");
//跨行增加
grid.attachFooter("A,#rspan,C,#rspan");
//表達式html值
grid.attachFooter ("A,<strong>B</strong>,C,<a );
//指定各單元格樣式
grid.attachFooter ("A,B,C,D",["","color:red;","",""]);
在onload事件中調用
grid.load("grid.xml",function(){
grid.attachFooter ('A,B,C');
grid.attachFooter ('G,H,I');
grid.setSizes();//文檔上說這里必須加上,但沒發現其必要性
});
1.1.3 attachHeader(values, style)
版本:大眾版
參數:
values:增加行的每個單元格值,以數組的形式給出,這里支持html的值表示
style:單元格的樣式
用途:
定義grid的表頭,注意當前表頭不會隨上下滾動條一起移動,并設置各單元數據和格樣式
具體運用與attachHeader類似
1.1.4 attachToObject(obj)
版本:大眾版
參數:
Obj:當前綁定的grid的對象
用途:
將當前定義grid對象重新綁定到某個容器中,可實現grid在頁面上容器間(如div)的動態切換,好像不能重新綁定到原有的容器定義,使用原有的容器僅是display=none而已,因為:通過alert 容器的innerHTML發現,原有容器和新綁定容器值一致
參考實例:
<!—原有容器-->
<div id="listdiv" style=" border-style:solid;width:100%;height:316px;"></div>
<table width="700">
<tr>
<td width="50%" valign="top">
<!—可綁定新容器-->
<div id="gridbox" style="width=350px;height:150px;background-color:white;"></div>
</td>
<td valign="top">
<!—可綁定新容器-->
<div id="gridbox2" style="width=350px;height:150px;background-color:white;border:1px solid blue;" align="right"></div>
</td>
</tr>
<table>
<input type="button" onclick="dotacche()" value="00000"/>
<input type="button" onclick="doctacche()" value="1111"/>
<script>
function dotacche() {
ckmygrid.attachToObject(document.getElementById("gridbox2"));
}
function doctacche() {
ckmygrid.attachToObject(document.getElementById("gridbox"));
// ckmygrid.attachToObject(document.getElementById("listdiv"));這里執行沒有效果
}
</script>
1.1.5 destructor
版本:大眾版
參數:
Obj:當前綁定的grid的對象
用途:
徹底銷毀當前grid在頁面中的使用,并釋放其對象占用的資源(如js數組置空等),若重新使用,必須通過init創建,有別于clearAll,后者僅把grid中的所有行刪除,grid本身還可以進行數據的重填充。
這里也可以采用比較暴力的銷毀方式,其grid負載的容器.innerHTML = “ ”;即可,但這樣grid創建的全局js變量沒有完成銷毀過程
參考實例:無
1.1.6 detachEvent(id)
版本:大眾版
參數:
id 事件序號,全局唯一
用途:
刪除grid中某個事件的處理過程
參考實例:無
1.1.7 detachFooter(index)
版本:專業版
參數:
index 表腳索引
用途:
刪除grid的某個表腳,與attachFooter配對使用
參考實例:無
(注:本人文章均為原創,轉載請注明出處!20100620寫于深圳。)