-
6 Dec 2011 6:31 AM #1
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, });Can you help lead me in the right direction? Any other tips are welcome.Code:}, { xtype: 'selectfield', name: 'TruckOM', store: TruckStore, displayField: 'OM', valueField: 'OM' }, {
-
Best Answer Posted by runner
This works but seems like an awful way to accomplish a simple task...
Now I just references var B in my selectfieldCode: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]};}
Code:}, {xtype: 'selectfield',name: 'Truck',options: B
-
6 Dec 2011 7:19 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
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
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
6 Dec 2011 3:32 PM #3
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.
-
7 Dec 2011 1:18 PM #4
Round About Solution
Round About Solution
This works but seems like an awful way to accomplish a simple task...
Now I just references var B in my selectfieldCode: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]};}
Code:}, {xtype: 'selectfield',name: 'Truck',options: B
-
16 Dec 2011 5:59 AM #5
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
-
8 Apr 2012 11:21 PM #6
Hi
Thanks.. Its helpful.
How this can be done in ST2.
we dont have store.collect method there.
-
9 Apr 2012 3:48 AM #7Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
26 Feb 2013 5:10 PM #8
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
And thenCode: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 }) },
Code:filterData: function(store, params) { var filtered = store.each(function(obj) { if (obj...) { //has not already been seen // add it to my view! } }) },


Reply With Quote