I am using SmartGwt Power 4.1p 2015-09-30 , Chrome 46.0.2490.80 OSX ElCapitan.
I'm developing TreeGrid and want to edit, add items inline in grid. It works but new added records aren’t save in database. After refetch I can see only changed data not inserted.
Following is a sample codes what I tried to test. Sample is based on BuiltInDs.
Pls help me to improve this sample.
public void initTreeGrid() {
treeGrid = new TreeGrid();
treeGrid.setWidth100();
treeGrid.setHeight(400);
treeGrid.setLoadDataOnDemand(false);
treeGrid.setDataSource(DataSource.get("employees"));
treeGrid.setAutoFetchData(true);
treeGrid.setCanEdit(true);
treeGrid.setCanRemoveRecords(true);
treeGrid.setAutoSaveEdits(false); // "true" + treeGrid.SaveAllEdits() also doesn't work
TreeGridField nameField = new TreeGridField("Name", 150);
TreeGridField idField = new TreeGridField("EmployeeId");
TreeGridField parentIdField = new TreeGridField("ReportsTo");
// btw: why ReportsTo field is invisible in grid?
TreeGridField jobField = new TreeGridField("Job", 150);
TreeGridField employeeStatusField = new TreeGridField("EmployeeStatus",
150);
TreeGridField genderField = new TreeGridField("Gender");
TreeGridField maritalStatusField = new TreeGridField("MaritalStatus");
treeGrid.setFields(nameField, idField, parentIdField, jobField,
employeeStatusField, genderField, maritalStatusField);
insertBtn = new IButton("Insert");
insertBtn.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
insertNode(0);
}
});
}
private int insertNode(int insertChild) {
DataSource seqDs = DataSource.get("employees_seq");
DSRequest request = new DSRequest();
request.setAttribute("insertChild", insertChild);
seqDs.fetchData(null, new DSCallback() {
public void execute(DSResponse response, Object rawData,
DSRequest request) {
Record r[] = response.getData();
String newId = r[0].getAttributeAsString("nextval");
int insertChild = request.getAttributeAsInt("insertChild");
Tree tree = treeGrid.getTree();
String parentName = tree.getParentIdField();
String idName = tree.getIdField();
TreeNode selNode = treeGrid.getSelectedRecord();
treeGrid.deselectAllRecords();
String parentId = null;
if (selNode != null) {
if (insertChild == 1) parentId = selNode.getAttribute(idName);
else parentId = selNode.getAttribute(parentName);
} else {
parentId = tree.getRootValue();
}
TreeNode newNode = new TreeNode();
newNode.setAttribute(parentName, parentId);
newNode.setAttribute(idName, newId);
tree.add(newNode);
if (insertChild == 1) treeGrid.openFolder(selNode);
int recordIndex = treeGrid.getRecordIndex(newNode);
treeGrid.scrollToRow(recordIndex);
treeGrid.startEditing(recordIndex);
}
}, request);
return 0;
}
I'm developing TreeGrid and want to edit, add items inline in grid. It works but new added records aren’t save in database. After refetch I can see only changed data not inserted.
Following is a sample codes what I tried to test. Sample is based on BuiltInDs.
Pls help me to improve this sample.
public void initTreeGrid() {
treeGrid = new TreeGrid();
treeGrid.setWidth100();
treeGrid.setHeight(400);
treeGrid.setLoadDataOnDemand(false);
treeGrid.setDataSource(DataSource.get("employees"));
treeGrid.setAutoFetchData(true);
treeGrid.setCanEdit(true);
treeGrid.setCanRemoveRecords(true);
treeGrid.setAutoSaveEdits(false); // "true" + treeGrid.SaveAllEdits() also doesn't work
TreeGridField nameField = new TreeGridField("Name", 150);
TreeGridField idField = new TreeGridField("EmployeeId");
TreeGridField parentIdField = new TreeGridField("ReportsTo");
// btw: why ReportsTo field is invisible in grid?
TreeGridField jobField = new TreeGridField("Job", 150);
TreeGridField employeeStatusField = new TreeGridField("EmployeeStatus",
150);
TreeGridField genderField = new TreeGridField("Gender");
TreeGridField maritalStatusField = new TreeGridField("MaritalStatus");
treeGrid.setFields(nameField, idField, parentIdField, jobField,
employeeStatusField, genderField, maritalStatusField);
insertBtn = new IButton("Insert");
insertBtn.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
insertNode(0);
}
});
}
private int insertNode(int insertChild) {
DataSource seqDs = DataSource.get("employees_seq");
DSRequest request = new DSRequest();
request.setAttribute("insertChild", insertChild);
seqDs.fetchData(null, new DSCallback() {
public void execute(DSResponse response, Object rawData,
DSRequest request) {
Record r[] = response.getData();
String newId = r[0].getAttributeAsString("nextval");
int insertChild = request.getAttributeAsInt("insertChild");
Tree tree = treeGrid.getTree();
String parentName = tree.getParentIdField();
String idName = tree.getIdField();
TreeNode selNode = treeGrid.getSelectedRecord();
treeGrid.deselectAllRecords();
String parentId = null;
if (selNode != null) {
if (insertChild == 1) parentId = selNode.getAttribute(idName);
else parentId = selNode.getAttribute(parentName);
} else {
parentId = tree.getRootValue();
}
TreeNode newNode = new TreeNode();
newNode.setAttribute(parentName, parentId);
newNode.setAttribute(idName, newId);
tree.add(newNode);
if (insertChild == 1) treeGrid.openFolder(selNode);
int recordIndex = treeGrid.getRecordIndex(newNode);
treeGrid.scrollToRow(recordIndex);
treeGrid.startEditing(recordIndex);
}
}, request);
return 0;
}
Comment