asked
2014-11-07 11:02:54 +0800
jeevaJR7 1 ● 2 I have created dynamic tree using mvvm and drag and drop using composer.
but the problem is this tree is always closed.I want this tree to always open whenever i load this file from database .is any solution available for that? and another question is can i make this dynamic tree as organaizational treeview in this same program using zk??
tree.zul
<zk>
viewModel="@init('com.hierarchy.vm.HierarchyVM') @id('vm')" width="100%" height="100%"
validationMessages="@id('vmsgs')" >
<hlayout style="background:silver">
<div width="100%" height="800px" style="background:silver" align="center">
<tree id="tree1" height="100%" width="100%" model="@load(vm.treeModel)" droppable="true" >
<template name="model" var="emp">
<treeitem open="true" >
<treerow droppable="true" draggable="true" apply="com.hierarchy.vm.HierarchyVM" >
<treecell >
<button width="200px" height="40px" style="color:blue" label="@load(c:cat5(emp.data.name,' ','[',emp.data.title,']'))"></button>
</treecell>
</treerow>
</treeitem>
</template>
<zscript><![CDATA[
]]></zscript>
</tree>
</div>
</hlayout>
</zk>
view model.java
private EmployeeData employeeData;
private ListModelList<EmployeeData> employeeList;
public EmployeeData getEmployeeData() {
return employeeData;
}
public void setEmployeeData(EmployeeData employeeData) {
this.employeeData = employeeData;
}
public ListModelList<EmployeeData> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(ListModelList<EmployeeData> employeeList) {
this.employeeList = employeeList;
}
@Init
public void init(){
employeeData = new EmployeeData();
employeeList = new ListModelList<EmployeeData>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyDb","root","root");
String query = "Select * from EmployeeData";
PreparedStatement preparedStatement=connection.prepareStatement(query);
ResultSet resultSet=preparedStatement.executeQuery();
while(resultSet.next()){
employeeList.add(new EmployeeData(resultSet.getString("title"),resultSet.getString("name")));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public DefaultTreeModel<EmployeeData> getTreeModel() {
DefaultTreeModel<EmployeeData> result = genTreeModel();
return result;
}
public DefaultTreeModel<EmployeeData> genTreeModel() {
DefaultTreeNode<EmployeeData> root = new DefaultTreeNode<EmployeeData>(null, mockNode(0));
DefaultTreeModel<EmployeeData> result = new DefaultTreeModel<EmployeeData>(root);
return result;
}
public ArrayList<DefaultTreeNode<EmployeeData>> mockNode(int level){
ArrayList<DefaultTreeNode<EmployeeData>> result = new ArrayList<DefaultTreeNode<EmployeeData>>();
for(int i=0; i<employeeList.size(); i++){
if(i==level){
DefaultTreeNode<EmployeeData> root = new DefaultTreeNode<EmployeeData>
(employeeList.get(i), mockNode(level+1));
if(root!=null){
result.add(root);
}
}
}
return result;
}
@SuppressWarnings({ "unchecked" })
public void doAfterCompose(Component win) throws Exception {
win.addEventListener("onDrop", new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
// TODO Auto-generated method stub
DropEvent dropevent = ((DropEvent) event);
List list = dropevent.getTarget().getParent().getChildren();
for (Object element : list) {
if (element instanceof Treechildren) {
((Treechildren)element).appendChild((dropevent.getDragged().getParent()));
}
}
}
});
}
}
most important here is not to mix model based Tree creation, and then update the components directly, if a tree model is used, then changes to the tree should be done in the treemodel. Of course DefaultTreeModel is Read only and needs to be extended for that
cor3000 ( 2014-11-10 09:20:12 +0800 )editthis demo http://www.zkoss.org/zkdemo/tree/dynamic_tree shows how a TreeModel can be implemented to be modifiable, then addOpenObject works naturally
cor3000 ( 2014-11-10 09:21:27 +0800 )edit