我尝试在将重复值存储到商店之前将其删除。我想只在商店中存储相同的值1次。但似乎以下Ext.Array.unique行不起作用。有谁可以帮我纠正这个。 谢谢
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());
}
}
});
答案 0 :(得分:5)
您可以使用filterBy
方法过滤掉加载后不希望在商店中显示的记录。
请注意,商店将保留已过滤记录的副本,如果调用clearFilter
(可能由您或使用商店的组件),它将恢复。如果你想彻底摆脱这些记录,你将不得不删除该副本。
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
},
},
})
答案 1 :(得分:1)
您可以在商店中指定idProperty
值(idProperty =='name')
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Model-cfg-idProperty