Announcement

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

    Which foreign key field will be used as a TreeGrid "tree" field ?

    Hi,
    I have DataSource similar to EmployeeXmlDS used in TreeGrid showcase examples. The only difference is that I have got more than one foreign key defined prior "ReportsTo" (otherField is a foreign key and it is placed BEFORE tree relationship field reportsToField):
    Code:
       public EmployeeXmlDS(String id) {
    
            setID(id);
            setTitleField("Name");
            setRecordXPath("/List/employee");
            DataSourceTextField nameField = new DataSourceTextField("Name", "Name", 128);
    
            DataSourceIntegerField employeeIdField = new DataSourceIntegerField("EmployeeId", "Employee ID");
            employeeIdField.setPrimaryKey(true);
            employeeIdField.setRequired(true);
    
            DataSourceIntegerField otherField = new DataSourceIntegerField("Other", "Other");
            otherField.setRequired(true);
            otherField.setForeignKey("OtherDS.id");
    
            DataSourceIntegerField reportsToField = new DataSourceIntegerField("ReportsTo", "Manager");
            reportsToField.setRequired(true);
            reportsToField.setForeignKey(id + ".EmployeeId");
            reportsToField.setRootValue("1");
    
            setFields(nameField, employeeIdField, otherField, reportsToField);
    
            setDataURL("ds/test_data/employees.data.xml");
            setClientOnly(true);
        }
    }
    I have noticed that TreeGrid bound to such DataSource takes FIRST foreign key field as the tree relationship which is otherField in my example. Is there a way to somehow point DataSource or TreeGrid which field should be considered as "tree" field ?
    Thanks,
    MichalG

    #2
    Take a look at the Tree Data Binding docs for details on the fields used to create the tree. Then take a look at the TG.fetchData() and Tree docs to set the idField and parentIdField as desired.

    Comment


      #3
      Thanks for the link. However according this part of the documentation:
      "com.smartgwt.client..ResultTrees are created for you by the TreeGrid when you set dataSource, but you can pass an initial dataset to a databound TreeGrid by setting initialData. The idField is derived from the dataSource you provide to the TreeGrid - the first field marked as primaryKey:true becomes the idField of the ResultTree. The parentIdField is found by looking for a field that has a foreignKey property pointing to the idField."
      I would understand that in case of ResultTree created as the result of setting DataSource, the parentIdField is chosen for me.

      I did try to setParentId for the Tree this way:
      Code:
         public EmployeeXmlDS() {
      
              setID("EmployeeDS");
              setTitleField("Name");
      
              DataSourceTextField nameField = new DataSourceTextField("Name", "Name", 128);
      
              DataSourceIntegerField employeeIdField = new DataSourceIntegerField("id", "Employee ID");
              employeeIdField.setPrimaryKey(true);
              employeeIdField.setRequired(true);
      
              DataSourceIntegerField otherField = new DataSourceIntegerField("Other", "Other");
              otherField.setRequired(true);
              otherField.setForeignKey("OtherDS.id");
      
              DataSourceIntegerField reportsToField = new DataSourceIntegerField("ReportsTo", "Manager");
              reportsToField.setRequired(true);
              reportsToField.setForeignKey("EmployeeDS.id");
              reportsToField.setRootValue("1");
      
              setFields(nameField, employeeIdField, otherField, reportsToField);
          }
      }
      
          public void onModuleLoad() {
              TreeGrid employeeTreeGrid = new TreeGrid();
              employeeTreeGrid.setAutoFetchData(true);
              employeeTreeGrid.setDataSource(EmployeeXmlDS.getInstance());
              employeeTreeGrid.getTree().setParentIdField("ReportsTo");       
              employeeTreeGrid.draw();
          }
      but still I am observing wrong criteria for the OtherDS instead of EmployeeDS in the request.
      MichalG

      Comment


        #4
        All of this is true, however, see TG.setDataProperties for a way to customize the Tree. Just setup a Tree template as desired, apply it there and then let SGWT create the resulting Tree from your DataSource.

        Comment

        Working...
        X