问题
I try to remove the duplicate value before storing it into a store. I want to store the same value in the store 1 time only. But it seems the following Ext.Array.unique line does not working. Could anyone please help me to correct this. Thank you
var input1store = new Ext.data.Store({
fields: [{name: 'name'}],
proxy: {
type: 'ajax',
url: 'www.requesturl.com?format=json&source1',
reader: {
type: 'json',
root: 'xml.result'
}
},
autoLoad: false,
sorters: [{property: 'name', direction: 'asc'}],
listeners:{
load: function(rec){
uniqueArray = Ext.Array.unique(rec.getRange());
}
}
});
回答1:
You can use the filterBy method to filter out records that you don't want to appear in the store after loading.
Note that the store will keep a copy of the filtered out records, that it would restore if clearFilter is called (that could be by you or the component using the store). If you want to get rid definitively of these records, you'll have to delete that copy.
Ext.create('Ext.data.Store', {
fields: ['name'],
// example data
proxy: {
type: 'memory',
data: [{name: 'Foo'},{name:'Bar'},{a:'Baz'},{a:'Foo'},{a:'Bar'}],
},
listeners: {
load: function(store) {
// using a map of already used names
const hits = {}
store.filterBy(record => {
const name = record.get('name')
if (hits[name]) {
return false
} else {
hits[name] = true
return true
}
})
// delete the filtered out records
delete store.snapshot
},
},
})
回答2:
You can specify the idProperty value (idProperty == 'name') in your store
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Model-cfg-idProperty.
来源:https://stackoverflow.com/questions/16947624/extjs-remove-duplicate-value-before-storing-it-into-a-store