public List<BasicProductType> findTreeByParentId(Long parentId) {
// TODO Auto-generated method stub
BasicProductType productType = new BasicProductType();
productType.setParentId(parentId);
List<BasicProductType> list = productTypeDAO.findByConditions(productType);
if (list != null && list.size() > 0) {
for (BasicProductType ptype : list) {
List<BasicProductType> tempList = findTreeByParentId(ptype.getId());
ptype.setTypeList(tempList);
}
}
return list;
}
private Long[] ptypeIds
private String ptypeTree;
ptypeList = productTypeService.findTreeByParentId(0L);
if(ptypeList != null && ptypeList.size() > 0){
StringBuffer sb = new StringBuffer();
buildCategoryData(ptypeList, sb, ptypeIds);
ptypeTree = sb.toString();
}
private void buildCategoryData(List<BasicProductType> ptypeList, StringBuffer sb, Long[] ptypeIds){
int i = 0;
sb.append("[ ");
for(BasicProductType ptype : ptypeList){
if(i == 0){
sb.append("{ ");
}else{
sb.append(",{ ");
}
i++;
sb.append("id:").append(ptype.getId()).append(", ");
sb.append("name:'").append(ptype.getName()).append("' ");
if(ptypeIds != null && ptype.getId().equals(ptypeIds[0]+"")){
sb.append(",'checked':true ");
}
if(ptype.TypeList() != null && ptype.TypeList().size() > 0){
sb.append(",'children': ");
buildCategoryData(ptype.TypeList(), sb, ptypeIds);
}
sb.append("} ");
}
sb.append("]");
}
遍历数据库产品类型信息,主要代码,仅供参考。