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();
}
}
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);