效果
1.数据库
父类目ID parent_id=0时,代表的是一级的分类
首先查询出一级分类
以手机为例,它的id为558,即它的二级分类的parent_id就是558
首先查询出二级分类
继续查询出三级分类
2.后端
由于数据库及查询已经写得很详细了,后端代码不祥说了
- ItemCatController.java
/**
* 根据上级ID查询商品分类列表
* @param parentId
* @return
*/
@RequestMapping("/findByParentId")
public List<TbItemCat> findByParentId(Long parentId) {
return itemCatService.findByParentId(parentId);
}
3.前端
- itemCatService.js
//服务层
app.service('itemCatService',function($http){
//根据上级分类查询商品分类列表
this.findByParentId=function(parentId){
return $http.get('../itemCat/findByParentId.do?parentId='+parentId);
}
});
- goodsController.js
//控制层
app.controller('goodsController',function($scope,$controller,goodsService,itemCatService){
//查询一级商品分类列表
$scope.selectItemCat1List=function(){
itemCatService.findByParentId(0).success(
function(response){
$scope.itemCat1List=response;
}
);
}
//$watch方法用于监控某个变量的值,当被监控的值发生变化,就自动执行相应的函数。
//查询二级商品分类列表
$scope.$watch('entity.goods.category1Id',function(newValue,oldValue){
itemCatService.findByParentId(newValue).success(
function(response){
$scope.itemCat2List=response;
}
);
});
//查询三级商品分类列表
$scope.$watch('entity.goods.category2Id',function(newValue,oldValue){
itemCatService.findByParentId(newValue).success(
function(response){
$scope.itemCat3List=response;
}
);
});
});
- goods_edit.html
引入js
<script type="text/javascript" src="../js/service/itemCatService.js"></script>
<script type="text/javascript" src="../js/controller/goodsController.js"></script>
页面加载调用selectItemCat1List方法,查询一级商品分类列表
修改goods_edit.html一级分类下拉选择框
<td>
<select class="form-control"
ng-model="entity.goods.category1Id"
ng-options="item.id as item.name for item in itemCat1List">
</select>
</td>
ng-options属性可以在表达式中使用数组或对象来自动生成一个select中的option列表。ng-options与ng-repeat很相似,很多时候可以用ng-repeat来代替ng-options。但是ng-options提供了一些好处,例如减少内存提高速度,以及提供选择框的选项来让用户选择。
<option value="1">图书、音像、电子书刊</option>
item.id as item.name for item in itemCat1List
这里 item.id as item.name for item in itemCat1List我们使用 itemCat1List 对象的 id作为select的value(即“1”),使用itemCat1List的name 作为 select 的label(即“图书、音像、电子书刊”)
修改goods_edit.html二、三级分类下拉选择框
<td>
<select class="form-control select-sm" ng-model="entity.goods.category2Id" ng-options="item.id as item.name for item in itemCat2List"></select>
</td>
<td>
<select class="form-control select-sm" ng-model="entity.goods.category3Id" ng-options="item.id as item.name for item in itemCat3List"></select>
</td>