关于lazyTreeGrid增加checkbox

   没有太好的办法,只能用formatter属性来做。下面是我写的一个完整的实现方法
   1  function( value,rowIndex,cells)
 2 {    var rowdata = this.grid.getItem(rowIndex);
 3     var rowvalue = rowdata.checked.toString();
 4     var first = "<div id='"+rowdata.id+"' style='height:15px;width:15px;float:left'";
 5     var last =  "onclick='OnChangeCheck("+rowIndex+","+this.grid.id+")'>  </div>"+value;
 6     if(rowvalue=='true'){
 7         return first+" class='sd-icon-checked' onmouseover ='oncheckover(this)' onmouseout='oncheckout(this)'"+last;
 8     }else if(rowvalue=='mix'){
 9         return first+" class='sd-icon-checkmix' onmouseover ='oncheckmixover(this)' onmouseout='oncheckmixout(this)'"+last;
10     }
11         return first+"class='sd-icon-nochecked' onmouseover ='onnocheckover(this)' onmouseout='onnocheckout(this)'"+last;
12         
13     
14 };
15 function oncheckmixout(e) {
16     e.className = "sd-icon-checkmix";
17  }
18 function oncheckmixover(e) {
19     e.className = "sd-icon-overcheckmix";
20  }
21 function onnocheckout(e) {
22     e.className = "sd-icon-nochecked";
23  }
24 function oncheckout(e) {
25     e.className = "sd-icon-checked";
26  }
27 function onnocheckover(e) {
28         e.className = "sd-icon-overnochecked";
29      }
30 function oncheckover(e) {
31     e.className = "sd-icon-overchecked";
32  }
33 function OnChangeCheck(rowIndexs,id)
34 {
35     var store=dijit.byId(id).store;
36     var rowdata = dijit.byId(id.toString()).getItem(rowIndexs);
37 
38     
39     if(rowdata.checked=='true'){
40         store.setValue(rowdata,"checked",'false');
41         if(rowdata.children!=null){
42             findChildrenItem(rowdata,'false',id);
43         }
44     }
45     else{
46         store.setValue(rowdata,"checked",'true');
47         if(rowdata.children!=null){
48             findChildrenItem(rowdata,'true',id);
49         }
50     }
51     var parentItem= getTreeParentItem(id,rowdata);
52     if(parentItem!=null){
53         findParentItem(parentItem,id);
54     }
55     
56 }
57 
58 function findChildrenItem(thisItem,value,id){
59     var store=dijit.byId(id).store;
60      for(var k=0;k<thisItem.children.length;k++){
61          
62          var theChildren = thisItem.children[k];
63          store.setValue(theChildren,"checked",value);
64          if(theChildren.children!=null){
65              findChildrenItem(theChildren,value,id);
66          }
67               
68     }
69     
70 }
71 function findParentItem(parentItem,id){
72     var store=dijit.byId(id).store;
73     var parentChecked = 0;
74     if(parentItem!=null){
75          var children = parentItem.children;
76          for(var i=0;i<children.length;i++)
77          {    
78              if(children[i].checked.toString()!='true'){
79                  if(children[i].checked.toString()=='mix'){parentChecked +=0.5;}
80                  else{parentChecked +=1;};
81              }
82          }
83          if(!parentChecked)
84          {
85              store.setValue(parentItem,"checked",'true');
86          }else{
87              if(parentChecked!=parentItem.children.length||parentChecked!=parseInt(parentChecked)){ 
88                  store.setValue(parentItem,"checked",'mix');
89                  }
90              else{
91                  store.setValue(parentItem,"checked",'false');
92                  };
93          }
94     }
95     var superParent=            PM.addCheckbox = function( value,rowIndex,cells)
{ var rowdata = this.grid.getItem(rowIndex);
var rowvalue = rowdata.checked.toString();
var first = "<div id='"+rowdata.id+"' style='height:15px;width:15px;float:left'";
var last =  "onclick='OnChangeCheck("+rowIndex+","+this.grid.id+")'>  </div>"+value;
if(rowvalue=='true'){
return first+" class='sd-icon-checked' onmouseover ='oncheckover(this)' onmouseout='oncheckout(this)'"+last;
}else if(rowvalue=='mix'){
return first+" class='sd-icon-checkmix' onmouseover ='oncheckmixover(this)' onmouseout='oncheckmixout(this)'"+last;
}
    return first+"class='sd-icon-nochecked' onmouseover ='onnocheckover(this)' onmouseout='onnocheckout(this)'"+last;
   
    
};
function oncheckmixout(e) {
e.className = "sd-icon-checkmix";
 }
function oncheckmixover(e) {
e.className = "sd-icon-overcheckmix";
 }
function onnocheckout(e) {
e.className = "sd-icon-nochecked";
 }
function oncheckout(e) {
e.className = "sd-icon-checked";
 }
function onnocheckover(e) {
e.className = "sd-icon-overnochecked";
}
function oncheckover(e) {
e.className = "sd-icon-overchecked";
 }
function OnChangeCheck(rowIndexs,id)
{
var store=dijit.byId(id).store;
var rowdata = dijit.byId(id.toString()).getItem(rowIndexs);
if(rowdata.checked=='true'){
store.setValue(rowdata,"checked",'false');
if(rowdata.children!=null){
findChildrenItem(rowdata,'false',id);
}
}
else{
store.setValue(rowdata,"checked",'true');
if(rowdata.children!=null){
findChildrenItem(rowdata,'true',id);
}
}
var parentItem= getTreeParentItem(id,rowdata);
if(parentItem!=null){
findParentItem(parentItem,id);
}
}
function findChildrenItem(thisItem,value,id){
var store=dijit.byId(id).store;
for(var k=0;k<thisItem.children.length;k++){
 
var theChildren = thisItem.children[k];
store.setValue(theChildren,"checked",value);
if(theChildren.children!=null){
findChildrenItem(theChildren,value,id);
}
 
}
}
function findParentItem(parentItem,id){
var store=dijit.byId(id).store;
var parentChecked = 0;
if(parentItem!=null){
var children = parentItem.children;
for(var i=0;i<children.length;i++)
{
if(children[i].checked.toString()!='true'){
if(children[i].checked.toString()=='mix'){parentChecked +=0.5;}
else{parentChecked +=1;};
}
    }
    if(!parentChecked)
    {
    store.setValue(parentItem,"checked",'true');
    }else{
    if(parentChecked!=parentItem.children.length||parentChecked!=parseInt(parentChecked)){ 
    store.setValue(parentItem,"checked",'mix');
    }
    else{
    store.setValue(parentItem,"checked",'false');
    };
    }
}
var superParent= getTreeParentItem(id,parentItem);
if(superParent!=null){
findParentItem(superParent,id);
}

}(id,parentItem);
96     if(superParent!=null){
97         findParentItem(superParent,id);
98     }
99 }
    写的可能比较繁琐,没有优化,是用图片控制的,因为formatter函数中不能使用dojo 的组件,这有点遗憾。图片在dojo包的\dojox\form\resources\images中,自己找一下。getTreeParentItem是公司封装的方法,查找父节点,这里就不贴了,网上找一找就行,希望对有这类任务的人有帮助

posted on 2012-07-19 15:22 七孑 阅读(409) 评论(0)  编辑  收藏 所属分类: DOJO


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜