Multiple related Datasource and a single grid

Hello,

First of all, I would like to thanks the SmartClient guys for this wonderful piece of code. Now my problem:

I have 2 tables:

Person
Id Name
1 John
2 Woo
3 Dilbert
4 Nobody

Address
Id PersonId Street
1 1 Street1
2 1 Street2
3 2 Street3
4 3 Street4

Exist a one-to-many relationship between Person-Address. I want to create a ListGrid with the content:

Person Address
John [Street1, Street2]
Woo [Street3]
Dilbert [Street4]
Nobody

Where the address column has the value retrieved and formated as show. Reading the documentation, I saw 2 solutions to this problem:

  1. Create 1 DataSource, for example using the JSON, with the content:

[
{
id:1,
name:John,
addresses: [{id:1, street:Street1}, {id:2, street:Street2}]
}
…
]

And use a XPath + CellFormatter to accomplish the task.

  1. Create 2 DataSources, one for each table, and use DataSourceField.setForeignKey() to inform the relationship.

But… Maybe why I’m new for the SmartClient framework, I couldn’t figure out how to make work, and I will appreciate code examples if anyone already did that.

Thanks in advance!

#1 is the right approach. With #2, foreignKey plus optionDataSource is not going to result in the format you want unless you implement a formatter, so your back to #1.

For help, show your best attempt at writing a formatter, and also, the result of using methods like isc.echo() and isc.echoAll() to dump the data involved to the Developer Console.

But if I want to reuse the other DataSource???

Ok, I will post my attempt soon. But… what I like int the #2 approach is the possibility to reuse the second DataSource in another context, in despite of in this example the Person is an Entity and the Address is a Value (following the Domain Driven Design…) and sounds good to send the two together in a single request…

Where can I find examples using the #2 approach?

Yes, It’s working!

Sorry my unnecessary question, it’s easy to do what I want, if anyone want to do the same, my contribution:

If you are using JSON or XML, the code is the same. If your JSON request has a nested list like:

[
{
name: John
addresses: [{id:1, address:street1}, {id:2, address:street2}]
},
{
name: Woo
addresses: [{id:3, address:street3}, {id:4, address:street4}]
}
]

Your DataSource needs to know about the list and its XPath:

DataSource streets = new DataSource("streets", "Streets");
streets.setValueXPath("addresses/address"); // <-- Very important!!!!

And to format a grid field, use the CellFormatter:

ListGridField streetsField = new ListGridField("streets");
streetsField.setCellFormatter(new CellFormatter() {

            public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                String fields = "[ ";
                for (String s : record.getAttributeAsStringArray("streets")) {
                    fields += s + " ";
                }
                fields += "]";
                SC.say("Object value: " + value + ", record value: " + fields);
                return fields;
            }
        });

And Voilá!
Thanks for SmartClient guys for the patience.

Thanks fbdo for posting your solution, that will surely benefit others.