PDA

View Full Version : populating form with hasMany relationship in Model



shizny
18 Apr 2012, 11:07 AM
Hello, I've got a form that I'm trying to populate with a model (actually two models with a hasMany/belongsToMany relationship). For example, my two models are 'user' and 'book'. A user can have more than one book. What I want to do is create a form where you can view basic user info ('username', 'address', 'books') and edit that info. For the books portion of the form I've got a combo box that will hold all the individual books that a user has. I've seen a method where you populate a form with a model but have not seen and can't find any documentation on how you can (if you can ) fill form items that represent the belongsToMany relationship. I'd like all a users books to end up in that combo box for selecting. Is this possible or do I need to create a store specifically for the combo box and use the form filling method for the user data only?

börn
19 Apr 2012, 5:33 AM
First a model is a record.. so for the normal data in your form you can use loadRecord on your form... You can do this e.g. in some selection model callback handler... After doing that you can use a store which is created automatically from your model hasMany attribute: user.books() as the combobox's store...

stockholm
18 Jul 2012, 11:51 PM
you can do it when rendering the field, let's say one user has many posts, the following code show how to list them in one list:


initComponent:function(){
this.columns = [
{header: 'Name', dataIndex: 'name', flex:1},
{header: 'Email',dataIndex: 'email',flex:1},

//here, use the renderer to fetch and present the relative posts.
{
header: 'Post',
dataIndex: 'name',
renderer:function(value){
var user = this.getStore().findRecord('name',value);
var post = user.posts[0];
return post.title;
},
flex:1}
];

// initCompoment is a template method, so you must call the parent method
// when you override it.
this.callParent(arguments);
}

mrroohian
26 Apr 2013, 1:39 AM
I had the same problem and tried this method but sorting functionality breaks if you use a custom renderer function. Is there anyway to fix that also?