Announcement

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

    DataSource getFields() does not return same objects as getField()

    Hello,

    I've created a DataSource, assigned it on a ListGrid and called grid.getDataSource().getFields() and grid.getDataSource().getField("fieldname")

    When I examine the field[0] returned by getFields() it looks more like an object of the DataSource definition.
    Is something wrong with the implementation of DataSource.getFields() ?

    SmartGWT build 706

    Code:
    DataSourceField dsField1 = new DataSourceField();
    dsField1.setName("Show me");
    
    DataSourceField dsField2 = new DataSourceField();
    dsField2.setName("Don't show me 1");
    dsField2.setHidden(true);
    
    DataSourceField dsField3 = new DataSourceField("Don't show me 2", FieldType.ENUM);
    dsField3.setAttribute("defaultValue", "bla");
    dsField3.setValueMap("bla","ble");
    
    ...
    
    DataSourceField [] fields = grid.getDataSource().getFields();
    final DataSourceField field0 = fields[0];
    final DataSourceField field1 = grid.getDataSource().getField("Show me");
    
    
    ... debug output field0 and field1:
    
    
    com.smartgwt.client.data.DataSourceField.Show me = [object Object] com.google.gwt.core.client.JavaScriptObject$
    com.smartgwt.client.data.DataSourceField.Don't show me 1 = [object Object] com.google.gwt.core.client.JavaScriptObject$
    com.smartgwt.client.data.DataSourceField.Don't show me 2 = [object Object] com.google.gwt.core.client.JavaScriptObject$
    
    
    com.smartgwt.client.data.DataSourceField.name = Show me java.lang.String
    com.smartgwt.client.data.DataSourceField._typeDefaultsAdded = true (not an Object) java.lang.String
    com.smartgwt.client.data.DataSourceField.title = Show me java.lang.String

    #2
    What is your question? How does this affect you?

    Note that your field names are not valid (they should be valid identifiers).

    Comment


      #3
      I wanted to scan the datasource fields for a certain attribute (custom attribute I set myself when generating the fields, not the field's name) and looking at its type. That way I know it is the field I need and copy its valuemap to another field (instead of re-retrieving the valuemap).

      My question is why the class or type from the objects in the array as result of "getFields()" is not the same as calling "getField(x)".

      At the moment I do this, I do not know the name of the field I need to get, that's why I wanted to search all fields till I have the right one.

      Comment


        #4
        DataSource.getFields() and getFieldNames() error

        I have encountered an error while using DataSource.getFieldNames() method.
        I' using SmartGWT 1.3. Seems to be an error with the call to the underlying JSNI method.

        I have provided the fields to the datasource.

        Code:
        	public static final String ID = "Id";
        	public static final String PLU = "PluNum";
        	public static final String PROD_NAME = "ProductName";
        	public static final String PROD_PRICE = "ProductPrice";
        	public static final String STATUS = "Status";
        
        	public static String FIELD_NAMES[] = { ID, PLU, PROD_NAME, PROD_PRICE,
        			STATUS };
        
        	private static ProductDS instance = null;
        
        	public static ProductDS getInstance() {
        		if (instance == null) {
        			instance = new ProductDS("productDS");
        		}
        		return instance;
        	}
        
        	public ProductDS(String id) {
        
        		this.setID(id);
        
        		DataSourceField field;
        
        		field = new DataSourceField(ID, FieldType.INTEGER, "Product ID");
        		field.setPrimaryKey(true);
        		field.setHidden(true);
        		field.setRequired(true);
        		this.addField(field);
        
        		field = new DataSourceField(PLU, FieldType.TEXT, "PLU#");
        		field.setRequired(false);
        		this.addField(field);
        
        		field = new DataSourceField(PROD_NAME, FieldType.TEXT, "Product Name");
        		field.setRequired(true);
        		this.addField(field);
        
        		field = new DataSourceField(PROD_PRICE, FieldType.FLOAT,
        				"Product Price");
        		field.setRequired(true);
        		this.addField(field);
        
        		field = new DataSourceField(STATUS, FieldType.TEXT, "Status");
        		field.setRequired(false);
        		field.setCanEdit(false);
        		this.addField(field);
        
        	}
        But when I call the getFieldNames() method, I get this error.

        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:2359)
        	at web.client.ds.ProductDS.createCriteriaMap(ProductDS.java:252)
        	at web.client.ds.ProductDS.executeFetch(ProductDS.java:109)
        	at web.client.ds.GwtRpcDataSource.transformRequest(GwtRpcDataSource.java:73)] in
        Seems like 'data' is unknown in the JSNI method

        Code:
        /**
             * Retrieves the list of fields declared on this DataSource.
             *
             * @param excludeHidden If true, returns only those fields that are not marked as hidden
             * @return names of all fields declared on this DataSource
             */
            public native String[] getFieldNames(boolean excludeHidden) /*-{
                var self = this.@com.smartgwt.client.core.BaseClass::getOrCreateJsObj()();
                var value = self.getFieldNames(excludeHidden);
                if(value == null) return null;
                if(!@com.smartgwt.client.util.JSOHelper::isArray(Lcom/google/gwt/core/client/JavaScriptObject;)(data)) {
                    value = [value];
                }
                return @com.smartgwt.client.util.JSOHelper::convertToJavaStringArray(Lcom/google/gwt/core/client/JavaScriptObject;)(value);
            }-*/;
        I'm having problems with the getFields() method as well. Help would be appreciated.

        Comment


          #5
          Fixed DataSource.getFieldNames() in SVN. Can you describe the issue you're having with DS.getFields()?

          Thanks,
          Sanjiv

          Comment


            #6
            thanks sanjiv.

            Using the same DS in my post above, when using DataSource.getFields(), I always get a DataSourceField array with only 1 element and null properties.

            Code:
            for (DataSourceField field : ProductDS.getInstance().getFields()) {
            			fname = field.getName();
            			type = field.getType();
            			System.out.println("Field Name: " + fname);
            			System.out.println("Field Type: " + fname);
            ....
            and output

            Fields using 'getFields': 1
            Field Name: null
            Field Type: null

            Seems like the call to to JSNI method doesn't recognize 'fields' attribute as a JS array. So it creates a DataSourceField array with 1 element.

            In DataSource class

            Code:
            /**
                 * The list of fields that compose records from this DataSource.
                 * <p>
                 * Each DataSource field can have type, user-visible title, validators, and other metadata attached.
                 *
                 * @return array of DataSourceFields
                 */
                public DataSourceField[] getFields() {
                    return DataSourceField.convertToDataSourceFieldArray(getAttributeAsJavaScriptObject("fields"));
                }
            in DataSourceField class

            Code:
            public static DataSourceField[] convertToDataSourceFieldArray(JavaScriptObject nativeArray) {
                    if (nativeArray == null) {
                        return new DataSourceField[]{};
                    }
                    if (JSOHelper.isArray(nativeArray)) {
                        JavaScriptObject[] componentsj = JSOHelper.toArray(nativeArray);
                        DataSourceField[] objects = new DataSourceField[componentsj.length];
                        for (int i = 0; i < componentsj.length; i++) {
                            JavaScriptObject componentJS = componentsj[i];
                            objects[i] = DataSourceField.getOrCreateRef(componentJS);
                        }
                        return objects;
                    } else {
                        DataSourceField[] ret = new DataSourceField[1];
                        ret[0] = DataSourceField.getOrCreateRef(nativeArray);
                        return ret;
                    }
                }
            I may be wrong. ;) I just can't figure it out. thanks.

            Comment


              #7
              I've identified the issue with DataSource.getFields(). Feel free to create an issue and refer to this thread. For now please call getFieldNames() and iterate over it and call getField(fieldName) to obtain the list of DataSource fields.

              Sanjiv

              Comment


                #8
                Fixed in SVN.

                Comment

                Working...
                X