Announcement

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

    ForeignKey and ListGrid. Show name, but not id.

    Hi,

    I don't understand and didn't find solution for task.

    For example, I have two datasources - Users (it has id, login, department_id) and Departments (it has id, title). I get data with REST protocol.
    When I receive Users data - it has department_id which is foreign key for Departments.Id.
    I build grid with Users datasource, but I would like to show Department.title instead of department_id.

    How can I do it?

    I haven't found solution in SmartGWT showcase and google.

    Please help.

    Thanks.
    Last edited by MaEcTPo; 27 Jan 2010, 03:06.

    #2
    My English is ugly..

    I try to find, but haven't found solution, and ask you here.

    Sorry...

    Comment


      #3
      Is there someone in this forum who can help ?

      Comment


        #4
        You can do it one of two ways

        1) Have an extra field in the Users datasource, which is the value for the Department.id (ie, you send Department.title directly to the client)

        2) Set up the ListGridField with an OptionDataSource, and set the valueField to Department.id, and the displayField to Department.title.

        Comment


          #5
          You need to create ListGridField for you refercence field setting setOptionDataSource for it. Set displayField to control which field to show instead of id.

          And set listGrid.setUseAllDataSourceFields(true) to have mixture of datasource fields and listgrid fields

          Comment


            #6
            I used sunnyl suggestion 2) and it worked.
            Here is what I did for all those who are trying to implement it.

            The option data source (Departments, 'departmentDS' in the code) defines two fields: id (primary key) and title.
            Code:
            DataSourceTextField idField = new DataSourceTextField("id");
            idField.setPrimaryKey(true);
            DataSourceTextField titleField = new DataSourceTextField("title");
            The "main" data source (Users) that we bind to a list grid has one of its fields that references the department ID:
            Code:
            DataSourceTextField departmentIdField = new DataSourceTextField("department_id");
            And on the list grid, I override that field to attach the option data source:
            Code:
            ListGridField departmentTitleField = new ListGridField("department_id", "Department title");
            departmentTitleField.setType(ListGridFieldType.TEXT);
            departmentTitleField.setOptionDataSource(departmentDS);
            departmentTitleField.setValueField("id");
            departmentTitleField.setDisplayField("title");
            This works fine and the "Department title" column fetches correctly the department title, from the the option data source, through the department ID that is provided by the Users data source.

            But let's now assume that the Departments option data source defines another field: location.
            Code:
            DataSourceTextField idField = new DataSourceTextField("id");
            idField.setPrimaryKey(true);
            DataSourceTextField titleField = new DataSourceTextField("title");
            DataSourceTextField locationField = new DataSourceTextField("location");
            Then the following code fails:
            Code:
            ListGridField departmentTitleField = new ListGridField("department_id", "Department title");
            departmentTitleField.setType(ListGridFieldType.TEXT);
            departmentTitleField.setOptionDataSource(departmentDS);
            departmentTitleField.setValueField("id");
            departmentTitleField.setDisplayField("title");
            
            ListGridField departmentLocationField = new ListGridField("department_id", "Department location");
            departmentLocationField.setType(ListGridFieldType.TEXT);
            departmentLocationField.setOptionDataSource(departmentDS);
            departmentLocationField.setValueField("id");
            departmentLocationField.setDisplayField("location");
            In that case only the title is retrieved through the option data source and the "Department location" is never added to the list grid.
            It seems that SmartGWT doesn't allow to create two list grid fields from the same data source field, but in that case how can I fetch several fields from the option data source?

            Comment


              #7
              There is a workaround however to the issue I mentioned in my previous message.
              Since it appears that a DataSourceField can only be overriden by only one ListGridField, there is another way to "generate" several ListGridField overrides of a DataSourceField by duplicating it, and overriding each of its duplicates.
              In my previous message I needed to override "department_id" twice:
              - once in order to retrieve the department "title" from it through an option data source, and
              - another time in order to retrieve the department "location" through the same option data source.
              So what I did is to duplicate the DataSourceField "department_id" by creating another DataSourceField "department_id_2", and I added the corresponding field to my Record set via DataSource.transformResponse().

              Code:
              DataSourceTextField departmentIdField = new DataSourceTextField("department_id");
              DataSourceTextField departmentIdField_2 = new DataSourceTextField("department_id_2");
              For those interested, here is the transformResponse() of my main data source:
              Code:
              @Override
              protected void transformResponse(DSResponse response, DSRequest request, Object data) {
              		
              	for(Record record : response.getData()) {
              		String value = record.getAttribute("department_id");
              		if(value != null)
              			record.setAttribute("department_id_2", value);
              	} 
              	
              	super.transformResponse(response, request, data);
              }
              That works, and I am now able to display records mixing fields from two data sources within one single ListGrid.

              Again, for those interested, here is the way I defined the ListGridFields used to display the department's "title" and "location":

              Code:
              ListGridField departmentTitleField = new ListGridField("department_id", "Department title");
              departmentTitleField.setType(ListGridFieldType.TEXT);
              departmentTitleField.setOptionDataSource(departmentDS);
              departmentTitleField.setValueField("id");
              departmentTitleField.setDisplayField("title");
              
              ListGridField departmentLocationField = new ListGridField("department_id_2", "Department location");
              departmentLocationField.setType(ListGridFieldType.TEXT);
              departmentLocationField.setOptionDataSource(departmentDS);
              departmentLocationField.setValueField("id");
              departmentLocationField.setDisplayField("location");
              However, I find it a little disappointing that there is no way to override the same DataSourceField several times in a ListGrid.

              Could anyone from the Isomorphic team confirm that I am getting the logic right here?

              Comment

              Working...
              X