Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to get the fields list of a datasource.

    How can I get the fields list of a datasource? I could only find the functions to set them and I want to implement a general filter component similar with FilterBuilder that should get the fileds name from the datasource.

    #2
    I've added DataSource.getFields() / getFieldNames() to SVN.

    Sanjiv

    Comment


      #3
      That's great. Thanks.

      Comment


        #4
        Emm ... do these methods work with the generated datasources from the smartGwt server ? I'm using the smartgwt 1.2 jar with your server (and not the 1.3 that comes with your ee distribution - which is older ? confused yet ? I was :-) ) and I have a simple datasource

        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <DataSource
            ID="test1"
            serverType="sql"
            tableName="test1"
        >
            <fields>
                <field name="ID" title="Id" type="integer"  primaryKey="true"  required="true"/>
                <field name="NAME" title="Name"  type="text" />
            </fields>
        </DataSource>
        if I use
        Code:
               DataSource ds = DataSource.get(dsName);
               DataSourceField[] fields = ds.getFields();
        I get only one field returned ...

        If use
        Code:
         
                DataSource ds = DataSource.get(dsName);
            	String[] fieldNames = ds.getFieldNames();
        I get the following exception

        Code:
        Uncaught JavaScript exception [com.google.gwt.core.client.JavaScriptException: (TypeError): 'data' is undefined
         number: -2146823279
         description: 'data' is undefined
        	at com.smartgwt.client.data.DataSource.getFieldNames(Native Method)
        	at com.smartgwt.client.data.DataSource.getFieldNames(DataSource.java:2302)
        	at com.smartgwt.sample.client.BuiltInDS.getDataSource(BuiltInDS.java:181)
        	at com.smartgwt.sample.client.BuiltInDS.bindComponents(BuiltInDS.java:206)
        	at com.smartgwt.sample.client.BuiltInDS.access$0(BuiltInDS.java:205)
        	at com.smartgwt.sample.client.BuiltInDS$2.onRecordClick(BuiltInDS.java:73)
        	at com.smartgwt.client.widgets.grid.events.RecordClickEvent.dispatch(RecordClickEvent.java:97)
        	at com.smartgwt.client.widgets.grid.events.RecordClickEvent.dispatch(RecordClickEvent.java:1)
        	at com.google.gwt.event.shared.HandlerManager$HandlerRegistry.fireEvent(HandlerManager.java:65)
        	at com.google.gwt.event.shared.HandlerManager$HandlerRegistry.access$1(HandlerManager.java:53)
        	at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:178)
        	at com.smartgwt.client.widgets.BaseWidget.fireEvent(BaseWidget.java:66)] in http://localhost:8080/builtinds/hosted.html?builtinds, line 18

        Comment


          #5
          As an aside, and to speed things up since I'm in a different timezone I wonder if you could comment on my approach (and why I need the above method) as I maybe trying to reinvent something you have already (but I haven't been able to find it) .

          I want to create a new form item for fields that have a foreign key but I want to do it with your dynamic/generated datasources.

          I want to be able to display the foreign key in the form but allow the user to select a new value from a listgrid launched with the datasource of the other table. I see there are some datasource methods associated with SelectItem but to do what I want I think I'll need to at least subclass SelectItem or maybe even combine it with FormItemIcon. This involves taking the generated datasource, finding the field that is a foreign key and replacing the editor with one I have create ... Does this seem like the correct apporach ?

          Comment


            #6
            Sounds like SelectItem will do exactly what you want. If you use the setPickListFields() method you can define the dropdown to have multiple columns. In fact once you set it to have multiple columns the dropdown looks exactly like a ListGrid and can even be sorted by any column.

            Comment


              #7
              Originally posted by mhulsman
              Sounds like SelectItem will do exactly what you want. If you use the setPickListFields() method you can define the dropdown to have multiple columns. In fact once you set it to have multiple columns the dropdown looks exactly like a ListGrid and can even be sorted by any column.
              I thought about that but I haven't been able to try it as I can't iterate through the fields in the generated datasource to find the SelectItem to update.

              Also I'm not sure if a drop down is the way I want to go as if the user is scrolling through a large list and clicks away for a second by mistake then they'll have to do it all again, a modal dialog with listgrid would be my preferred option. Thanks for the comments though ... all debate welcomed ...

              Comment


                #8
                If you prefer not to use the dropdown approach, create a StaticTextItem with a FormItemIcon that launches a pop-up dialog to pick the record, and install that custom editor via DataSourceField.setEditorType()

                Comment


                  #9
                  Originally posted by Isomorphic
                  If you prefer not to use the dropdown approach, create a StaticTextItem with a FormItemIcon that launches a pop-up dialog to pick the record, and install that custom editor via DataSourceField.setEditorType()
                  Cool ... I was thinking the FormItemIcon was the way to go but I wasn't sure how to link it back to a FormItem ...

                  So back to my original question is, is there a bug in getfields method (see earlier in the thread) or am I using it incorrectly ?

                  Comment


                    #10
                    While you're working out wether the above is a bug or me doing something stupid maybe you can help me with browse button I'm trying to write.

                    I've followed your example but my FormItemIcon is only appearing when I double click on an entry in a Listgrid, for the Dynamic form it doesn't appear, I'm using you builtinds example as the basis for trying this out so the types are
                    Code:
                        private ListGrid boundList;
                        private DynamicForm boundForm;
                        private DetailViewer boundViewer;
                    Further more when I do a setValue on the FormItem it doesn't update the entry in the ListGrid ... If you can spot what I'm doing I'd be really appreciative ...

                    Heres where I modify the datasource ....

                    Code:
                       DataSourceField field = ds.getField("test1_id");
                       if(field != null)
                       {
                    	System.out.println("Found " + field.getTitle() );
                    	String fk = field.getForeignKey();
                    	if(fk != null)
                    	{
                    		System.out.println("Found " + fk);
                    		String[] split = fk.split("\\.");
                    		if((split != null) && (split.length > 0))
                    		{
                    			System.out.println("Related " + split[0] );
                    			BrowseItem browse = new BrowseItem(split[0],split[1]);
                    			field.setEditorType(browse);
                    		}
                          		else
                           			System.out.println("Split size "+split.length);
                    	}
                    	else
                    		System.out.println("No foreign key");
                        }
                    and heres the new FormItem I've created, the setSelected method below is called from ListWindowBrowse to set the value and close the window, in the case of the ListGrid it does nothing, for the DetailViewer I don't know as the icon isn't appearing (Argh!!!) ...

                    Code:
                    public class BrowseItem extends TextItem {
                    	private Window listing;
                    	
                    	public BrowseItem(final String dataSource, final String attributeName)
                    	{
                    		FormItemIcon formItemIcon = new FormItemIcon(); 
                    		setIcons(formItemIcon);
                    		addIconClickHandler(new IconClickHandler(){
                    
                    			@Override
                    			public void onIconClick(IconClickEvent event) {
                    				listing = new ListingWindowBrowse(dataSource, attributeName, BrowseItem.this);
                    				listing.setIsModal(true);
                    				listing.draw();
                    			}
                    		});
                    		System.out.println("Created browse");
                    	}
                    	
                    	public void setSelected(String value)
                    	{
                    		setValue(value);
                    		if(listing != null)
                    			listing.markForDestroy();
                    		redraw();
                    	}
                    }

                    Comment


                      #11
                      I have exactly the same problem with the DataSoutrce.getFields() function.
                      It returns only one field.

                      Is this a bug ?

                      Chris

                      Comment


                        #12
                        Please file an issue in tracker.

                        Sanjiv

                        Comment


                          #13
                          I'm still trying to change the editor associated with a field in a dynamic form.

                          If I modify the datasource field using this
                          Code:
                             DataSourceField field = datasource.getField("test1_id");
                             BrowseItem browse = new BrowseItem("test1","ID");
                             field.setEditorType(browse);
                             DynamicForm boundForm.setDataSource(datasource);
                          It doesn't work ...

                          If I modify the DynamicForm directly i.e. like this
                          Code:
                             DynamicForm boundForm.setDataSource(datasource);
                              BrowseItem browse = new BrowseItem("test1","ID");
                              browse.setTitle("Browse");
                              browse.setDisplayField("test1_id");
                              boundForm.setFields(browse);
                          My BrowseItem works ... is this a bug as well ? (The DataSource in question has been generated from the smartclient server rather then a client side datasource)

                          Comment


                            #14
                            Bump

                            Bump ! anyone on the isomorphic side ?

                            Originally posted by mike_mac
                            I'm still trying to change the editor associated with a field in a dynamic form.

                            If I modify the datasource field using this
                            Code:
                               DataSourceField field = datasource.getField("test1_id");
                               BrowseItem browse = new BrowseItem("test1","ID");
                               field.setEditorType(browse);
                               DynamicForm boundForm.setDataSource(datasource);
                            It doesn't work ...

                            If I modify the DynamicForm directly i.e. like this
                            Code:
                               DynamicForm boundForm.setDataSource(datasource);
                                BrowseItem browse = new BrowseItem("test1","ID");
                                browse.setTitle("Browse");
                                browse.setDisplayField("test1_id");
                                boundForm.setFields(browse);
                            My BrowseItem works ... is this a bug as well ? (The DataSource in question has been generated from the smartclient server rather then a client side datasource)

                            Comment


                              #15
                              I wasn't able to find an issue report for this so I've created one.

                              Comment

                              Working...
                              X