部门表的树状下拉框实现
public class DepartmentForTree {
private int id;
private String text;
private int pid;
private List<DepartmentForTree> children
= new ArrayList<DepartmentForTree>();
public DepartmentForTree() {
super();
}
public DepartmentForTree(int id, String text) {
super();
this.id = id;
this.text = text;
}
public DepartmentForTree(int id, String text, int pid) {
super();
this.id = id;
this.text = text;
this.pid = pid;
}
public DepartmentForTree(int id, String text,
List<DepartmentForTree> children) {
super();
this.id = id;
this.text = text;
this.children = children;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List<DepartmentForTree> getChildren() {
return children;
}
public void setChildren(List<DepartmentForTree> children) {
this.children = children;
}
@Override
public String toString() {
return "DepartmentForTree [id=" + id + ", text=" + text + ", children="
+ children + "]";
}
}
service实现方法
public class DepartmentImpl implements IDepartmentBiz{
private DepartmentDao dao = new DepartmentDao();
public List<DepartmentForTree> queryAllDepartmentsForTree() {
String sql = "SELECT "
+ " departmentID id , departmentName text "
+ ", parentID pid " //departmentID是二级部门ID parentID是一级部门ID
+ " FROM department order by parentID asc";
List<DepartmentForTree> departments =
dao.query(sql, DepartmentForTree.class, new Object[]{});
List<DepartmentForTree> ones = new ArrayList<DepartmentForTree>();
List<DepartmentForTree> twos = new ArrayList<DepartmentForTree>();
for (DepartmentForTree department : departments) {
if(department.getPid() == 0)
{
ones.add( department );
}else
{
twos.add( department);
}
}
for (DepartmentForTree departmentOne : ones) {
for (DepartmentForTree departmentTwo : twos) {
if( departmentOne.getId() ==
departmentTwo.getPid())
{
departmentOne.getChildren()
.add(departmentTwo);
}
}
}
return ones;
}
Servlet实现方法
private void queryAll(HttpServletRequest request,
HttpServletResponse response) throws IOException {
List<DepartmentForTree> departments = biz.queryAllDepartmentsForTree();
departments.add(
new DepartmentForTree( 0 , "全部" ) );
String jsonString = JSON.toJSONString(departments);
PrintWriter out = response.getWriter();
out.print(jsonString);
out.flush();
out.close();
}
combotree方法
$(
function () {
$("#departmentID").combotree(
{
url:'http://localhost:8080/KQSYS/DepartmentController/queryAllDepartmentsForTree',
height:26,
width:'150px',
onBeforeSelect : function(note)
{
var rows = note.children;
if(rows.length>0)
{
$("#departmentID").treegrid("unselect");
}
}
,onSelect:function () {
}
});
});
有错误请指正 谢谢。