extjs remove duplicate value before storing it into a store

[亡魂溺海] 提交于 2019-12-24 04:47:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!