Go Back   SmartClient Forums > Smart GWT Technical Q&A
Wiki Register Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 5th Apr 2011, 04:43
banang banang is offline
Registered Developer
 
Join Date: Mar 2010
Posts: 37
Default Adding fields to existing datasource

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":"*" 
         },
The data source backing the form will then get the response and build an appropriate form based on this, like so:

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
   }
}
This is all working fine, but my problem is that with fields where the user could want to supply more than one, or some unknown amount, I don't know how to add these fields to the data source. I've tried adding them after the data source has been created, but this is not allowed. I can't keep them hidden, as I don't have an upper bound, and since the format is not fixed I can't use an editable listgrid.

How can I achieve this? Thankful for any suggestions.
Reply With Quote
  #2  
Old 5th Apr 2011, 08:41
Isomorphic Isomorphic is offline
Administrator
 
Join Date: May 2006
Posts: 37,806
Default

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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search


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

© 2010,2011 Isomorphic Software. All Rights Reserved