Announcement

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

    How to creating a Tree object from this flat list for Records

    Need help creating a Tree object from this flat list of Records returned by a DataSource.fetchData.

    I have looked over the documentation and searched all around but cannot see how to do this.

    Here is what I have

    Code:
    DataSource.get("...").fetchData(criteria, new DSCallback() {
        @Override
        public void execute(DSResponse dsResponse, Object o, DSRequest dsRequest) {
            treeGrid.setData(dsResponse.getData());
        }
    }, dsRequest);
    This doesn't work and as I've seen on other posts that I need to create a Tree object from this flat this and maybe do something like

    Code:
    treeGrid.getTree().setData(blah);
    However, how do I create the blah from "dsResponse.getData()"?
    Do I iterate through it and create TreeNodes from Records? This just doesn't seem right though!

    I have checked out the documentation but I can't seem to find what I want: http://www.smartclient.com/docs/6.0/a/b/c/go.html#class..Tree

    Help appreciated, thanks.

    #2
    Read the Tree Data Binding overview and look at the linked samples.

    Comment


      #3
      Unfortunately the example links are broken. Would appreciate if you could provide links to them. Thanks.

      Reference: http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/docs/TreeDataBinding.html

      Comment


        #4
        Hi,
        If you use a datasource you just have to bind the presentation widget to the datasource.
        This one should have a model organization(its fields..) which is conformed to what expect the widget somewhere. In the case of the treegrid you will need to have some fields which can be used to map to the node (id an parentid if your treegrid is in the parent mode). You have to build a list of treeNode in the execute fetch to make your datasource return it. The Treegrid setData method is to be used to initialize the treeGrid with already existing data.
        If you want to use a hierarchical data ther should be some key in each record to children or parent... It couldn't be really a flat list.

        BTW: The link is not broken....

        Regards

        Alain

        Comment


          #5
          Thanks for info. I suppose I'm still confused though. However I did find a solution, but please let me know if there is a better way.

          Basically here is my objective:
          1) Load the TreeGrid with certain folders open (preloaded) and selected
          2) The rest of the folder nodes will not yet have been fetched

          Because of how the design of TreeGrid, the only solution I found was to do a generic DataSource.fetchData and set the TreeGrids initialValues.

          To solve the problem with converting a flat record list to a TreeNode list I came up with this

          Code:
          DSRequest dsRequest = new DSRequest();
          dsRequest.setOperationId("...");
          DataSource.get("...").fetchData(criteria, new DSCallback() {
              @Override
              public void execute(DSResponse dsResponse, Object o, DSRequest dsRequest) {
                  TreeNode[] treeNodes = new TreeNode[dsResponse.getData().length];
                  for (int i=0; i<dsResponse.getData().length; i++) {
                      treeNodes[i] = new TreeNode(dsResponse.getData()[i].getJsObj());
                  }
          
                  treeGrid.setInitialData(treeNodes);
                  // bummer that I have to setInitialData first before I can display treeGrid, but I handle that here to addMember to the Canvas
              }
          }, dsRequest);

          Comment


            #6
            ?? this does the same thing that would be accomplished by providing the DataSource to the TreeGrid and calling fetchData().

            Comment


              #7
              BTW ...example links which are broken are within the Javadocs (see previous reference link)

              It points to examples here (redirects to download page): http://www.smartclient.com/examples/components/treeGrid_init.html

              Comment


                #8
                There is a difference with what I'm doing...

                I need to be able to call TreeGrid.fetchData with come criteria for only the first initial load. The criteria is used to query the database for all children records that were previously saved.

                These children records (and all their parents) need to be downloaded displayed with all folders open and selected (I handle setting isOpen and isSelected on the server). As well so we have then, we'll include all other root nodes (not in open or selected state).

                Now we can't use TreeGrid.fetchData because if we specific either (1) a different operationId or (2) criteria -- then all further fetch operations (like opening up another folder) use that same operationId or criteria, which was meant only for the initialData load. At time point we need the TreeGrid to behave normally, just download the parents children when folder opened.

                Comment


                  #9
                  That's a valid reason to provide initialData and then go from there, however, what are you critiquing about the TreeGrid's design in that case? It's hard to imagine shorter code.

                  Comment


                    #10
                    Honestly, I ended up at that through this discussion and learning through trial and error how I have to approach this problem.

                    To me, I guess I was thinking there there would be a better way at approaching this problem as maybe I was just trying to over engineer it.

                    But your right ....doesn't take much code and handles the problem nicely.

                    Thanks for quick responses! Certainly helped guide me on the right path.

                    Comment

                    Working...
                    X