<script type="text/javascript">
function areaOnSelect(obj) {
var res = '';
$.ajax({ url: '@Url.Action("GetSubTree")',
type: 'GET',
data: { parentId: obj.value },
success: function (msg) {
$(obj).nextAll().remove();
res = "<select name='Sub' onchange='areaOnSelect(this)'>";
res += "<option value=''>请选择</option>";
$.each(msg, function (i, item) {
res += "<option value='" + item["ID"] + "'>" + item["Name"] + "</option>";
});
res += "</select>";
if ($(res).find("option").size() > 1)
$(obj).after(res);
}
});
}
</script>
#region 树型结构相关
/// <summary>
/// 递归找老祖宗
/// </summary>
/// <param name="father"></param>
void GetFather(SubItem father)
{
if (father != null)
{
father.Parent = _subList.FirstOrDefault(i => i.ID == father.ParentID);
GetFather(father.Parent);
}
}
/// <summary>
/// 弟妹找子孙
/// </summary>
/// <param name="father">父对象</param>
void getSons(SubItem father)
{
if (father != null)
{
father.Sons = _subList.Where(item =>
item.ParentID.Equals(father.ID)).ToList();
father.Sons.ForEach(item =>
{
item.Parent = father;
getSons(item);
});
}
}
#endregion
/// <summary>
/// 递归得到它的所有祖宗以selectlist的形式进行拼接
/// </summary>
/// <param name="son"></param>
/// <param name="sbr"></param>
void getSelectList(SubItem son, StringBuilder sbr)
{
StringBuilder inSbr = new StringBuilder();
if (son != null)
{
if (son.ParentID == 0)
inSbr.Append("<select name='Parent' onchange = 'areaOnSelect(this)' >");
else
inSbr.Append("<select name='Sub'>");
GetCommon_CategoryByLevel(son.Level).ToList().ForEach(i =>
{
if (i.ID == son.ID)
inSbr.Append("<option value='" + i.ID + "' selected='true'>" + i.Name + "</option>");
else
inSbr.Append("<option value='" + i.ID + "'>" + i.Name + "</option>");
});
inSbr.Append("</select>");
sbr.Insert(0, inSbr);
getSelectList(son.Parent, sbr);
}
}
/// <summary>
/// 得到指定深度的列表
/// </summary>
/// <param name="level"></param>
/// <returns></returns>
public List<SubItem> GetCommon_CategoryByLevel(int level)
{
var linq = from data1 in _subList
join data2 in _subList on data1.ParentID equals data2.ID into list
select new SubItem
{
ID = data1.ID,
Level = data1.Level,
Name = data1.Name,
Parent = list.FirstOrDefault(),
ParentID = data1.ParentID,
};
return linq.Where(i => i.Level.Equals(level)).ToList();
}
public ActionResult Category(int? id)
{
ViewData["Parent"] = new SelectList(_subList.Where(i => i.ID == (id ?? 0)), "ID", "Name", id ?? 1);
SubItem current = _subList.FirstOrDefault(i => i.ID == (id ?? 1));
GetFather(current);
StringBuilder sbr = new StringBuilder();
getSelectList(current, sbr);
ViewData["edit"] = sbr.ToString();//修改时,进行绑定
return View();
}
@Html.Raw(ViewData["edit"].ToString())
/// <summary>
/// 树型分类结构
/// </summary>
public class Category
{
/// <summary>
/// 父ID
/// </summary>
public int ParentID { get; set; }
/// <summary>
/// 树ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// 树名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 深度
/// </summary>
public int Level { get; set; }
/// <summary>
/// 子孙节点
/// </summary>
public List<Category> Sons { get; set; }
/// <summary>
/// 父节点
/// </summary>
public Category Parent { get; set; }
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有