Announcement

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

    DataSource and DSResponse does not work with TreeGrid

    Hello,
    A custom data source does not work with a TreeGrid for me.

    This is the code I use to return the tree data from the data source:

    Code:
      @Override
      protected Object transformRequest( DSRequest request )
      {
    
        if ( request.getOperationType() == DSOperationType.FETCH )
        {
          
          VorgangAuftragNode v1 = new VorgangAuftragNode();
          v1.setID( "v1" );
          v1.setParentID( "root" );
          v1.setData( new Vorgang( "Haus bauen", 1 ) );
    
          VorgangAuftragNode v2 = new VorgangAuftragNode();
          v2.setID( "v2" );
          v2.setData( new Vorgang( "Baum bauen", 2 ) );
          v2.setParentID( "v1" );
    
          VorgangAuftragNode v3 = new VorgangAuftragNode();
          v3.setID( "v3" );
          v3.setData( new Auftrag( "Loch buddeln", 1, new Mitarbeiter( 1, "Bernhold Besenstiel" ), 33d, 15d,
              "Bla", true ) );
          v3.setParentID( "v2" );
    
          VorgangAuftragNode v4 = new VorgangAuftragNode();
          v4.setID( "v4" );
          v4.setData( new Auftrag( "Baum buddeln", 2, new Mitarbeiter( 1, "Bernhold Besenstiel" ), 44d, 15d,
              "Bla", true ) );
          v4.setParentID( "v2" );
    
          DSResponse response = new DSResponse();
          response.setStatus( 0 );
          response.setAttribute( "clientContext", request.getAttributeAsObject( "clientContext" ) );
          response.setData( new VorgangAuftragNode[]{ v1, v2, v3, v4 } );
          processResponse( request.getRequestId(), response );
    
        }
    
        return request.getData();
    
      }
    This is what happens:



    The last two nodes should be leaves without children, but for some reason they are displayed as folders with children (which do not exist). When I click one of the last two nodes to expand it, the TreeGrid becomes empty.

    This is how it should look like:



    I can achieve this result easily without using a DataSource:

    Code:
        VorgangAuftragNode v1 = new VorgangAuftragNode();
        v1.setID( "v1" );
        v1.setParentID( "root" );
        v1.setData( new Vorgang( "Haus bauen", 1 ) );
    
        VorgangAuftragNode v2 = new VorgangAuftragNode();
        v2.setID( "v2" );
        v2.setData( new Vorgang( "Baum bauen", 2 ) );
        v2.setParentID( "v1" );
    
        VorgangAuftragNode v3 = new VorgangAuftragNode();
        v3.setID( "v3" );
        v3.setData( new Auftrag( "Loch buddeln", 1, new Mitarbeiter( 1, "Bernhold Besenstiel" ), 33d, 15d, "Bla",
            true ) );
        v3.setParentID( "v2" );
    
        VorgangAuftragNode v4 = new VorgangAuftragNode();
        v4.setID( "v4" );
        v4.setData( new Auftrag( "Baum buddeln", 2, new Mitarbeiter( 1, "Bernhold Besenstiel" ), 44d, 15d, "Bla",
            true ) );
        v4.setParentID( "v2" );
    
        Tree treeModel = new Tree();
        treeModel.setModelType( TreeModelType.PARENT );
        treeModel.setRootValue( "root" );
        treeModel.setData( new VorgangAuftragNode[]{ v1, v2, v3, v4 } );
    
        treeGrid.setData( treeModel );
    This works as expected. The main difference is that I used a Tree and added it to the TreeGrid which is not possible with a DataSource. What am I doing wrong?

    How can I achieve this using a custom data source? Is this even possible?

    DSResponse.setData() only accepts an array of records. Is there a way to return a tree model from a DataSource rather than an array of records?

    Can someone give a working example of a TreeGrid with a custom data source?
    Last edited by jballh; 13 Jul 2009, 05:19.

    #2
    I have now achieved the desired result by explicitly setting isFolder(false) on the leaf nodes. Is this the correct way to do it? Or can a node automatically determine if it is a leaf node?
    Last edited by jballh; 13 Jul 2009, 06:16.

    Comment


      #3
      Same problem

      dear SmartGWT community,

      I'm (was until now, thanks community!) experiencing the exact same problem a jballh mentioned above. To me it also seems, that the default behavior is erroneous - TreeGrid correctly determines the hierarchy of objects, but fails to recognize the leaves..

      Just to explain to accidental reader, the "isFolder attribute" mentioned is a normal record attribute accessed by node.setAttribute("isFolder", true); call.. The solution works just fine.

      What would interest me is, whether and where is this option documented other than here in this forum. Probably I'm not working correctly with the docs - and it's not the first time I'm struggling... Some suggestions?

      anyways thakns a million for this thread, all the best,
      .. peter

      Comment


        #4
        See tree.defaultIsFolder. In a nutshell, leaves are autodetected if loadDataOnDemand is false. If data is being loaded on demand, a node with no children is assumed to be a folder that has not loaded children yet.

        Comment


          #5
          Please explain?

          I've been banging my head over the same thing - @jbalih, please explain how to return a Tree via DSResponse - it sounds like you have solved this.

          I have been trying to return an array of records, but wasn't understanding why my treegrid was flat. Thanks to your (partial) explanation, I see I need to construct a tree model. But as you point out DSResponse.setData() only accepts an array of records.

          Here's the onSuccess method from Fetch:
          Code:
                      public void onSuccess (List<ServiceNodeRecord> result) {   
                      	Tree treeModel = new Tree();
                      	treeModel.setModelType(TreeModelType.PARENT);
                      	treeModel.setRootValue("root");
                          TreeNode[] list = new TreeNode[result.size ()];
                          for (int i = 0; i < list.length; i++) {
                          	TreeNode record = new TreeNode();
                              copyValues (result.get (i), record);
                              list[i] = record;
                          }
                          treeModel.setData(list);
                   
                          response.setData (treeModel);  // ??? How to return a Tree here ???
          
                          processResponse (requestId, response);
          Here's the entire Fetch
          Code:
              @Override
              protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
                  ServiceNodesServiceAsync service = GWT.create (ServiceNodesService.class);
                  service.fetch (new AsyncCallback<List<ServiceNodeRecord>> () {
                      public void onFailure (Throwable caught) {
                          response.setStatus (RPCResponse.STATUS_FAILURE);
                          processResponse (requestId, response);
                      }
                      public void onSuccess (List<ServiceNodeRecord> result) {   
                      	Tree treeModel = new Tree();
                      	treeModel.setModelType(TreeModelType.PARENT);
                      	treeModel.setRootValue("root");
                          TreeNode[] list = new TreeNode[result.size ()];
                          for (int i = 0; i < list.length; i++) {
                          	TreeNode record = new TreeNode();
                              copyValues (result.get (i), record);
                              list[i] = record;
                          }
                          treeModel.setData(list);
                   
                          response.setData (treeModel);  // ??? How to return a Tree here ???
          
                          processResponse (requestId, response);
                      }
                  });
              }
          @isomorphic, can you explain further - does the folder attribute (tree.defaultIsFolder ??) somehow allow us to return a tree model through DSResponse? I'm confused and I've been working on this problem far too long now. (sorry, but I've left queries with full examples and no response) Maybe I just don't understand this stuff.... I feel dumb.


          Dave

          Comment


            #6
            Found my problem (working example link included)

            I now see how to structure a DataSource using setPrimaryKey, and setForeignKey. I don't feel so dumb.

            I've been able to simply return a Record[] to DSResponse and the nodes are now hierarchical.

            Code:
                        public void onSuccess (List<ServiceNodeRecord> result) {   
                            TreeNode[] list = new TreeNode[result.size ()];
                            for (int i = 0; i < list.length; i++) {
                            	TreeNode record = new TreeNode();		// changing datatype - TODO: for other methods
                                copyValues (result.get (i), record);
                                list[i] = record;
                            }
                            response.setData(list);
                            processResponse (requestId, response);
                        }
            I will post my correction to the DataSource here:
            http://forums.smartclient.com/showthread.php?t=6913

            Thanks,
            Dave

            Comment

            Working...
            X