Store.getById()得到Undefined

store.getById()要能成功得到record,需要有两个条件:
1、store的config Options中设置了id属性
2、创建record是要说明id属性
后者是时常被遗漏的要注意

引用

Record( Array data, [Object id] )
This constructor should not be used to create Record objects. Instead, use the constructor generated by create. The parameters are the same.
Parameters:
  data : Array
     An associative Array of data values keyed by the field name.
  id : Object
    (Optional) The id of the Record. This id should be unique, and is used by the Ext.data.Store object which owns the Record to index its collection of Records. If not specified an integer id is generated.



例如
Ext.BLANK_IMAGE_URL = '../resources/images/default/s.gif'; 
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';
/*sequece*/
var id=0;
/**屏蔽系统右键菜单*/
Util.forbidContextMenu(); 
Ext.onReady(function(){
	var sm=new Ext.grid.CheckboxSelectionModel({});
	var cm=new Ext.grid.ColumnModel([
		sm,
		{
			header:"URL",
			sortable:true,
			dataIndex:'url',
			editor:new Ext.form.TextField({
				vtype:"url",
				allowBlank:false
			})
		},
		{	
			header:'删除',
			renderer:delRender,
			sortable:false,
			menuDisabled:true,
			dataIndex:'id'
		}
	]);
	var ds=new Ext.data.JsonStore({
		data:{data:[],number:0},
		root:'data',
		storeId:"strBlackUrl",
		id:"id",
		totalProperty: 'number',
		fields:['id','url']
	});

	var grid=new Ext.grid.EditorGridPanel({
		sm:sm,
		cm:cm,
		ds:ds,
		title:'黑名单网址列表',
		tbar:[
			'->',
			btnFactory('add'),
			'-',
			btnFactory('del'),
			'-',
			{
				text:'保存',
				cls:'x-btn-text-icon',
				icon:'../images/backup.gif',
				handler:saveHandler
			}
		]
	});

	var viewport=new Ext.Viewport({
		layout:'fit',
		id:'viewport',
		items:[
			grid
		]
	});
})
BlackUrl=Ext.data.Record.create([
	{name:"url",mapping:"url"},
	{name:"id",mapping:"id",id:true}
]);
function addHandler()
{
	var record=new BlackUrl({url:"http://www.test.com",id:id},id);
	id++;
	Ext.StoreMgr.lookup("strBlackUrl").add(record);
}
function delHandler(id)
{
	var store=Ext.StoreMgr.lookup("strBlackUrl");
	var record=store.getById(id);
	store.remove(record);
}
function saveHandler()
{
	
}


若没有提供第二个可选参数 var record=new BlackUrl({url:"http://www.test.com",id:id},id);
函数delHandler(id)中的store。getById()就会报错

你可能感兴趣的:(ext,prototype)