實現(xiàn)Ext表單對checkBoxGroup的統(tǒng)一管理
1 對于類型是checkboxgroup的數(shù)據(jù),數(shù)據(jù)庫中保存數(shù)據(jù)的格式是value1,value2...valueN,其中1~N的數(shù)據(jù)有可能不存在,如果選中則存在,最后拼接成一個串。在Ext中,通過Record對象向FormPanel中的內(nèi)置對象BasicForm加載數(shù)據(jù)時,采用的是setValues方法,而setValues第一步要通過Record中定義的name使用findField方法找到表單元素,遺憾的是,繼承了Field的checkboxgroup組件并不能正確的通過getName返回自身引用,所以,需要對getName方法進行重寫,此外,為了適應(yīng)我們采用的數(shù)據(jù)格式,對于該組件的setValue(被setValues調(diào)用)和getValue(獲取到已加工的數(shù)據(jù),此事后話)也要進行重寫。故而對于形如:
{
xtype: 'checkboxgroup',
name: 'biztype',
width: 220,
columns: 3,
fieldLabel: '業(yè)務(wù)類別',
items: [
{boxLabel: '類別1', inputValue: '01'},
{boxLabel: '類別2', inputValue: '02'},
{boxLabel: '類別3', inputValue: '03'},
{boxLabel: '類別4', inputValue: '04'}
]
}
xtype: 'checkboxgroup',
name: 'biztype',
width: 220,
columns: 3,
fieldLabel: '業(yè)務(wù)類別',
items: [
{boxLabel: '類別1', inputValue: '01'},
{boxLabel: '類別2', inputValue: '02'},
{boxLabel: '類別3', inputValue: '03'},
{boxLabel: '類別4', inputValue: '04'}
]
}
的checkboxgroup定義,需重寫類如下:
Ext.override(Ext.form.CheckboxGroup,{
//在inputValue中找到定義的內(nèi)容后,設(shè)置到items里的各個checkbox中
setValue : function(value){
this.items.each(function(f){
if(value.indexOf(f.inputValue) != -1){
f.setValue(true);
}else{
f.setValue(false);
}
});
},
//以value1,value2的形式拼接group內(nèi)的值
getValue : function(){
var re = "";
this.items.each(function(f){
if(f.getValue() == true){
re += f.inputValue + ",";
}
});
return re.substr(0,re.length - 1);
},
//在Field類中定義的getName方法不符合CheckBoxGroup中默認的定義,因此需要重寫該方法使其可以被BasicForm找到
getName : function(){
return this.name;
}
});
//在inputValue中找到定義的內(nèi)容后,設(shè)置到items里的各個checkbox中
setValue : function(value){
this.items.each(function(f){
if(value.indexOf(f.inputValue) != -1){
f.setValue(true);
}else{
f.setValue(false);
}
});
},
//以value1,value2的形式拼接group內(nèi)的值
getValue : function(){
var re = "";
this.items.each(function(f){
if(f.getValue() == true){
re += f.inputValue + ",";
}
});
return re.substr(0,re.length - 1);
},
//在Field類中定義的getName方法不符合CheckBoxGroup中默認的定義,因此需要重寫該方法使其可以被BasicForm找到
getName : function(){
return this.name;
}
});
2 通過內(nèi)置對象basicForm的getValues方法可以獲取到一個form的完整json數(shù)據(jù),但遺憾的事,這里取到的是dom的raw數(shù)據(jù),類似emptyText的數(shù)據(jù)也會被返回,而Field的getValue方法不存在這個問題,所以如果想要返回一個非raw的json集合,可以給formpanel添加如下方法:
getJsonValue:function(){
var param = '{';
this.getForm().items.each(function(f){
var tmp = '"' + f.getName() + '":"' + f.getValue() + '",';
param += tmp;
});
param = param.substr(0,param.length - 1) + '}';
return param;
}
var param = '{';
this.getForm().items.each(function(f){
var tmp = '"' + f.getName() + '":"' + f.getValue() + '",';
param += tmp;
});
param = param.substr(0,param.length - 1) + '}';
return param;
}
這個方法同樣適用于上面定義的checkboxgroup,如此就可以把前后臺的數(shù)據(jù)通過json統(tǒng)一起來了
@2008 楊一. 版權(quán)所有. 保留所有權(quán)利
posted on 2009-03-04 12:50 楊一 閱讀(2238) 評論(0) 編輯 收藏 所屬分類: Other Tech