递归下拉列表树

public String add() throws Exception
{
Property property = new Property();
property.setDeleteFlag('0');
propertyList = this.getFacadeFactory().getGdsPropertyFacade().findByExample(property);
Category cate = new Category();
cate.setDeleteFlag('0');
List<CategoryDto> list1 = new ArrayList<CategoryDto>();
List<Category> list = this.getFacadeFactory().getGdsCategoryFacade().findByExample(cate);
dtoList = getNewList(new Long(0),list,list1,0);
return SUCCESS;
}
/**
*categoryList 为原先未排序的列表
*list1 排序完后的列表
* i 为递归的次数
*/

private List<CategoryDto> getNewList(Long id, List<Category> categoryList,List<CategoryDto> list1,int i) throws Exception
{
i++;
for(Category category :categoryList)
{
if(category.getParentId().equals(id))
{
CategoryDto categoryDto = new CategoryDto();
categoryDto.setId(category.getCateId());
categoryDto.setParentId(category.getParentId());
StringBuilder sb = new StringBuilder();
for(int j=0;j<i-1;j++) {
sb.append("&nbsp;&nbsp;");
}
categoryDto.setName(sb.append(category.getCateName()).toString());
list1.add(categoryDto);
getNewList(category.getCateId(),categoryList,list1,i);
}

}
return list1;
}

你可能感兴趣的:(下拉列表)