Announcement

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

    Using SelectItem with Datasource how to make a combo of two fields as the Display?

    SmartGWT 2.1
    GWT 2.0.4

    I currently have this working however I can't seem to find any information on how to make my displayed value be a combination of two others, is this possible?

    For instance I have this but would like my setDisplayField to be a
    combination of the value and display field so the currently selected value can also be seen when shown on a DynamicForm.

    I've also noticed if my data returned in this object is more than a page the value I have on my form is not the value that is preselected in the SelectItem (is there a way to fix this ?).

    Code:
    public class XMLSelectItem extends SelectItem {
    
    	public XMLSelectItem(long id) {
    		XMLListGridDS ds = new XMLListGridDS();
    		ds.setID("xmlds_"+id);
    		setValueField("xmlid");
    		setDisplayField("dsc");
    		setEmptyPickListMessage("No data in table");
    		setPickListWidth(450);
    
    		ListGridField idField = new ListGridField("xmlid");
    		ListGridField dscField = new ListGridField("dsc");
    		setPickListFields(idField, dscField);
    
    		setOptionDataSource(ds);
    		setAutoFetchData(true);
    	}
    
    
    }
    Thank you,

    #2
    Use pickListFields with a CellFormatter to access more than one field from each record and display it.

    Could you clarify your second question?

    Comment


      #3
      Originally posted by Isomorphic
      Use pickListFields with a CellFormatter to access more than one field from each record and display it.

      Could you clarify your second question?
      I've included some images to make it easier to understand...

      I have a few forms that use the OptionDataSource using a SelectItem when the results returned are small the value in the form field is found and highlighted but when it's large the SelectItem doesn't match the form field value, not sure if it doesnt' find it or it doesn't pull all the data until it's scrolled. Just wondering if there is something I can do to make it go to the proper value in the SelectItem like it does when there is a small set of data to display.
      Attached Files

      Comment


        #4
        You'd have to turn off data paging, which you probably don't want to do unless the data is no more than a few hundreds records in the worst case. Otherwise, you're hitting the database and application server too much.

        Comment


          #5
          Originally posted by Isomorphic
          You'd have to turn off data paging, which you probably don't want to do unless the data is no more than a few hundreds records in the worst case. Otherwise, you're hitting the database and application server too much.

          That's kind of what I figured I'll try using the pickListField with a CellFormatter so they can at least see the value and description instead of just a description that could appear more than once.

          Thank you,

          Comment


            #6
            Originally posted by Isomorphic
            Use pickListFields with a CellFormatter to access more than one field from each record and display it.

            Could you clarify your second question?
            Ok, I think I know what I need to do with CellFormatter but not sure how to use it with my SelectItem, what method do I use to set it?

            Do I need to set it on anything or does it just override my display?

            How do I get the ListGridFields in my case to use the setPickListFields, if I pull off my DataSource they're DataSourceFields, can I simply cast them?

            Code:
            public class XMLSelectItem extends SelectItem {
            
            	public XMLSelectItem(long id) {
            		XMLListGridDS ds = new XMLListGridDS();
            		ds.setID("xmlds_"+id);
            		setValueField("xmlid");
            		setDisplayField("dsc");
            		setEmptyPickListMessage("No data in table");
            		setPickListWidth(450);
            
            		ListGridField idField = new ListGridField("xmlid");
            		ListGridField dscField = new ListGridField("dsc");
            		setPickListFields(idField, dscField);
            
            		setOptionDataSource(ds);
            		setAutoFetchData(true);
            		
            		CellFormatter cellFormatter = new CellFormatter() {
            			
            			@Override
            			public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
            				return record.getAttributeAsString("dsc") + " - " + record.getAttributeAsString("xmlid");
            			}
            		};
            		
            
            	}
            }

            Comment


              #7
              All you need to do now is set the CellFormatter on one of the ListGridFields you've already created.

              Comment


                #8
                Originally posted by Isomorphic
                All you need to do now is set the CellFormatter on one of the ListGridFields you've already created.
                Thank you so simple when you pointed to it....thank you again.

                Comment


                  #9
                  Originally posted by Isomorphic
                  All you need to do now is set the CellFormatter on one of the ListGridFields you've already created.
                  Ok that worked in the SelectItem but the initial value still only shows the dsc field, is there a way to change that?

                  If I comment out the setDisplayField("dsc"); then it shows the underlying value but with it, it shows the text value but not both.

                  Do I need to do something in my DynamicForm so it shows like the CellFormatter once the SelectItem is clicked?

                  Comment


                    #10
                    Originally posted by gilcollins
                    Ok that worked in the SelectItem but the initial value still only shows the dsc field, is there a way to change that?

                    If I comment out the setDisplayField("dsc"); then it shows the underlying value but with it, it shows the text value but not both.

                    Do I need to do something in my DynamicForm so it shows like the CellFormatter once the SelectItem is clicked?
                    Would you like this in it's own thread?

                    Comment


                      #11
                      Note the separate formatter/parser APIs on the item itself - use those. Call getValue() to get the stored value, getDisplayValue() to get the display value (these are the two you want to combine, right)?

                      Note that at some points, the displayValue is not available (eg, immediately after a setValue() call, before fetchMissingValues has done it's job). Your formatter will be automatically called again when the displayValue has been loaded.

                      Comment


                        #12
                        Originally posted by Isomorphic
                        Note the separate formatter/parser APIs on the item itself - use those. Call getValue() to get the stored value, getDisplayValue() to get the display value (these are the two you want to combine, right)?

                        Note that at some points, the displayValue is not available (eg, immediately after a setValue() call, before fetchMissingValues has done it's job). Your formatter will be automatically called again when the displayValue has been loaded.
                        Ok, I'm not sure which item you're referring to however I can try to make my question clearer if it helps.

                        I have a DynamicForm in a Page View that uses a DataSource to load all it's fields (some are SelectItem type) like so:
                        Code:
                        CatalogCategoryListGridDS cglds = new CatalogCategoryListGridDS();
                        final DynamicForm df = new DynamicForm();
                        if(c != null) {
                        	df.setInitialCriteria(c);
                        }
                        df.setUseAllDataSourceFields(true);
                        df.setDataSource(cglds);
                        df.setAutoFetchData(true);
                        My CatalogCategoryListGridDS contains some fields that have FK lookups using the SelectItem like so :

                        Code:
                        intField = new DataSourceIntegerField("xmlid");
                        time = System.currentTimeMillis();
                        intField.setEditorType(new XMLSelectItem(time));
                        name = "xmlds_"+time;
                        intField.setForeignKey(name+".xmlid");
                        intField.setTitle("XML ID");
                        intField.setCanEdit(true);
                        addField(intField);
                        My XMLSelectItem looks like (note I commented out the cell formatter in here because I don't really want it in the drop down just in the original cell of the DynamicForm):
                        Code:
                        public class XMLSelectItem extends SelectItem {
                        
                        	public XMLSelectItem(long id) {
                        		XMLListGridDS ds = new XMLListGridDS();
                        		ds.setID("xmlds_"+id);
                        		setValueField("xmlid");
                        		setDisplayField("dsc");
                        		setEmptyPickListMessage("No data in table");
                        		setPickListWidth(450);
                        
                        //		CellFormatter cellFormatter = new CellFormatter() {
                        //			
                        //			@Override
                        //			public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
                        //				return record.getAttributeAsString("dsc") + " - " + record.getAttributeAsString("xmlid");
                        //			}
                        //		};
                        		
                        		ListGridField idField = new ListGridField("xmlid");
                        		ListGridField dscField = new ListGridField("dsc");
                        		setPickListFields(idField, dscField);
                        
                        		setOptionDataSource(ds);
                        		setAutoFetchData(true);
                        		
                        //		dscField.setCellFormatter(cellFormatter);
                        	}
                        }
                        When I initially display my Form the field xmlid is displayed but shows as only the description; I want to create a custom description that shows the description and value like we did in the SelectItem.

                        Thank you for your help

                        Comment


                          #13
                          Does my explanation help?

                          Comment


                            #14
                            Originally posted by Isomorphic
                            Note the separate formatter/parser APIs on the item itself - use those. Call getValue() to get the stored value, getDisplayValue() to get the display value (these are the two you want to combine, right)?

                            Note that at some points, the displayValue is not available (eg, immediately after a setValue() call, before fetchMissingValues has done it's job). Your formatter will be automatically called again when the displayValue has been loaded.
                            Ok I tried this as below but I get StackExceptions do I need to change something?

                            Exception : too much recursion
                            Code:
                            SelectItem tmp = new SelectItem("xmlid");
                            tmp.setValueFormatter(new FormItemValueFormatter() {
                            			
                            	@Override
                            	public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {
                            		if(item != null && item.getDisplayValue() != null) {
                            			return item.getDisplayValue() + " - " + item.getValue();
                            		}
                            		else {
                            			return record.getAttribute("xmlid");
                            		}
                            	}
                            });
                            df.setFields(tmp);
                            Last edited by gilcollins; 6 Aug 2010, 10:50.

                            Comment


                              #15
                              What StackExceptions are you getting? If you're ever getting an error, you always need to post it.

                              You seem to be assuming "record" will be non-null if displayValue is null, this is not necessarily the case.

                              Also, "record" is the values currently being edited in the form, *not* the selected record, which, if a record has been selected, is available from getSelectedRecord().

                              Comment

                              Working...
                              X