效果图
在查询表格中
<el-form-item label="商品分类:">
<el-cascader
clearable
v-model="selectProductCateValue"
:options="productCateOptions">
</el-cascader>
</el-form-item>
把两个变量定义出来
watch:{
selectProductCateValue(newValue){
this.query.productCategoryId = newValue[1];
}
},
data() {
return {
query: {
pageNum: 1,
pageSize: 5,
productCategoryId: null,
},
selectProductCateValue: null,//选择商品分类后要传到后台的值
productCateOptions:[],
};
}
methods定义方法发送请求:
//路径
import {
getCategoryListAll} from "../../../api/pms/product/product";
methods: {
// 查询商品分类
getCategoryList() {
getCategoryListAll(this.query).then(res => {
this.productCateOptions = res.data.data;
}).catch(res =>{
this.$.messages.error("获取商品分类失败")
})
},
}
js发起请求:
//查询商品分类数据
export const getCategoryListAll = () => {
return request({
url: 'http://localhost:8080/category/all',
method: 'get',
});
};
后端代码:
Controller类
//递归获取所有的分类
@GetMapping("all")
public ResultObj queryCateList(){
List<Map<String,Object>>categoryList=categoryService.queryCateList();
return ResultObj.success(categoryList);
}
实现类代码
@Override
public List<Map<String, Object>> queryCateList() {
List<Category>categoryList=super.list();
return getChidren(categoryList,0L);
}
private List<Map<String,Object>>getChidren(List<Category>categoryList,Long pid){
List<Map<String,Object>>list=new ArrayList<>();
categoryList.forEach(category -> {
Map<String,Object>map=null;
//一级分类
if(category.getParentId()==pid){
map=new HashMap<>();
map.put("label",category.getName());
map.put("value",category.getId());
//二级节点
if(category.getLevel()==0){
map.put("children",getChidren(categoryList,category.getId()));
}
}
if(map!=null){
list.add(map);
}
});
return list;
}