8 Sept 2014

Distinct, Refilter or Remove duplicate value in ComboBox Extjs

The issue is we cannot distinct value from SQL server, or Server code because this function is used in other services.
To solve it we use filterBy function of Store.

me.store = new Ext.data.DirectStore({
                directFn: Combo.Remote.LoadPrincipalByQuotation,
                autoLoad: true,
                root: 'data',
                fields: ['PrincipalId', 'PrincipalName', 'QuotationId'],
                filters: [{
                    property: 'QuotationId', value: me.quotationId
                }],
                listeners: {
                    load: function (obj, records, successful, operation, eOpts) {
                        var hits = {};
                        obj.filterBy(function (record) {
                            var name = record.get('PrincipalId');
                            if (hits[name]) {
                                return false;
                            } else {
                                hits[name] = true;
                                return true;
                            }
                        });

                        // delete the filtered out records
                        delete obj.snapshot;

                        if (records != null && records.length > 0 && me.selectFirst)
                            me.setValue(records[0].data.PrincipalId, true);  
                    }
                }
            });

Enjoy IT!

No comments: