Announcement

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

    ListGrid - How to implement lookup on another table

    I know that this has probably been discussed elsewhere or is in the documentation, perhaps I am not using the right terms when I search, but I simply cannot find the answer to this.

    So please can someone point me to either the right documentation or give me and answer here?

    I have a listgrid that displays info from a datasource. However, one of the columns is an ID (integer value) that is actually a reference to a description table.

    Here's what I want to do:

    a) I don't want to display the ID, instead, I want to display the description off the other table.
    b) However, it's perfectly valid for the ID to be null. I.e. there is no link to the other table. In this case, it should simply be blank.
    c) The user can, if the wish, edit this field. In this case, I want to have a dropdown that they can choose values from. The dropdown should be the descriptions from the description table.

    So it's a simple lookup that supports null values. If the null value part is a problem, then I can inject a 0 code ("N/A") into the data as it comes from that back end and allow users to select that "N/A" and then parse it correctly with CRUD calls handled by the back end.

    But the main problem is that the IDs themselves are meaningless to users. The descriptions from the foreign key table are what matters.

    Now i can't do a static valuemap, it has to be driven off the database because the mapping is different for every user. The source table isn't a simple code-description table, it's a table that's user-maintained (a list of information groups), so the ID isn't sequential from 1 ... X, for each user, the list of values and ID will be different.

    How do I do this?
    Last edited by kieser; 25 Feb 2010, 08:56.

    #2
    Has no-one else come across this problem yet?

    Comment


      #3
      ListGridField.optionDataSource

      Comment


        #4
        Originally posted by Isomorphic
        ListGridField.optionDataSource
        Thank you!

        I will give that a go!

        Comment


          #5
          Unfortunately that doesn't work, because I cannot get any fields from the ListGrid. I think that before it's displayed, it isn't instantiated, and that means that it has no columns, which in turn means that you cannot set lookups.

          This is my code that sets up the listgrid:
          Code:
          		final ListGrid recipientGrid = new ListGrid();
          
          		// Add the title
          		SectionStack sectionStack = new SectionStack();  
          		sectionStack.setWidth(464);  
          		sectionStack.setHeight(230);  
          		   
          		String title = "Message Recipients (use right side of this page to add more)";  
          		SectionStackSection section = new SectionStackSection(title);
          		
          		section.setCanCollapse(false);
          		section.setExpanded(true); 
          		
          		// Now create the ListGrid to display the data
          		recipientGrid.setWidth(464);  
              	recipientGrid.setHeight(224);  
              	recipientGrid.setDataSource(AppRecipientListSgwtDS.getInstance());  
              	recipientGrid.setCanEdit(true);
              	recipientGrid.setCanRemoveRecords(true);
              	recipientGrid.setCanFreezeFields(true);  
              	recipientGrid.setCanRemoveRecords(true);
              	recipientGrid.setRemoveOperation("delete");
              	
              	ListGridField groupID=recipientGrid.getField(1);
              	GroupHeaderDS lookup= GroupHeaderDS.getInstance();
              	groupID.setOptionDataSource(lookup);
              	groupID.setValueField("group_id");
              	groupID.setDisplayField("group_name");
              	groupID.setAutoFetchDisplayMap(true);
            	
              	    	
              	recipientGrid.setAutoFetchData(true);
              	section.setItems(recipientGrid);
              	sectionStack.setSections(section);
              	
              	leftPanel.addMember(sectionStack);
          Now, the groupID variable is always null. Even if I use a number to get the appropriate column by absolute reference rather than the column name.

          This means that groupID.setOptionDataSource and all other groupID.* calls generate an exception.

          If I comment out the part that attempts to set the lookup, then all is well. The ListGrid displays perfectly, with the integer value in the group_id column as expected.

          I think that this is a classic case of chicken and egg. There are not any columns created, before the ListGrid displays. And so you cannot set the setOptionDataSource. But of course the setOptionDataSource needs to be called before it's displayed!

          Any ideas on this anyone?

          Comment


            #6
            See useAllDataSourceFields. You can provide fields to a grid that is bound to a DataSource. The definitions are merged.

            Comment


              #7
              Originally posted by Isomorphic
              See useAllDataSourceFields. You can provide fields to a grid that is bound to a DataSource. The definitions are merged.
              Hmm... still getting null when I try to get the field.
              I can see that there is no call to the backend database either, so at this stage of setting up the ListGrid, smartGWT has no idea what fields there are in the ListGrid.

              I have this:
              Code:
                  	recipientGrid.setUseAllDataSourceFields(true);
                  	
                  	ListGridField groupID=recipientGrid.getField("group_id");
              I have also tried the numeric version as posted above, but clearly the problem is that there is no group_id field to get yet because it hasn't set up the ListGrid.

              Comment


                #8
                Last try.. You do not call getField, you call setFields() and they are merged with the DataSource fields. See useAllDataSourceFields.

                Comment


                  #9
                  WooHoo! Slowly getting there! Thank you for the response and the help!

                  I had some problem working out how to relate the new field that I am creating and adding to the ListGrid to the underlying column that I want mapped, but finally worked out that I have to setName it to the column name and then the code identifies which column to map.

                  It's now working great when I click to edit. The mapping occurs just fine in the dropdown. But the values are not mapped when ListGrid first loads and after I have saved the change to the database, then the name reverts back to the ID, it doesn't remain displayed as a name.

                  How do I get it to map when it loads and after it saves?

                  Here is the code that I added to implement you suggestion above:

                  Code:
                  ListGridField groupID=new ListGridField();
                      	groupID.setOptionDataSource(GroupHeaderDS.getInstance());
                  groupID.setValueField("group_id");
                  groupID.setDisplayField("group_name");
                  groupID.setName("group_id");
                  groupID.setTitle("Group Name");
                  groupID.setAutoFetchDisplayMap(true);
                  recipientGrid.setFields(groupID);
                  And this is what my ListGrid code now looks like:
                  Code:
                  		final ListGrid recipientGrid = new ListGrid();
                  
                  		// Add the title
                  		SectionStack sectionStack = new SectionStack();  
                  		sectionStack.setWidth(464);  
                  		sectionStack.setHeight(230);  
                  		   
                  		String title = "Message Recipients (use right side of this page to add more)";  
                  		SectionStackSection section = new SectionStackSection(title);
                  		
                  		section.setCanCollapse(false);
                  		section.setExpanded(true); 
                  		
                  		// Now create the ListGrid to display the data
                  		recipientGrid.setWidth(464);  
                      	recipientGrid.setHeight(224);  
                      	recipientGrid.setDataSource(AppRecipientListSgwtDS.getInstance());  
                      	recipientGrid.setCanEdit(true);
                      	recipientGrid.setCanRemoveRecords(true);
                      	recipientGrid.setCanFreezeFields(true);  
                      	recipientGrid.setCanRemoveRecords(true);
                      	recipientGrid.setRemoveOperation("delete");
                      	recipientGrid.setUseAllDataSourceFields(true);
                      	
                      	ListGridField groupID=new ListGridField();
                      	groupID.setOptionDataSource(GroupHeaderDS.getInstance());
                      	groupID.setValueField("group_id");
                      	groupID.setDisplayField("group_name");
                      	groupID.setName("group_id");
                      	groupID.setTitle("Group Name");
                      	groupID.setAutoFetchDisplayMap(true);
                      	recipientGrid.setFields(groupID);
                    	
                      	    	
                      	recipientGrid.setAutoFetchData(true);
                      	section.setItems(recipientGrid);
                      	sectionStack.setSections(section);
                      	
                      	leftPanel.addMember(sectionStack);
                  and this is the fields definition section in the datasource for the ListGrid data:
                  Code:
                  setID(id);  
                  setDataFormat(DSDataFormat.JSON);  
                  DataSourceField accountIdField = new DataSourceField("id", FieldType.INTEGER,  
                              "id");  
                  accountIdField.setPrimaryKey(true);
                  accountIdField.setHidden(true);
                   	      
                  DataSourceField accountCodeField = new DataSourceField("number", FieldType.TEXT,  
                    	                                                             "Number");  
                  DataSourceField accountGroupID = new DataSourceField("group_id", FieldType.INTEGER,  
                    	                                                             "Group ID");
                    	      
                  setFields(accountIdField,accountCodeField, accountGroupID);
                  and this is the option lookup datasource field definitions:
                  Code:
                  setID(id);  
                  setDataFormat(DSDataFormat.JSON);  
                  DataSourceField groupIdField = new DataSourceField("group_id", FieldType.INTEGER,  
                              "group_id");  
                  groupIdField.setPrimaryKey(true);
                  groupIdField.setHidden(true);
                   	      
                  DataSourceField ownerIDField = new DataSourceField("group_owner", FieldType.INTEGER,  
                    	                                                             "Owner ID");  
                  DataSourceField groupName = new DataSourceField("group_name", FieldType.TEXT,  
                    	                                                             "Group Name");
                    	      
                  setFields(groupIdField,ownerIDField, groupName);
                  I have also tried without the group_id being set in the ListGrid datasource. This still doesn't work after initial load or after it has saved.
                  Last edited by kieser; 28 Feb 2010, 11:59.

                  Comment


                    #10
                    I have also tried with:
                    Code:
                    recipientGrid.setAutoFetchDisplayMap(true);
                    but still no luck.

                    Comment


                      #11
                      Having read through the documentation yet again and spending 2 days on google as well as the very helpful comments by support posted here, I am starting to think that this lack of mapping once the value is saved and on load is a bug in the current version of smartGWT.

                      From everything that I have read, it should be mapped all the time, but it isn't. Yet it's mapping perfectly during editing or when I hit escape to quit out of editing. So it looks like something is broken when data loads.

                      Comment


                        #12
                        No, it's not broken, it's used absolutely every day in our applications.

                        Next obvious step is to look at whether fetches against the optionDataSource are being performed and whether the returned data is valid. Use the RPC tab of the Developer Console.

                        Overall, please try to make more use of the docs and tools.

                        Comment


                          #13
                          Thank you very much for the reply. I will dig further, but I can see clearly that the back end is receiving the requests and yes, the developer console shows both the request and the reply.

                          I am sure that if you say it's in daily use, the problem will be simple and will be local to my environment one way or the other.

                          Just a note on your comment about using the docs. As I originally posted when I started this thread, I found it extremely difficult to find the right information in the documentation for this facility. Even with your feedback on this thread, it took me a long time to find the right places. Which is why I started this thread.

                          However, thanks to your responses here, this thread should now form a part of the searchable documentation and so others in future should be able find this and find the answers. That's why we are all here contributing and asking in public! It's all adding to the wealth and knowledge.

                          Hopefully this thread is contributing to that!

                          Comment


                            #14
                            Well, there are another 5+ threads already pointing to this property based on the same question. And the documentation links to this property in many, many places. If you can suggest something we can do better based on however you chose to search for it, please do.

                            Comment


                              #15
                              good

                              Code:
                              ListGridField groupID=new ListGridField();
                                  	groupID.setOptionDataSource(GroupHeaderDS.getInstance());
                              groupID.setValueField("group_id");
                              groupID.setDisplayField("group_name");
                              groupID.setName("group_id");
                              groupID.setTitle("Group Name");
                              groupID.setAutoFetchDisplayMap(true);
                              recipientGrid.setFields(groupID);

                              Comment

                              Working...
                              X