我已经记录保存在数据库中我希望当我点击更新按钮时,它会从数据库中获取记录并默认选择并展开该记录,并显示所有其他记录但未选择但未展开,
这是我的模型代码:
public TreeNode getSelectedEmpMarketsForUpdate(int moID) {
ConnectionHandler conHandler = ConnectionHandler.getConnectionHandler();
Connection con = conHandler.getConnection();
Statement stmt = null;
ArrayList<MarketBean> list = new ArrayList<MarketBean>();
TreeNode root = new DefaultTreeNode(new BasicBean(1, "Root"), null);
root.setExpanded(true);
try {
ResultSet rs = null;
stmt = con.createStatement();
String selectQry = " select x.*,y.market_id as selected_id from (\n"
+ "select a.name as COUNTRY_NAME,a.id as COUNTRY_ID,b.name as ZONE_NAME, b.id as ZONE_ID, c.name as REGION_NAME, \n"
+ "C.ID as REGION_ID, d.name as CITY_NAME, D.ID AS CITY_ID, e.name as AREA_NAME, E.ID AS AREA_ID,f.name as MARKET_NAME, \n"
+ "F.ID AS MARKET_ID, concat(a.name,concat(b.name,concat(c.name,concat(d.name,concat(e.name,f.name))))) as completeName \n"
+ "from country a left outer join zone b on a.id=b.country_id left outer join region c on b.id=c.zone_id \n"
+ "left outer join city d on c.id=d.region_id left outer join area e on d.id=e.city_id \n"
+ "left outer join market f on e.id=f.area_id \n"
+ "where a.name like '%%' OR b.name like '%%' OR c.name like '%%' OR d.name like '%%' OR e.name like '%%' \n"
+ "OR f.name like '%%') x\n"
+ "\n"
+ "LEFT OUTER JOIN\n"
+ "(select F.ID AS MARKET_ID\n"
+ " from country a left outer join zone b \n"
+ " on a.id=b.country_id \n"
+ " left outer join region c \n"
+ " on b.id=c.zone_id \n"
+ " left outer join city d \n"
+ " on c.id=d.region_id \n"
+ " left outer join area e \n"
+ " on d.id=e.city_id \n"
+ " left outer join market f \n"
+ " on e.id=f.area_id \n"
+ " right join mo_market_bridge mb on mb.`MARKET_ID` = f.`ID`\n"
+ " where mb.`MO_ID` = '" + moID + " ' ) y\n"
+ " ON y.market_id=x.market_id";
System.out.println(selectQry);
rs = stmt.executeQuery(selectQry);
TreeMap<String, TreeNode> tm = new TreeMap<String, TreeNode>();
TreeNode country = null;
TreeNode zone = null;
TreeNode region = null;
TreeNode city = null;
TreeNode area = null;
TreeNode market = null;
int i = 0;
while (rs.next()) {
System.out.print(rs.getString("completeName"));
if (rs.getString("COUNTRY_NAME") != null && !tm.containsKey("CN" + rs.getString("COUNTRY_ID"))) {
country = new DefaultTreeNode(new BasicBean(rs.getInt("COUNTRY_ID"), rs.getString("COUNTRY_NAME")), root);
tm.put("CN" + rs.getString("COUNTRY_ID"), country);
if (rs.getBoolean("selected_id")) {
country.setExpanded(true);
country.setSelected(true);
}
}
if (rs.getString("ZONE_NAME") != null && !tm.containsKey("ZN" + rs.getString("ZONE_ID"))) {
zone = new DefaultTreeNode(new BasicBean(rs.getInt("ZONE_ID"), rs.getString("ZONE_NAME")), country);
tm.put("ZN" + rs.getString("ZONE_ID"), zone);
if (rs.getBoolean("selected_id")) {
zone.setExpanded(true);
zone.setSelected(true);
}
}
if (rs.getString("REGION_NAME") != null && !tm.containsKey("RG" + rs.getString("REGION_ID"))) {
region = new DefaultTreeNode(new BasicBean(rs.getInt("REGION_ID"), rs.getString("REGION_NAME")), zone);
tm.put("RG" + rs.getString("REGION_ID"), region);
if (rs.getBoolean("selected_id")) {
region.setExpanded(true);
region.setSelected(true);
}
}
if (rs.getString("CITY_NAME") != null && !tm.containsKey("CT" + rs.getString("CITY_ID"))) {
city = new DefaultTreeNode(new BasicBean(rs.getInt("CITY_ID"), rs.getString("CITY_NAME")), region);
tm.put("CT" + rs.getString("CITY_ID"), city);
if (rs.getBoolean("selected_id")) {
city.setExpanded(true);
city.setSelected(true);
}
}
if (rs.getString("AREA_NAME") != null && !tm.containsKey("AR" + rs.getString("AREA_ID"))) {
area = new DefaultTreeNode(new BasicBean(rs.getInt("AREA_ID"), rs.getString("AREA_NAME")), city);
tm.put("AR" + rs.getString("AREA_ID"), area);
if (rs.getBoolean("selected_id")) {
area.setExpanded(true);
area.setSelected(true);
}
}
if (rs.getString("MARKET_NAME") != null && !tm.containsKey("MK" + rs.getString("MARKET_ID"))) {
if (!rs.getString("MARKET_NAME").contains("MARKET")) {
String marketname = rs.getString("MARKET_NAME") + " MARKET ";
market = new DefaultTreeNode(new BasicBean(rs.getInt("MARKET_ID"), marketname), area);
tm.put("MK" + rs.getString("MARKET_ID"), market);
if (rs.getBoolean("selected_id")) {
market.setExpanded(true);
market.setSelected(true);
}
} else {
market = new DefaultTreeNode(new BasicBean(rs.getInt("MARKET_ID"), rs.getString("MARKET_NAME")), area);
tm.put("MK" + rs.getString("MARKET_ID"), market);
if (rs.getBoolean("selected_id")) {
market.setExpanded(true);
market.setSelected(true);
}
}
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
System.out.println(e);
}
conHandler.freeConnection(con);
}
return root;
}
这里是xhtml:
<p:tree value="#{marketMappinBean.allMarketsRoot}"
selectionMode="checkbox"
selection="#{marketMappinBean.selectedNode}"
dynamic="true"
var="node" id="treeNode" animate="true" highlight="true" style="width: auto "
>
<p:treeNode >
<h:outputText value="#{node.name}" />
</p:treeNode>
</p:tree>
这是bean:
MarketMappingModel db = new MarketMappingModel();
selectedMarketsroot = db.getSelectedEmployeeMarketsTree(moID);
默认选择并展开不起作用,但所有记录都显示