element el-cascader动态加载数据 (多级联动,落地实现)
element
A Vue.js 2.0 UI Toolkit for Web
项目地址:https://gitcode.com/gh_mirrors/eleme/element
免费下载资源
·
element el-cascader动态加载数据 (省市区三级联动)
选择效果图
选择完毕效果图
前端代码
- v-model:选择保存的值
- :props:配置项
- clearable:可清除
<el-form-item label="省市区" prop="addrIdPath">
<el-cascader
style="width: 340px;"
v-model="form.addrIdPath"
:props="address"
clearable
>
</el-cascader>
</el-form-item>
data中数据配置,对应上方props
// 省市区
address: {
lazy: true, // 开启懒加载
checkStrictly: true, //可选择任意节点
lazyLoad (node, resolve) { //node为当前点击的节点,resolve为数据加载完成的回调(必须调用)
setTimeout(() => {
const nodes = [];
// 构造查询条件
let query = {
id: node.level == 0 ? 0:node.value,
level:node.level + 1
}
//查询接口
getTree(query).then(res=>{
res.data.map((item)=>{
let obj = {
value: item.value,
label: item.label,
//leaf: item.hasChildren // 节点级别,如果没有子节点就停止查询
}
nodes.push(obj);
})
//重新加载节点
resolve(nodes);
})
}, 500);
}
}
组件数据回填
- 配置 options 属性
- options 中存放数据
- this.$refs.cascader.panel.initStore() 初始化组件
<el-form-item label="省市区" prop="addrIdPath">
<el-cascader
style="width: 340px;"
v-model="form.addrIdPath"
:props="address"
:options="options"
clearable
>
</el-cascader>
</el-form-item>
后端代码
controller
@RestController
@RequestMapping("/region")
public class RegionController extends BaseController {
@Resource
private RegionService regionService;
/**
* 查询区域信息树
*/
@GetMapping("/tree")
public AjaxResult getTree(Region region) {
List<RegionTree> tree = regionService.getTree(region);
return success(tree);
}
}
service
@Service
public class RegionServiceImpl implements RegionService {
@Resource
private RegionMapper regionMapper;
/**
* 查询区域信息树
* @return List<RegionTree>
*/
@Override
public List<RegionTree> getTree(Region region) {
List<RegionTree> tree = regionMapper.getTreeList(region);
return tree;
}
}
mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.region.mapper.RegionMapper">
<resultMap type="RegionTree" id="RegionTreeResult">
<result property="value" column="id" />
<result property="label" column="name" />
</resultMap>
<select id="getTreeList" parameterType="Region" resultMap="RegionTreeResult">
select id,name from am_region
<where>
parent_id = #{id} and level = #{level}
</where>
</select>
</mapper>
GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:1 个月前 )
c345bb45
5 个月前
a07f3a59
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update transition.md
* Update table.md
* Update table.md
* Update transition.md
* Update popover.md 6 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)