sqlserver省市区表
十年河东,十年河西,莫欺少年穷
学无止境,精益求精
参考1:https://www.cnblogs.com/zhu520/p/8244578.html
生成vol/vue树状级联方法
public WebResponseContent2 Getareatree() { // string sql = string.Format(@"WITH Sys_area(area_id,parent_id,name) //AS //(SELECT area_id,parent_id,name //FROM dbo.Sys_area //WHERE 1=1 //UNION ALL //SELECT o.area_id,o.parent_id,o.name //FROM dbo.Sys_area o INNER JOIN Sys_area oo ON o.area_id=oo.parent_id) //SELECT area_id as id ,parent_id as MyparentId,name FROM Sys_area"); // var trees = DataRepository.GetBySql<TreeDto>(sql); // return WebResponseContent.Instance.OK(ResponseType.Success.ToString(), trees); using (swapDbContext context = new swapDbContext()) { List<TreeDto> trees = new List<TreeDto>(); var ProvincData = context.SysProvinces.ToList(); var CityData = context.SysCities.ToList(); var AreaData = context.SysAreas.ToList(); foreach (var item in ProvincData) { var MyItem = new TreeDto() { value = item.Code.ToString(), label = item.Name }; var itemchild = CityData.Where(A => A.Provincecode == item.Code); foreach (var citem in itemchild) { var MyCitem = new TreeDto() { value = citem.Citycode.ToString(), label = citem.City }; MyItem.children.Add(MyCitem); var citemchild = AreaData.Where(A => A.Citycode == citem.Citycode); foreach (var Ditem in citemchild) { var MyDitem = new TreeDto() { value = Ditem.Areacode.ToString(), label = Ditem.Area }; MyCitem.children.Add(MyDitem); } } trees.Add(MyItem); } return WebResponseContent.Instance.OK(ResponseType.Success.ToString(), trees); } }
参考2:
create table Sys_area( area_id int not null primary key, parent_id int not null, name nvarchar(40) not null )
数据脚本
生成vol/vue树状级联方法:
public WebResponseContent Getareatree() { using (swapDbContext context = new swapDbContext()) { List<TreeDto> trees = new List<TreeDto>(); var areasdata = context.SysArea.ToList(); var area_1 = areasdata.Where(A => A.ParentId == 0); foreach (var item in area_1) { var MyItem = new TreeDto() { value = item.AreaId.ToString(), label = item.Name }; var itemchild = areasdata.Where(A => A.ParentId == item.AreaId); foreach (var citem in itemchild) { var MyCitem = new TreeDto() { value = citem.AreaId.ToString(), label = citem.Name }; MyItem.children.Add(MyCitem); var citemchild = areasdata.Where(A => A.ParentId == citem.AreaId); foreach (var Ditem in citemchild) { var MyDitem = new TreeDto() { value = Ditem.AreaId.ToString(), label = Ditem.Name }; MyCitem.children.Add(MyDitem); } } trees.Add(MyItem); } return WebResponseContent.Instance.OK(ResponseType.Success.ToString(), trees); } }
vol、vue 前端
this.http.get("api/Evl_Groups/Getareatree", {}, true).then(reslut => { console.log(reslut.data) if (reslut.data) { let source = reslut.data; console.log(source) //设置数据源(必须) this.editFormOptions.forEach(x => { x.forEach(item => { if (item.field == 'areaid') { //设置为级联类型(必须) item.type = "cascader"; //初始化一个空数据源; item.data = source } }) }) } })
public class TreeDto
{
public string value { get; set; }
public string label { get; set; }
public List<TreeDto> children { get; set; } = new List<TreeDto>();
}