PDA

View Full Version : How to check whether record already exists in the Store?



dev_java
13 Sep 2010, 8:16 PM
Hi,

I have a scenario like in a store i have 2 entries and i will add new entries on click of 'Add' button. .If the record already exists in the store, alert message has to be shown as 'duplicates not allowed'.
I tried using store.indexOf() , but this is always returning -1. I don't want to iterate through the records and find the record.

Here is my code snippet

Store code:


alarmStore = new Ext.data.Store({
proxy: new Ext.data.HttpProxy(
new Ext.data.Connection({
url:'../demo/showAlarm.htm'
})
),
reader:new Ext.data.JsonReader({
totalProperty:'almSize',
root:'almList',
fields: [
'id',
'type',
'value',
'severity',
'enable',
'mode'
]
})
});
alarmStore.setDefaultSort('value', 'asc');



Button handler code


buttons: [
{
text:'Add',
handler: function(){
var almValue = alarmStore.recordType;
var almMap = new almValue({
id:3,
type:strType,
value:strClli,
severity:Ext.getCmp('severity').getValue(),
enable:Ext.getCmp('enable').getValue(),
mode:'add'
});
alert('Found : '+alarmStore.indexOf(almMap));// This is always returning -1
if(alarmStore.indexOf(almMap)== -1){
Ext.getCmp('alarmgrid').stopEditing();
alarmStore.addSorted(almMap);
Ext.getCmp('alarmgrid').startEditing(0, 0);
}
}
}
]



Can any one help me out on this. Is there any other way to get check whether record already exists in the store.

Thanks

dev_java
13 Sep 2010, 8:25 PM
Hi,

I have a scenario like in a store i have 2 entries and i will add new entries on click of 'Add' button. .If the record already exists in the store, alert message has to be shown as 'duplicates not allowed'.
I tried using store.indexOf() , but this is always returning -1. I don't want to iterate through the records and find the record.

Here is my code snippet

Store code:


alarmStore = new Ext.data.Store({
proxy: new Ext.data.HttpProxy(
new Ext.data.Connection({
url:'../demo/showAlarm.htm'
})
),
reader:new Ext.data.JsonReader({
totalProperty:'almSize',
root:'almList',
fields: [
'id',
'type',
'value',
'severity',
'enable',
'mode'
]
})
});
alarmStore.setDefaultSort('value', 'asc');



Button handler code


buttons: [
{
text:'Add',
handler: function(){
var almValue = alarmStore.recordType;
var almMap = new almValue({
id:3,
type:strType,
value:strClli,
severity:Ext.getCmp('severity').getValue(),
enable:Ext.getCmp('enable').getValue(),
mode:'add'
});
alert('Found : '+alarmStore.indexOf(almMap));// This is always returning -1
if(alarmStore.indexOf(almMap)== -1){
Ext.getCmp('alarmgrid').stopEditing();
alarmStore.addSorted(almMap);
Ext.getCmp('alarmgrid').startEditing(0, 0);
}
}
}
]



Can any one help me out on this. Is there any other way to get check whether record already exists in the store.

Thanks

Animal
13 Sep 2010, 10:34 PM
Well a brand new Record which you just CREATED won't exist. Will it?

Look up the ID!

Animal
13 Sep 2010, 10:36 PM
Unfortunately, your Records won't HAVE a known ID because you did NOT tell the Reader which property to USE as the ID!

dev_java
14 Sep 2010, 4:50 AM
Sorry.. I dint get this. How to tel reader which property to use as id. Is there any other way to check whether record already exists in the store ?

Condor
14 Sep 2010, 6:08 AM
You didn't specify an id for the record (and no, specifying the id field isn't enough - have a look at the Record contructor parameters).