PDA

View Full Version : [SOLVED] How select data in Ext.data.Store and to feed it in a formPanel ?



lpastor
23 Aug 2009, 7:31 AM
(Hello,

I would like to select a particular registration in my data store and to polate it in a form.

I have tried that, but it find nothing (result=-1) :


var idx = store.find('ldap_contact', 'toto');

Ext.MessageBox.alert('result',idx);

if(-1 !== idx) {
userForm.getSelectionModel(idx).SetValues();
} Before I do a store.load()

I am not sure too that : userForm.getSelectionModel(idx).SetValues(); is the good way to populate data in the form ?

Condor
23 Aug 2009, 7:45 AM
Use:

userForm.getForm().loadRecord(store.getAt(idx));

lpastor
23 Aug 2009, 8:03 AM
Thank you Condor,

An other problem is that my find don't work.

I have put my find at the end of my code, but perhaps something in not instanciate when it runs ?

Laurent

Condor
23 Aug 2009, 10:38 AM
1. Is the record found? Does store.getAt(idx) return the correct record?
2. userForm.getForm().loadRecord() assumes the field in the record are the same as the names of the form fields.

lpastor
23 Aug 2009, 11:10 AM
Thanks a lot Condor to help me, you are the best !

I think idx is an index, also I have tried with a random number :


idx=3;

if(-1 !== idx) {
userForm.getForm().loadRecord(store.getAt(idx));
}


But that display nothing...

Condor
23 Aug 2009, 10:09 PM
Again:
1. Is the record found? Does store.getAt(idx) return the correct record?
2. Is the form rendered and layed out? What does userForm.getForm().getValues() return?

lpastor
24 Aug 2009, 7:27 AM
Hello,

1. Is the record found? Does store.getAt(idx) return the correct record? Nothing is return

2. Is the form rendered and layed out? What does userForm.getForm().getValues() return? Yes, but fields are empty.

When I tried this, that's work :



var myData = [
['3m Co','titi'],
['Alcoa Inc','toto'],
['Altria Group Inc','tutu'],
['American Express Company','tata']
];

var store = new Ext.data.Store({
reader: new Ext.data.ArrayReader({}, [
{name: 'nom_contact'},
{name: 'organisation_contact'}
])
});

store.loadData(myData);

Ext.onReady(function() {
Ext.QuickTips.init();

var bd = Ext.getBody();

var gridForm = new Ext.FormPanel({
id: 'company-form',
frame: true,
labelAlign: 'left',
title: 'Company data',
bodyStyle:'padding:5px',
width: 750,
layout: 'column',
items: [{
xtype: 'fieldset',
labelWidth: 90,
title:'Company details',
defaults: {width: 140, border:false},
defaultType: 'textfield',
autoHeight: true,
border: false,
listeners: {
render: function(g) {
gridForm.getForm().loadRecord(store.getAt(3));
},
delay: 10
},
items: [
{fieldLabel: 'Nom Prénom', name:'nom_contact', width:300, allowBlank: false},
{fieldLabel: 'Organisation', name:'organisation_contact', width:300, allowBlank: true}
]
}],
renderTo: bd
});

});When I tried with my json script, that don't work :


var proxy = new Ext.data.HttpProxy({
url: 'Mysql_Call.php?table=contact&idkey=id_contact',
method: 'POST'
});

var reader = new Ext.data.JsonReader({
totalProperty: 'total',
successProperty: 'success',
idProperty: 'id_contact',
root: 'data'
}, [
{name: 'id_contact'},
{name: 'nom_contact', sortDir: 'ASC', sortType: 'asUCString', allowBlank: false},
{name: 'organisation_contact'}
]);

var writer = new Ext.data.JsonWriter();

var store = new Ext.data.Store({
id: 'id_contact',
proxy: proxy,
reader: reader,
writer: writer,
sortInfo:{field: 'nom_contact', direction: "ASC"},
listeners: {
write : function(store, action, result, response, rs) {}
}
});

Ext.onReady(function() {
Ext.QuickTips.init();

store.load();

var bd = Ext.getBody();

var gridForm = new Ext.FormPanel({
id: 'company-form',
frame: true,
labelAlign: 'left',
title: 'Company data',
bodyStyle:'padding:5px',
width: 750,
layout: 'column',
items: [{
xtype: 'fieldset',
labelWidth: 90,
title:'Company details',
defaults: {width: 140, border:false},
defaultType: 'textfield',
autoHeight: true,
border: false,
listeners: {
render: function(g) {
gridForm.getForm().loadRecord(store.getAt(3));
},
delay: 10
},
items: [
{fieldLabel: 'Nom Prénom', name:'nom_contact', width:300, allowBlank: false},
{fieldLabel: 'Organisation', name:'organisation_contact', width:300, allowBlank: true}
]
}],
renderTo: bd
});


});
However, this json script is use well in other application with the same data !

Condor
24 Aug 2009, 7:30 AM
1. You never load the store.
2. Loading is asynchronous. You have to wait until the server returns the data.

Try:

var gridForm = new Ext.FormPanel({
...
listeners: {
afterlayout: function(g){
store.load({
callback: function(){
gridForm.getForm().loadRecord(store.getAt(3));
}
});
},
single: true
}
});

lpastor
24 Aug 2009, 7:43 AM
Thank you very much Condor, now it's good !!!

Laurent