ZK 多个下拉菜单动态级联绑定对象

ZK学习博客http://minjiechenjava.iteye.com/category/110729

 

//前台页面的zul文件

=======================================================================================

userxml
<?xml version="1.0" encoding="utf-8"?>
<?page id="userPage"?>
<?xel-method prefix="c" name="hasPermission"
class="com.linktel.linkFax.web.zk.util.AuthorityTools"
signature="com.linktel.linkFax.web.zk.util.AuthorityTools  hasPermission(java.lang.String)"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" root="./userWin"?>
<window id="userWin" width="100%"
 use="com.linktel.linkFax.web.zk.controller.UserController" xmlns:h="http://www.w3.org/1999/xhtml"
 xmlns:n="http://www.zkoss.org/2005/zk/native" xmlns="http://www.zkoss.org/2005/zul"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
       <listbox id="userLbx"    model="@{userWin.userList}"
                    selectedItem="@{userWin.user}"  onSelect="showUserEdit();"
                    fixedLayout="true"  paginal="@{pagingUser}" >    
           <listhead>
                <listheader label="用户名" align="center"></listheader>
                <listheader label="手机号" align="center"></listheader>
                <listheader label="分机号" align="center"></listheader>
                <listheader label="职位" align="center"></listheader>
                <listheader label="传真" align="center"></listheader>
                <listheader label="启用状态" align="center"></listheader>
            </listhead>
            <listitem self="@{each=user}">
                <listcell label="@{user.username}"></listcell>
                <listcell label="@{user.phone}"></listcell>
                <listcell label="@{user.exten}"></listcell>
                <listcell label="@{user.position}"></listcell>
                <listcell label="@{user.fax}"></listcell>
                <listcell label="@{user.enabled,converter='com.linktel.linkFax.web.zk.converter.DataConverter'}"></listcell>
             </listitem>
        </listbox>
        <paging id="pagingUser" pageSize="3"></paging>
        <label value="公司名称:"></label>
        <combobox id="cbxCom" model="@{userWin.companyList}">
                       <comboitem self="@{each=companyEdit}" 
                                           label="@{companyEdit.name}"
                                           value="@{companyEdit.id}">
                       </comboitem>
         </combobox>
         <label value="部门名称:"></label>
         <combobox id="cbxDep" model="@{userWin.deptList}"
                                                 selectedItem="@{userWin.deptEdit}">
                      <comboitem self="@{each=deptEdit}"
                                           label="@{deptEdit.name}"
                                           value="@{deptEdit.id}">
                     </comboitem>
          </combobox>
</window>

=======================================================================================

Java代码 复制代码 收藏代码
  1. //后台绑定Controller类 下面有详细的注释
  2. publicclass UserController extends Window implements AfterCompose {
  3. protected AnnotateDataBinder binder;
  4. private User user = new User();
  5. private User u = new User();
  6. private Chinaregion province = new Chinaregion();
  7. private Chinaregion city = new Chinaregion();
  8. private Chinaregion district = new Chinaregion();
  9. private Role role = new Role();
  10. private Company company = new Company();
  11. protected Dept dept = new Dept();
  12. private Company companyEdit = new Company();
  13. private Dept deptEdit = new Dept();
  14. private Combobox cbxCompany;
  15. protected Listbox userLbx;
  16. protected Paging pagingUser;
  17. protected Combobox cbxProvince;
  18. protected Combobox cbxCity;
  19. protected Combobox cbxDistrict;
  20. private Combobox cbxDept;
  21. private Combobox cbxCom;
  22. private Combobox cbxDep;
  23. private List<Company> companyList;
  24. private List<User> userList;
  25. private List<Dept> deptList;
  26. private List<Role> roleList;
  27. private List<Chinaregion> provinceList;
  28. private List<Chinaregion> cityList;
  29. private List<Chinaregion> districtList;
  30. public Dept getDeptEdit() {
  31. return deptEdit;
  32. }
  33. publicvoid setDeptEdit(Dept deptEdit) {
  34. this.deptEdit = deptEdit;
  35. }
  36. public Dept getDept() {
  37. return dept;
  38. }
  39. public Chinaregion getDistrict() {
  40. return district;
  41. }
  42. publicvoid setDistrict(Chinaregion district) {
  43. this.district = district;
  44. }
  45. public Chinaregion getProvince() {
  46. return province;
  47. }
  48. public Chinaregion getCity() {
  49. return city;
  50. }
  51. publicvoid setCity(Chinaregion city) {
  52. this.city = city;
  53. }
  54. publicvoid setProvince(Chinaregion province) {
  55. this.province = province;
  56. }
  57. public Role getRole() {
  58. return role;
  59. }
  60. publicvoid setRole(Role role) {
  61. this.role = role;
  62. }
  63. publicvoid setDept(Dept dept) {
  64. this.dept = dept;
  65. }
  66. public List<Chinaregion> getProvinceList() {
  67. return provinceList;
  68. }
  69. publicvoid setProvinceList(List<Chinaregion> provinceList) {
  70. this.provinceList = provinceList;
  71. }
  72. public List<Chinaregion> getCityList() {
  73. return cityList;
  74. }
  75. publicvoid setCityList(List<Chinaregion> cityList) {
  76. this.cityList = cityList;
  77. }
  78. public List<Chinaregion> getDistrictList() {
  79. return districtList;
  80. }
  81. publicvoid setDistrictList(List<Chinaregion> districtList) {
  82. this.districtList = districtList;
  83. }
  84. public List<Role> getRoleList() {
  85. return roleList;
  86. }
  87. publicvoid setRoleList(List<Role> roleList) {
  88. this.roleList = roleList;
  89. }
  90. public Company getCompany() {
  91. return company;
  92. }
  93. publicvoid setCompany(Company company) {
  94. this.company = company;
  95. }
  96. public User getU() {
  97. return u;
  98. }
  99. publicvoid setU(User u) {
  100. this.u = u;
  101. }
  102. public List<Dept> getDeptList() {
  103. return deptList;
  104. }
  105. publicvoid setDeptList(List<Dept> deptList) {
  106. }
  107. public List<Company> getCompanyList() {
  108. return companyList;
  109. }
  110. publicvoid setCompanyList(List<Company> companyList) {
  111. this.companyList = companyList;
  112. }
  113. public User getUser() {
  114. return user;
  115. }
  116. public List<User> getUserList() {
  117. return userList;
  118. }
  119. publicvoid setUserList(List<User> userList) {
  120. this.userList = userList;
  121. }
  122. publicvoid setUser(User user) {
  123. this.user = user;
  124. }
  125. public Company getCompanyEdit() {
  126. return companyEdit;
  127. }
  128. publicvoid setCompanyEdit(Company companyEdit) {
  129. this.companyEdit = companyEdit;
  130. }
  131. /**
  132. * 组件创建时初始化的事件监听器
  133. */
  134. publicvoid init() {
  135. // UserService service= (UserService) SpringUtil.getBean("userService");
  136. // int count =service.countUser();
  137. // pagingUser.setTotalSize(count);
  138. pagingUser.addEventListener(ZulEvents.ON_PAGING,
  139. new UserPagingEventListener());
  140. userLbx.addEventListener(Events.ON_SELECT,
  141. new UserSelectedEventListener());
  142. cbxProvince.addEventListener(Events.ON_SELECT,
  143. new ProvinceSelectedEventListener());
  144. cbxCity.addEventListener(Events.ON_SELECT,
  145. new CitySelectedEventListener());
  146. cbxCom.addEventListener(Events.ON_SELECT,
  147. new EditCompanySelectedEventListener());
  148. if (judgeCompontent(cbxCompany)) {
  149. cbxCompany.addEventListener(Events.ON_SELECT,
  150. new CompanySelectedEventListener());
  151. }
  152. }
  153. /**
  154. * 初始化所属公司下的部门列表
  155. *
  156. * @param company
  157. */
  158. publicvoid initDept(Company company) {
  159. DeptService service = (DeptService) SpringUtil.getBean("deptService");
  160. deptList = service.getDeptsByCoId(company);
  161. binder.loadComponent(cbxDep);
  162. }
  163. /**
  164. * 初始化公司列表
  165. */
  166. publicvoid initCompany() {
  167. CompanyService service = (CompanyService) SpringUtil .getBean("companyService");
  168. companyList = service.getCompanyAll();
  169. }
  1. /**
  2. * 动态绑定变量值
  3. */
  4. @Override
  5. publicvoid afterCompose() {
  6. Components.wireVariables(this, this);
  7. Components.addForwards(this, this);
  8. initiaData();
  9. }

 

  1. class UserSelectedEventListener implements EventListener {
  2. @SuppressWarnings("unchecked")
  3. publicvoid onEvent(Event event) throws Exception {
  4. String cId = user.getCoid();
  5. // 获取Combobox组件中的Comboitem所有的子项集合
  6. List<Comboitem> comsItems = cbxCom.getChildren();
  7. // 使用List接口的迭代器,迭代每个Comboitem对象
  8. Iterator<Comboitem> comsIt = comsItems.iterator();
  9. // 是否有下个对象
  10. while (comsIt.hasNext()) {
  11. // 迭代对象转换成Comboitem对象
  12. Comboitem item = comsIt.next();
  13. // 判断item中的值是否相等,如果相等
  14. if (StringUtils.equals((String) item.getValue(), cId)) {
  15. // 如果相等 绑定并且选中该对象
  16. cbxCom.setSelectedItemApi(item);
  17. // 把该事件放入时间列队的末尾,在所有事件都处理完 之后,将会立即处理该事件
  18. Events.postEvent(Events.ON_SELECT, cbxCom, null);
  19. // 初始化部门对象
  20. Company c = new Company();
  21. c.setId(cId);
  22. initDept(c);
  23. break;
  24. }
  25. }
  26. }
  27. }

 

  1. class EditCompanySelectedEventListener implements EventListener {
  2. publicvoid onEvent(Event event) throws Exception {
  3. if (deptList == null || deptList.isEmpty())
  4. return;
  5. // 循环迭代Dept集合对象
  6. for (Iterator<Dept> it = deptList.iterator(); it.hasNext();) {
  7. Dept each = it.next();
  8. if (StringUtils.equals(user.getDeptid(), each.getId())) {
  9. deptEdit = each;// 赋值给cbxDep绑定的对象;
  10. // 重新绑定对象
  11. binder.loadComponent(cbxDep);
  12. return;
  13. }
  14. }
  15. }
  16. }

你可能感兴趣的:(下拉菜单)