现在的位置: 首页 > 综合 > 正文

Extjs 解决store.getUpdatedRecordes()获取不到数据

2014年03月27日 ⁄ 综合 ⁄ 共 2536字 ⁄ 字号 评论关闭

Extjs3.x中是store.getModifiedRecordes(),

用editgridpanel来获取被修改的记录,可是怎么也获取不到,深究才知道

store类的getUpdatedRecords 会通过 model类的三个条件 

1,dirty == true       2, phantom !== true     3,isValid()    

来确定一个record是否被更新过。条件1和3没问题,问题就出在了条件2上。

当一个record(model类的实例) 存在于服务器端数据库中时,其phantom 为false,否则为true,

对于Ext来说,通过model 类的idProperty属性指定的 数据域来判断该record是否存在于服务器端数据库。

idProperty默认值为id,即如果record中id值不为空,则phantom=fasle,否则为true。

     解决方法:1 模型中添加一个id字段,给一个id属性。2 idProperty 设置为fields中的一个,例如userName。  相当于一个主键,用于识别

Ext.onReady(function(){
	Ext.QuickTips.init();  
	Ext.form.Field.prototype.msgTarget='side';
	Ext.regModel("userInfo",{
		idProperty:'Name',
		fields:['Name','Password','RPassword','Email'],
		proxy:{
		       type:'memory',
		       reader:{
		          type:'json',
		          root:'items'
		       }
		    }
	});
	
	var store=Ext.create('Ext.data.Store',{
		storeId:'gridStore',
		autoLoad:true,
	    model:'userInfo',
	    data:{'items':[
	         {'Name':'11','Password':'11','RPassword':'11','Email':'11'},  	
	    ]},
	    
		
	});	
	var gridPanel=Ext.create('Ext.grid.Panel',{
		id:'gridPanel',
		autoScroll:true,
		height:110,
	    store:store,
	    plugins:[
	       Ext.create('Ext.grid.plugin.CellEditing',{
	            clicksToEdit:1
	       })
	    ],
	    columns:[
	      {header:'用户名',dataIndex:'Name',flex:1,
	         editor:{
	            xtype:'textfield',
	            name:'username'
	         }
	      },
	      {header:'密码',dataIndex:'Password',flex:1,
	         editor:{
	            xtype:'textfield',
	            name:'password1',
	            id:'password1',
	            inputType:'password',
	         }
	      },
	      {header:'确认密码',dataIndex:'RPassword',flex:1,
	         editor:{
	            xtype:'textfield',
	            name:'password2',
	            inputType:'password'
	         }
	      },
	      {header:'邮箱',dataIndex:'Email',flex:1,
	         editor:{
	            xtype:'textfield',
	            name:'email'
	         }
	      },
	    ],
	    listeners:{
		   edit:function(editor,eOptions){
		      var store=Ext.data.StoreManager.lookup("gridStore");
		      var lastRecord=store.last();
		      var temp=lastRecord.get('Name')+lastRecord.get('Password')
		              +lastRecord.get('RPassword')+lastRecord.get('Email');
		      temp=Ext.util.Format.trim(temp);
		      if(temp!=""){
			      var newRecord={'Name':'','Password':'','RPassword':'','Email':''};
			      store.add(newRecord);
		      }
		     
	       }
		
	    }
	    
		
	});
	
	
	
		//注册表单
	var formPanel=Ext.create('Ext.form.Panel',{
		id:"formPanel",
		width:700,
		frame:true,
		style:{
			position:'relative',
		    margin:'30px auto',
			border:'0'
		},
		renderTo:'content',
		defaults:{
		   xtype:'textfield',
		   labelSeperator:':',
		   labelAlign:'right',
		   style:{
		     marginBottom:'10px'
		   },
		   allowBlank  : false,//不允许为空  
           blankText   : "不能为空"
		},
		items:[gridPanel],
		buttons:[{
		   text:'注册',
		   handler:login
		},{
		   text:'取消'
		}]
		
		
	});
	
	function login(){
		var records=store.getUpdatedRecords()[0];
	    
		Ext.Msg.alert("www ok",store.getUpdatedRecords().length+"     "+records.get("Name"));	
		
	}

});

还可以通过model.dirty来判断是否修改过

var store = grid.getStore();
        var result = [];
        for(var i =0;i<store.getTotalCount();i++){
              var model = store.getAt(i);
              if(model.dirty){
                 result.push(model);
              }
        }
        alert(result.length);
dirty : Boolean

Readonly flag - true if this Record has been modified.

抱歉!评论已关闭.