PDA

View Full Version : Reading total from DataStore



nicosk
7 Nov 2009, 5:18 AM
Hi all,
i have this code (extending the combobox - i will be using this kind of combobox a lot so i thought to extend it)

Application.RatingCombo = Ext.extend(Ext.form.ComboBox, {
nodeId : null,
initComponent:function() {
var config = {
store: new Ext.data.Store({
id : 'RatingDataStore',
proxy : new Ext.data.HttpProxy({
url : './php/misc.php',
method : 'POST'
}),
baseParams: {
task : "RATING_LISTING"
},
reader: new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total'
}, [{
name: 'RatingId',
type: 'int',
mapping: 'id'
}, {
name: 'RatingType',
type: 'string',
mapping: 'rRating'
}, {
name: 'RatingDisplay',
type: 'string',
mapping: 'rDisplay'
}]),
sortInfo: {
field: 'RatingType',
direction: "ASC"
}
}),
displayField:'RatingDisplay',
valueField : 'RatingId',
editable : false,
mode: 'local',
forceSelection: true,
value : this Is Where I Need Help
triggerAction: 'all',
selectOnFocus:true,
}; // eo config object

// apply config
Ext.apply(this, Ext.apply(this.initialConfig, config));

Application.RatingCombo.superclass.initComponent.apply(this, arguments);
}

,onRender:function() {
this.store.load();
Application.RatingCombo.superclass.onRender.apply(this, arguments);
}
});

Ext.reg('ratingcombo', Application.RatingCombo);

My Json


({"total":"8","results":[{"id":"1","rRating":"1","rDisplay":"*","rIcon":null},{"id":"2","rRating":"2","rDisplay":"**","rIcon":null},{"id":"3","rRating":"3","rDisplay":"***","rIcon":null},{"id":"4","rRating":"4","rDisplay":"****","rIcon":null},{"id":"5","rRating":"4+","rDisplay":"****+","rIcon":null},{"id":"6","rRating":"5","rDisplay":"*****","rIcon":null},{"id":"7","rRating":"A","rDisplay":"Apt A","rIcon":null},{"id":"8","rRating":"B","rDisplay":"Apt B","rIcon":null}]})


I want to set the value of the combobox to Total from the store's reader. how can i do that?

Condor
7 Nov 2009, 6:33 AM
Application.RatingCombo = Ext.extend(Ext.form.ComboBox, {
nodeId: null,
constructor: function (config) { // <- In Ext 3 you should override the constructor
Application.RatingCombo.superclass.constructor.call(this, Ext.apply({
store: new Ext.data.Store({
//id: 'RatingDataStore', <- You want to create multiple instances, so you can't assign a unique id
proxy: new Ext.data.HttpProxy({
url: './php/misc.php',
method: 'POST'
}),
baseParams: {
task: "RATING_LISTING"
},
reader: new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total',
fields: [{
name: 'RatingId',
type: 'int',
mapping: 'id'
},{
name: 'RatingType',
type: 'string',
mapping: 'rRating'
},{
name: 'RatingDisplay',
type: 'string',
mapping: 'rDisplay'
}]
}),
sortInfo: {
field: 'RatingType',
direction: "ASC"
}
}),
displayField: 'RatingDisplay',
valueField: 'RatingId',
editable: false,
mode: 'local',
forceSelection: true,
triggerAction: 'all',
selectOnFocus: true,
}, config));
// You can only set the value after the store is loaded
this.store.on('load', function(store){
this.setValue(store.getTotalCount());
}, this);
},
onRender: function () {
this.store.load();
Application.RatingCombo.superclass.onRender.apply(this, arguments);
}
});
Ext.reg('ratingcombo', Application.RatingCombo);

nicosk
7 Nov 2009, 12:03 PM
thanks a lot Condor for your help! :)



({"currentId":"4","total":"8","results":[{"id":"1","rRating":"1","rDisplay":"*","rIcon":null},{"id":"2","rRating":"2","rDisplay":"**","rIcon":null},{"id":"3","rRating":"3","rDisplay":"***","rIcon":null},{"id":"4","rRating":"4","rDisplay":"****","rIcon":null},{"id":"5","rRating":"4+","rDisplay":"****+","rIcon":null},{"id":"6","rRating":"5","rDisplay":"*****","rIcon":null},{"id":"7","rRating":"A","rDisplay":"Apt A","rIcon":null},{"id":"8","rRating":"B","rDisplay":"Apt B","rIcon":null}]})


If i wanted to get "currentId" from that datastore, how do i do it?

nicosk
8 Nov 2009, 9:14 AM
what i am trying to ask is how to get the currentId from store and set it as this.setValue(currentId)

Condor
8 Nov 2009, 12:20 PM
Use:

this.store.on('load', function(store){
this.setValue(store.reader.jsonData.currentId);
}, this);