1 對于類型是checkboxgroup的數據,數據庫中保存數據的格式是value1,value2...valueN,其中1~N的數據有可能不存在,如果選中則存在,最后拼接成一個串。
在Ext中,通過Record對象向FormPanel中的內置對象BasicForm加載數據時,采用的是setValues方法,而setValues第一步要通過Record中定義的name使用findField方法找到表單元素,遺憾的是,繼承了Field的checkboxgroup組件并不能正確的通過getName返回自身引用,所以,需要對getName方法進行重寫,此外,為了適應我們采用的數據格式,對于該組件的setValue(被setValues調用)和getValue(獲取到已加工的數據,此事后話)也要進行重寫。故而對于形如:
{
xtype: 'checkboxgroup',
name: 'biztype',
width: 220,
columns: 3,
fieldLabel: '業務類別',
items: [
{boxLabel: '類別1', inputValue: '01'},
{boxLabel: '類別2', inputValue: '02'},
{boxLabel: '類別3', inputValue: '03'},
{boxLabel: '類別4', inputValue: '04'}
]
}
的checkboxgroup定義,需重寫類如下:
Ext.override(Ext.form.CheckboxGroup,{
//在inputValue中找到定義的內容后,設置到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內的值
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 通過內置對象basicForm的getValues方法可以獲取到一個form的完整json數據,但遺憾的事,這里取到的是dom的raw數據,類似emptyText的數據也會被返回,而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;
}
這個方法同樣適用于上面定義的checkboxgroup,如此就可以把前后臺的數據通過json統一起來了
@2008 楊一. 版權所有. 保留所有權利