1. #1
    Sencha User
    Join Date
    Feb 2011
    Posts
    6
    Vote Rating
    0
    Answers
    1
    runner is on a distinguished road

      0  

    Question Answered: Data Store - Unique Entries

    Answered: Data Store - Unique Entries


    Working on my first Sencha Touch App and have run into the following problem:

    I want to create a combo box with unique 'OM' values. When selected, a second combo box will filter unique 'Model' values based on the 'OM' pick. I found a nice post about dynamically generated combo boxes but am having trouble finding a clear example of how to show only unique values. My search has led me to 'collect' but have been unsuccessful at making this work so far.

    My data file looks similar to this (2000+ entries):

    Code:
    truckData = [ 
            { 
                    "OM": "BALKANCAR", 
                    "Model": "D 16", 
                    "iso": "II", 
                    "c": "500", 
                    "x": "445", 
                    "qc": "1600", 
                    "fw": "100", 
                    "ft": "40", 
                    "fl": "1150" 
            },{ 
                    "OM": "BALKANCAR", 
                    "Model": "D 20", 
                    "iso": "II", 
                    "c": "500", 
                    "x": "475", 
                    "qc": "2000", 
                    "fw": "80", 
                    "ft": "45", 
                    "fl": "1200" 
            },
    ...

    My HTML files contains the following parts:
    Code:
    <script type="text/javascript"> 
            var truckData; 
            function cachedata(dataset){ 
                    truckData = dataset; 
            } 
    </script>
    Code:
    Ext.regModel('Truck', { 
            idProperty: 'TruckID', 
            fields: [{ 
                    name: 'OM', 
                    type: 'string' 
            }, { 
                    name: 'Type', 
                    type: 'string' 
            }] 
    }); 
            
    var TruckStore = new Ext.data.Store({ 
            model: 'Truck', 
            data: truckData, 
            autoLoad: true, 
    });
    Code:
    }, { 
            xtype: 'selectfield', 
            name: 'TruckOM', 
            store: TruckStore, 
            displayField: 'OM', 
            valueField: 'OM' 
    }, {
    Can you help lead me in the right direction? Any other tips are welcome.

  2. This works but seems like an awful way to accomplish a simple task...

    Code:
    var A = TruckStore.collect('OM'); 
    var B = new Array();
    for (var i = 0; i < A.length; i++) {
    B[i] = {"text": A[i], "value": A[i]};
    }
    Now I just references var B in my selectfield

    Code:
    }, {
    xtype: 'selectfield',
    name: 'Truck',
    options: B

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,547
    Vote Rating
    873
    Answers
    3566
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    collect doesn't do anything special:

    Code:
        collect : function(dataIndex, allowNull, bypassFilter) {
            var values  = [],
                uniques = {},
                length, value, strValue, data, i;
    
            if (bypassFilter === true && this.snapshot) {
                data = this.snapshot.items;
            } else {
                data = this.data.items;
            }
    
            length = data.length;
    
            for (i = 0; i < length; i++) {
                value = data[i].data[dataIndex];
                strValue = String(value);
    
                if ((allowNull || !Ext.isEmpty(value)) && !uniques[strValue]) {
                    uniques[strValue] = true;
                    values[values.length] = value;
                }
            }
    
            return values;
        }
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    Feb 2011
    Posts
    6
    Vote Rating
    0
    Answers
    1
    runner is on a distinguished road

      0  

    Question Example Pleas

    Example Pleas


    Let me back up a bit... I'm new to Sencha as well as web app programming.

    I see you cut and pasted the source code from the online documentation.

    I'm looking for an example of how to use 'collect' and get the array returned in a combo box.

    In other words, how do I take the data in my store:
    var TruckStore = new Ext.data.Store({
    model: 'Truck',
    data: truckData,
    autoLoad: true,
    });
    and use it in the following combo box with only unique values.
    , {
    xtype: 'selectfield',
    name: 'TruckOEM',
    store: TruckStore,
    displayField: 'OEM',
    valueField: 'OEM'
    }]
    I'm sure this is probably easy but I need a little more help walking through it. Thanks.

  5. #4
    Sencha User
    Join Date
    Feb 2011
    Posts
    6
    Vote Rating
    0
    Answers
    1
    runner is on a distinguished road

      0  

    Thumbs up Round About Solution

    Round About Solution


    This works but seems like an awful way to accomplish a simple task...

    Code:
    var A = TruckStore.collect('OM'); 
    var B = new Array();
    for (var i = 0; i < A.length; i++) {
    B[i] = {"text": A[i], "value": A[i]};
    }
    Now I just references var B in my selectfield

    Code:
    }, {
    xtype: 'selectfield',
    name: 'Truck',
    options: B

  6. #5
    Sencha User
    Join Date
    Oct 2011
    Posts
    26
    Vote Rating
    0
    gopikumar is on a distinguished road

      0  

    Default struck up with same prob

    struck up with same prob


    Hi runner

    Even i ve the same prob.

    From the store I want to populate in a list.

    Can u explain me how u achieve this.
    thanks

  7. #6
    Sencha User
    Join Date
    Mar 2012
    Posts
    19
    Vote Rating
    0
    Answers
    1
    vaish is on a distinguished road

      0  

    Default


    Hi

    Thanks.. Its helpful.

    How this can be done in ST2.
    we dont have store.collect method there.

  8. #7
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,547
    Vote Rating
    873
    Answers
    3566
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Quote Originally Posted by vaish View Post
    Hi

    Thanks.. Its helpful.

    How this can be done in ST2.
    we dont have store.collect method there.
    Use store.each() to iterate through the store and get the unique fields there.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  9. #8
    Sencha User
    Join Date
    Feb 2013
    Posts
    1
    Vote Rating
    0
    tuw is on a distinguished road

      0  

    Default How to get only unique data

    How to get only unique data


    You mention to use the call the each() function on the store, but how can you then get only the unique values?

    I am so used to just being able to use a set which I could add to to preserve the uniqueness of the elements.
    This is basically what im looking at
    Code:
    showAlbums: function(list, record) {        var store = Ext.StoreManager.get('Songs');
            var params = "album"; // this is temporary until this.getParams() is made
            var data = this.filterData(store, params);
            this.getMain().push({
                xtype: 'musicdataalbums',
                data: data
            })
    },
    And then

    Code:
    filterData: function(store, params) {        var filtered = store.each(function(obj) {
                if (obj...) {     //has not already been seen
                            // add it to my view!
                }
            })
    },