1. 程式人生 > >extjs4.1 動態載入 checkboxgroup 後checked 為true老是失效的問題+combobo自動選擇特定記錄

extjs4.1 動態載入 checkboxgroup 後checked 為true老是失效的問題+combobo自動選擇特定記錄

剛剛遇到這個問題,官方網上也有這個,不過好像沒有解決。

http://www.sencha.com/forum/showthread.php?228669

	{
							    xtype: 'checkboxgroup',
						        fieldLabel: '城市',
						        vertical: true,
						        allowBlank :true,
						        items: [],
						        listeners:{
						            render:function(view, opt){
						            	var cbg=this;
						            	var cityCheckBoxs=new Array();//生成一個空陣列用來裝所有的城市物件
						            	console.debug(view);
						            	var t=this;
						                Ext.Ajax.request({
						                	async:false,  //設定同步
											url:'../api/city/param/list.json?id='+self.bid,//請求路徑,需要手動輸入指定
											method:'get',
											success: function(response){
												    var citys=eval('('+response.responseText+')');
													/*for(var j=0;j<citys.length;j++){
														cityCheckBoxs.push(
														     new Ext.form.Checkbox({
																checked:citys[j].checked,
																boxLabel: '廣州', 
																inputValue: citys[j].id, 
																name: 'city' ,//這個是後臺接收的表單域,這裡的checkboxgroup都使用同一個name
															    id:citys[j].id
													         })
													    );
												    }*/
												    for(var j=0;j<citys.length;j++){
													    cbg.items.add(
													             new Ext.form.Checkbox({
																	checked:citys[j].checked,
																	boxLabel: citys[j].value, 
																	inputValue: citys[j].id, 
																	name: 'city' ,//這個是後臺接收的表單域,這裡的checkboxgroup都使用同一個name
																    id:citys[j].id
														         }));
												    }
												    cbg.doLayout();
												   // console.debug(checkbox);
											}
									    })
									   
									  /*  view.add(cityCheckBoxs);
									    //view.cancelLayout();
										view.doLayout();*/
						           }
						      }
						    }

            	   iconCls : 'icon-edit',
                   tooltip: '編輯',
                   handler: function(grid, rowIndex, colIndex, item) {
                      var rec = grid.getStore().getAt(rowIndex);
					  var formWin = Ext.create("Sharera.view.web.Document.FormEdit",{bid:rec.data.id});					  
				      var formObj=formWin.down('form').getForm();
		              var opt={errorReader : Ext.create('Sharera.system.FormErrorReader')};				                                  	
		              Ext.apply(formObj,opt);					      
		              var fstore = Ext.create("Sharera.store.web.Documentform");
                	  fstore.setUrlforForm(rec.data.id);
                	  fstore.load(
                			{
                        	    scope: this,
                        	    callback: function(records, operation, success) {
                        	    	formObj.loadRecord(records[0]);
                        	    }
                			}
                	  );	
                	  formWin.show();
var b=formWin.down("button[ref='save'] "); var store = grid.getStore(); b.on("click", function(){ if (formObj.isValid()) { formObj.submit({ waitTitle : '傳送資料', waitMsg : '正在儲存...', timeout : 1000, success : function(form, action) { Ext.Sharera.msg("提示",'修改成功'); formWin.close(); store.reload(); }, failure : function(form, action) { Ext.Sharera.error("錯誤",'修改失敗'); formWin.close(); } }); } }, this); }

Ext.define('Sharera.view.web.Document.FormEdit', {
	extend : 'Ext.window.Window',
	alias : 'widget.Documentformedit',
	title : '修改',
	width : 850,
	height : 700,
	autoShow : false, // 自動開啟

後臺給的資料

[{"id":1,"key":"gz","value":"廣州","areaCode":"020","checked":true},{"id":2,"key":"sz","value":"深圳","areaCode":"0755","checked":false},{"id":3,"key":"dg","value":"東莞","areaCode":"0769","checked":false},{"id":4,"key":"fs","value":"佛山","areaCode":"0757","checked":false}]


===============================================================================================================================

combobox的城市是後臺提供的,裡面有一個是被選中的。

因為後臺的city實體類沒有checked這個屬性,所以我後臺給他。

cityApi中的程式碼:

	/**
	 * 可接收不同引數返回相應的資料
	 * @return
	 */
	@GET
	@Path("/param/list")
	@GZIP
	@Produces({MediaType.APPLICATION_JSON})
	public List getParamList(@QueryParam("id") String id,@QueryParam("type") String type) {
		System.out.println("---"+id);
		if(id==null){
			return this.getService().getCityList();
		}
		if("combobox4product".equals(type)){
			return this.getService().getCityList4combobox(id);
		}
		return this.getService().getCityListByParam(id,null);
	}

cityService中的程式碼:
	//獲取城市列表資訊,經過封裝的格式,返回的資料給前臺的combobox
	public  List<CitySupport> getCityList4combobox(String id){
		List<City> cityList = this.getDao().findAll();
		List<CitySupport> citySupport=new ArrayList<CitySupport>();
		Product p=this.productService.getDao().findOne(Long.parseLong(id));
		for(City city : cityList){
			CitySupport cs=new CitySupport(city);
			if(city.getId()==p.getCity().getId()){
				 cs.setChecked(true);
			}
			citySupport.add(cs);
	}
		return citySupport;
	}
	
	//獲取城市列表資訊,經過封裝的格式,返回的資料給前臺的checkboxgroup

	public static class CitySupport extends City{
		private static final long serialVersionUID = 7051497115371661530L;
		private boolean checked;//為了前臺需要新增的,前臺可以有多選的功能。
		CitySupport(City city){
			this.areaCode=city.getAreaCode();
			this.checked=false;
			this.id=city.getId();
			this.key=city.getKey();
			this.value=city.getValue();
					
		}
		public boolean isChecked() {
			return checked;
		}
		public void setChecked(boolean checked) {
			this.checked = checked;
		}
		public static long getSerialversionuid() {
			return serialVersionUID;
		}
		
	}

返回的資料:
[{"id":1,"key":"gz","value":"騫垮窞","areaCode":"020","checked":false},{"id":2,"key":"sz","value":"深圳","areaCode":"0755","checked":true},{"id":3,"key":"dg","value":"廣州","areaCode":"0769","checked":false},{"id":4,"key":"fs","value":"浣涘北","areaCode":"0757","checked":false}]

深圳==true

model中:

	{name:'checked',type:'boolean'}//這個是前臺需要才加上去的,後臺沒有。

view中:
{
						       fieldLabel:'城市',
						       queryMode:'local',
						       name:'city',
						       xtype:'combobox',
						       displayField:'value',
						       store:'web.City',
						       listeners:{
						        	afterrender:function(cb,eOpts){
						        		var store=cb.getStore();
						        		store.setUrlforCombo('combobox4product',self.bid);
						        		store.load({
										    scope: this,
										    callback: function(records, operation, success) {
										        for(var i=0;i<records.length;i++){
										        	if(records[i].data.checked==true){
										        	  cb.setValue(records[i]);
										        	}
										        }
										    }
										});
						        	}
						       }
						 }

效果: