PDA

View Full Version : Correct way to display a foreign key in grid column (as human readable text)



Fire-Dragon-DoL
12 Oct 2012, 5:33 AM
I found some solutions but none of them satisfact me enough. I would like to hear from you which possibilities do I have to display a foreign key text in human readable format. I'll bring you an example

Suppose I have Comment which has these fields: Id, Text, Author_Id
Now there is Author, which has: Id, Name

Author_Id is an int, like Id on both models. Name and Text are strings.

Now I'm displaying a grid containing all comments, but I would like to show to the user instead of an id in Author_Id, the name of the author.

What possibilities do I have to do this? The only way I currently found is loading all Author store (with all records) and eventully render it.

Are there any better solution?

Expecially, if I create a field that is used just to display this value (server side so), like Author_Id_Name, the problem is that if I edit Author_Id in my grid, Author_Id_Name won't get updated because Ext doesn't reload all record but just updates columns I edited.

What do you suggest?

scottmartin
12 Oct 2012, 8:36 AM
I prefer to create a view using joined fields. When you update the id, the string value will follow.
This way user can also search the string value.

To me this much better than rendering at the client.

Scott.

Fire-Dragon-DoL
12 Oct 2012, 8:51 AM
I prefer to create a view using joined fields. When you update the id, the string value will follow.
This way user can also search the string value.

To me this much better than rendering at the client.

Scott.

Thanks for suggestion. When you raise your row editor, I imagine you show a selectbox to choose an id (which is integer however). Do you handle the fact that you are updating a "string" value with the int on server side?

I consider yours a valid suggestion, if you can actually update the field.

Still I prefer to show the user a combobox filter instead of string (but that's not a problem I can do the same).

I'll use your technique in my next project I think.

scottmartin
12 Oct 2012, 9:00 AM
Since it is a view, you update the id only. The string (join field) is for user display/search only.

Scott.

Fire-Dragon-DoL
12 Oct 2012, 9:02 AM
Since it is a view, you update the id only. The string (join field) is for user display/search only.

Scott.

How do you implement it? I mean, do you have 2 fields on your model, one is author_id and the other is author_id_to_string, and you update only author_id and author_id_to_string gets autoupdated?

scottmartin
12 Oct 2012, 9:06 AM
Correct .. you add the new field to model.

Scott.

Fire-Dragon-DoL
12 Oct 2012, 9:09 AM
Correct .. you add the new field to model.

Scott.

Mhhh... that's how I did it before, however when I update the id field, the string field for some reason doesn't get updated (I have to refresh the store), I think because Ext updates the id field but doesn't read the whole object from the server again, so the string doesn't get updated. I need a way to force the "string field" update too.

scottmartin
12 Oct 2012, 9:17 AM
Were you using a SQL view at the server side as the table used to populate store?

model has:
id_product
product_name // join field

CREATE VIEW viewInvoices (
SELECT id_product, products.product_name AS product_name
FROM invoices
JOIN products ...
)

Updating the id will automatically update name.. same of you use SELECT * viewInvoices

Scott.

Fire-Dragon-DoL
12 Oct 2012, 9:39 AM
Were you using a SQL view at the server side as the table used to populate store?

model has:
id_product
product_name // join field

CREATE VIEW viewInvoices (
SELECT id_product, products.product_name AS product_name
FROM invoices
JOIN products ...
)

Updating the id will automatically update name.. same of you use SELECT * viewInvoices

Scott.

Ah thanks, I were missing the SQL View!
I'll look for a different solution at the moment however, because I'm using sqlite so I don't have views support :\