Create a form with dynamic attributes

I’m trying to create a dynamic form using a datasource. The datasource gets all the form field using an RPC call (shown below).

  public PublishedReportParameterDS(String id,String reportId) {
        setID(id);
        if (service == null) {
            service = WebAnalyticsRemoteService.Util.getInstance();
        }

        service.getHeaders(reportId,new AsyncCallback<List<HeaderGWT>>() {
            public void onFailure(Throwable caught) {
                SC.say("There was an error fetching data from the server");
            }
            public void onSuccess(List<HeaderGWT> result) {
                final List<DataSourceField> items = new ArrayList<DataSourceField>();

                for(HeaderGWT record:result){
                   
                    DataSourceTextField filterValueField = new DataSourceTextField(record.getName()+"filterValue","filterValue",100,false); 
                    items.add(filterValueField);

                }
                DataSourceField[] dsFields = new DataSourceField[items.size()];
                setFields( items.toArray(dsFields));

            }
        });

    }

It only works when I move the RPC call and field creating logic to my canvas code.

 service.getHeaders(reportId,new AsyncCallback<List<HeaderGWT>>() {
                        public void onFailure(Throwable caught) {
                            SC.say("There was an error fetching data from the server");
                        }
                        public void onSuccess(List<HeaderGWT> result) {

                            for(HeaderGWT record:result){
                             
                                TextItem filterValue = new TextItem(); 
                                filterValue.setName(record.getName()+"filterValue");
                                filterValue.setShowTitle(false); 
                                items.add(filterValue);

                            }

                            FormItem[] fitems = new FormItem[items.size()];

Is my approach to use datasource for the dynamic field generation not right?
Also, Any guidance on how to pass the filled form parameters to my RPC Datasource would be appreciated. In the past, I have set the form values to an attribute in the datasource class which was referenced in the RPC Datasource that set the parameters to be passed on to servlet.
Thanks,
Jyot

Rather than put code for fetching fields in the DataSource itself, just have it separate (a “DataSourceFactory” if you like) and have the DataSource only created once the data for the fields have arrived.

Not sure what you’re asking about the filled in form - if you insist on using GWT-RPC and doing the extra work, look at the GwtRPCDataSource in the sticky thread.

Thank you for your response. I’ll try to implement what you recommended.
For the second question, I apologize for not explaining it well enough. All I want to know is how to pass the form values to the RestDatasource setDefaultParams() method. See the code/comment below.

public class ReportDS extends RestDataSource {
    public static ReportDS instance = null;
    public static ReportDS getInstance() {
         if (instance == null) {
        instance = new ReportDS("reportDS_DS");
      }
        return instance;
    }
    public ReportDS(String id) {
          setDataFormat(DSDataFormat.JSON);
     
//???? How to get the dynamically generated form values as a parameter to the JSON service??
   setDefaultParams(getParameters());
    setFetchDataURL(GWT.getModuleBaseURL() +"/JSONServlet");
.. 
..

Also, I mentioned RPCDatasource in my earlier post, what I meant was RestDataSource, Sorry for the confusion.
Thanks again for your help.
J

Simple calling saveData() on a DynamicForm that has that RestDataSource as it’s DataSource is going to cause the form values to become parameters (with the default setting for dataProtocol). You may need to spend some more time with the docs and examples, particularly the RestDataSource overview.

I’ll see, if I can find some examples to do the same. Most of the ones that I’ve seen, are based on Listgrid tied to a RestDataSource and not a form, which works without a problem.
Anyway, I’ll try what you’ve suggested. But just to summarize what I’ve understood:
I will have a Dynamic form tied to a RestDataSource that gets created by a DataSourceFactory class (to ensure form fields are created and available). The results of Restdatasource populate a list grid, so I’m assuming the same RestDatasource can be tied to a listgrid to display the results. Please let know if I’ve missed anything.

Thanks again for you help.
Jyot