PDA

View Full Version : How to get the store columns by name?



Cyberangel67
10 Jan 2011, 3:37 PM
I am looking at the best method to get the column names, so that I can traverse over them.

For example I am looking at the filterBy for the store, and I was wondering how do a traverse over the columns to get the name to use for getting that value out of the record?

mitchellsimoens
10 Jan 2011, 3:46 PM
The fields (or as you are calling them columns) are stored in myStore.fields.


new Ext.data.Store({
fields: [
{ name: "firstname", type: "string" },
{ name: "lastname", type: "string" }
]
})

So you can then go through each array item (field) and get the name.


var fields = store.fields;
for (var i = 0, len = fields.length; i < len; i++) {
var field = fields[i];
var fieldName = field.name;
}

Cyberangel67
10 Jan 2011, 3:56 PM
but the store is not available inside the filterBy so would I just apply the same principle to the record?

mitchellsimoens
10 Jan 2011, 3:59 PM
When you use filterBy on a store, the function you use is scoped in the store so when you reference this inside the function, it will be the store.


store.filterBy(function(record) {
var fields = this.fields;
....
});

shenba
1 Nov 2013, 3:06 AM
i had tried it with my store

My Store name is temp.


var fields = temp.getAt(0).fields;for (var i = 0, len = fields.length; i < len; i++) { var field = fields[i]; var fieldName = field.name;}i received the error like that

Uncaught TypeError: Cannot read property 'name' of undefined

willigogs
1 Nov 2013, 6:08 AM
From your error, it is obvious that "var field" is undefined, or an incorrect object.

Start by debugging your variables to check they are what you think they are...

console.log(fields)
console.log(field)
etc...

shenba
3 Nov 2013, 10:59 PM
st = Ext.getStore('SecondaryOption');
fields = st.getAt(0).fields;
console.log(fields);

for (var i = 0; i< fields.length; i++) {
field = fields[i];
console.log(field);
fieldName = field.name;
}

shenba
3 Nov 2013, 11:00 PM
st = Ext.getStore('SecondaryOption');
fields = st.getAt(0).fields;
console.log(fields); /It shows an object that contains all columns name as items

for (var i = 0; i< fields.length; i++) {
field = fields[i];
console.log(field); // It shows an undefined
fieldName = field.name; // It show an error
}

error:Uncaught TypeError: Cannot read property 'name' of undefined

shenba
4 Nov 2013, 11:42 PM
Its not working

i had tried it with my store

My Store name is temp.


var fields = temp.getAt(0).fields;for (var i = 0, len = fields.length; i < len; i++) { var field = fields[i]; var fieldName = field.name;}i received the error like that

Uncaught TypeError: Cannot read property 'name' of undefined

willigogs
5 Nov 2013, 12:30 AM
You will need to debug this much further.

Make sure you are using Firebug, and also have the illuminations plugin installed:
http://www.illuminations-for-developers.com

If you debug your code again (simple console.log()) with these running, then you should see that objects you are logging are not what you are assuming them to be...

stephen.aaes
13 Nov 2013, 7:11 AM
Hi,

When you create a store, it's true that you can pass it an array of field configurations - however, once the store has been initialized, the fields property will have been changed to an instance of Ext.util.MixedCollection, which does not provide the same access methods as an array.

In short, fields[i] will not work, instead you have to use fields.get(i):

Example: http://jsfiddle.net/27KDr/