|
#1
|
|||
|
|||
|
My use case is quite complex, but I'll try my best to simplify it to make my problem as clear as possible. Bear with me, please.
On my server I have data models. These models may model any kind of objects, like chairs, people, animals or computer parts. I do not know in advance what these models will be, and my app needs to work for all kinds of models. You can think of the models as class definitions. My job is to build forms to populate these models with data, in order to create instances of these classes. Each field in the model is of some data type (like string, or date), and can be present in the instance one or multiple times. Sometimes I know how many times the field may be present, sometimes there is no upper limit. For example, there is no upper bound to how many pets a person may own. Right now I have one data source that is used to fetch a mold for the data sources used in the form. The data from the server (for i.e. a person) could look something like this (where quantity denotes the maximum number of entries per field, and * means there s no upper bound): Code:
"name":
{"id":"name",
"type":"text",
"name":"Name",
"quantity":"3"
},
"age":
{"id":"age",
"type":"integer",
"name":"Age",
"quantity":"1"
},
"pet":
{"id":"pet",
"type":"text",
"name":"Owns pet",
"quantity":"*"
},
Code:
for (Record record : response.getData())
{
for (String attribute : record.getAttributes())
{
Record field = record.getAttributeAsRecord(attribute);
addField(createField(field.getAttribute("id"),
field.getAttribute("type"),
field.getAttribute("name"));
}
}
private DataSourceField createField(String id, String type, String name)
{
switch(FieldType.valueOf(type.toUpperCase()))
{
case TEXT:
DataSourceField field = new DataSourceField(id, FieldType.TEXT, name);
FormItem item = new RichTextItem();
field.setEditorType(item);
return field;
case DATETIME:
//and so on
}
}
How can I achieve this? Thankful for any suggestions. |
|
#2
|
|||
|
|||
|
First, have you considered the DataSource.addDynamicDSGenerator() API? You could be getting all available server-side features for your dynamically generated DataSources/forms instead of just the client-side features.
As far as this multiple-fields thing, we'd guess this comes from XML Schema? How you do this depends a lot on how you ultimately want the data represented in the Record. Take a look, for example, at DataSourceField.multiple and it's consequences for XML serialization of a field value that's an Array. If one of those options works for you, then you could create a custom FormItem, probably based on CanvasItem, that provides an interface for entering one or more values (think of eg a plus sign button and remove button, similar to the FilterBuilder) and stores those values as an Array. |
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding fields to datasource at runtime | jay.l.fisher | Smart GWT Technical Q&A | 56 | 30th Mar 2013 09:38 |
| Download a File error | RHill | Technical Q&A | 12 | 5th Mar 2012 10:07 |
| Not understanding i18n of DataSource ds.xml title fields | dczech | Smart GWT Technical Q&A | 18 | 22nd Mar 2011 06:52 |
| Preserve existing dynamicform fields | lujop | Smart GWT Technical Q&A | 0 | 15th Nov 2010 07:55 |
| The Smart GWT FAQ | Isomorphic | Smart GWT Technical Q&A | 0 | 27th Oct 2009 17:41 |