3

I have a datasource in SmartGWT. How do i extract data from it? I have tried using fetchData()method but then in criteria i need to add a value i.e.

Criteria cr = new Criteria();
cr.addCriteria("Name", "Name_Value");

In order to execute a selectall on the DataSource what do i need to do?

Here is the code :

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.widgets.TransferImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VStack;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.widgets.tree.TreeGridField;

public class TreeDragNodesSample implements EntryPoint {
TreeGrid grid1 = new TreeGrid();
TreeGrid grid2 = new TreeGrid();
String name=null;
    public void onModuleLoad() {
Tree test = new Tree();

        grid1.setDragDataAction(DragDataAction.COPY);
        grid1.setAutoFetchData(true);
        grid1.setDataSource(EmployeeXmlDS.getInstance());
        grid1.setWidth(200);
        grid1.setHeight(200);
        grid1.setShowEdges(true);
        grid1.setBorder("0px");
        grid1.setBodyStyleName("normal");
        grid1.setShowHeader(false);
        grid1.setLeaveScrollbarGap(false);
        grid1.setEmptyMessage("<br>Drag & drop parts here");
        grid1.setManyItemsImage("cubes_all.png");
        grid1.setAppImgDir("icons/16/");
        grid1.setNodeIcon("cube.png");
        grid1.setFolderIcon("person.png");
        grid1.setCanReorderRecords(true);
        grid1.setCanAcceptDroppedRecords(true);
        grid1.setCanDragRecordsOut(true);
        grid1.setCanAcceptDrop(true);
        grid1.setCanDrop(true);
        TreeGridField tname = new TreeGridField("Name");
        TreeGridField child = new TreeGridField("ChildID");
        TreeGridField reps = new TreeGridField("ReportsTo");
        grid1.setFields(tname);

        grid2.setLeft(250);
        grid2.setAutoFetchData(true);
        grid2.setDataSource(EmployeeXmlDSDrop.getInstance());
        grid2.setWidth(200);
        grid2.setHeight(200);
        grid2.setShowEdges(true);
        grid2.setBorder("0px");
        grid2.setBodyStyleName("normal");
        grid2.setShowHeader(false);
        grid2.setLeaveScrollbarGap(false);
        grid2.setEmptyMessage("<br>Drag & drop parts here");
        grid2.setManyItemsImage("cubes_all.png");
        grid2.setAppImgDir("icons/16/");
        grid2.setNodeIcon("cube.png");
        grid2.setFolderIcon("person.png");
        grid2.setCanReorderRecords(true);
        grid2.setCanAcceptDroppedRecords(true);
        grid2.setCanDragRecordsOut(true);
        grid2.setCanAcceptDrop(true);
        grid2.setCanDrop(true);
        TreeGridField tname2 = new TreeGridField("Name");
        TreeGridField child2 = new TreeGridField("ChildID");
        TreeGridField reps2 = new TreeGridField("ReportsTo");
        grid2.setFields(tname2);

        VStack moveControls = new VStack(10);
        moveControls.setWidth(32);
        moveControls.setHeight(74);
        moveControls.setLayoutAlign(Alignment.CENTER);

        TransferImgButton rightArrow = new TransferImgButton(TransferImgButton.RIGHT, new ClickHandler() {

            public void onClick(ClickEvent event) {
                grid2.transferSelectedData(grid1);
            }
        });
        moveControls.addMember(rightArrow);

        TransferImgButton leftArrow = new TransferImgButton(TransferImgButton.LEFT, new ClickHandler() {

            public void onClick(ClickEvent event) {
                grid1.transferSelectedData(grid2);
            }
        });
        moveControls.addMember(leftArrow);

        HStack grids = new HStack(10);
        grids.setHeight(160);
        grids.addMember(grid1);
        grids.addMember(moveControls);
        grids.addMember(grid2);
        grids.draw();
    }
}
Sagar Tandel
  • 299
  • 1
  • 3
  • 10

1 Answers1

1

You do not execute a select all operation on a DataSource. You rather do it on its databounded object, e.g. the associated ListGrid. Provided that the data have been set and loaded in the ListGrid listGrid, you just call

listGrid.getRecords();

which returns a Record[], with all the visible, or matching to a set of criteria records. Another option, for databounded ListGrids, is to use the ResultSet object. This is handy when you want to find specific records in your data, but since you require all the records, I would suggest you just use the getAllRecords method. For more details look at the API

Update: Based on your code extract, I can now see that you are using a TreeGrid, instead of a ListGrid. Thus the method you should use is the:

TreeGrid.getTree().getAllNodes();

This will give you all the nodes of the tree, that have been loaded, no matter if they are opened or closed. The ListGrid.getRecords() would only return the opened nodes. Notice that the TreeGrid, by default, loads its data on demand. So if you want to retrieve all the tree's nodes, you have to deactivate this feature and pre-load all the data. This can of course create speed issues. To achieve this do the following:

TreeGrid.setLoadDataOnDemand(Boolean.FALSE);
gpapaz
  • 889
  • 14
  • 23
  • there is no method like getAllRecords() – Sagar Tandel Mar 15 '12 at 10:21
  • Oops, my mistake! The API link would have taken you to the correct method to use. In any case, I updated my answer with the correct method name. – gpapaz Mar 15 '12 at 10:29
  • even the getRecords() does not work...wen i get records using this method the whole project ceases to work.. – Sagar Tandel Mar 15 '12 at 10:56
  • Would you like to provide a part of your code so I can help you with what you are trying to do? – gpapaz Mar 15 '12 at 11:18
  • I am trying to achieve the same task mentioned here : http://stackoverflow.com/questions/9466780/drag-drop-with-xml-based-datasource-not-working-in-smartgwt but just was trying it some other way like first extract the nodes from the datasource and then set the Tree to the TreeGrid. Because if you set the TreeGrid datasource u are not able to get the dragged node. – Sagar Tandel Mar 15 '12 at 12:02
  • And the problem is i was able to copy the nodes from one tree to another but when i do a move operation it does not work. – Sagar Tandel Mar 15 '12 at 12:04
  • BTw..there is some problem with the getRecords() function...check this out : http://code.google.com/p/smartgwt/issues/detail?id=410 – Sagar Tandel Mar 15 '12 at 13:05
  • Nope there is not, as you can see from the whole list of comments. I have been using SmartGWT for 3 years now and getRecords is not causing me any troubles. To get back to your problem, unless you can provide me with a full code sample of your test case, so I can check your approach and reproduce the issue, I can't help ... Sorry! – gpapaz Mar 15 '12 at 15:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8935/discussion-between-jamshedkatta-and-gpapaz) – Sagar Tandel Mar 15 '12 at 16:22
  • But even the getTree() does not work...i have tried it...atleast not on my end don't know why...can you tell me what version of SmartGWT and GWT are you using...might be there are some clashes with the libraries.. – Sagar Tandel Mar 16 '12 at 11:14
  • nope still its not working for me...does it work fine at your end and even with the code i gave?? – Sagar Tandel Mar 16 '12 at 12:20
  • Use the chat link you provided. I will post there! – gpapaz Mar 16 '12 at 12:36