树形下拉框 动态加载 数据

时间:2022-11-21 19:34:49

在网上找了好久的列子, 终于找到一个,

前台页面

 1 <script type="text/javascript">
 2         $(function () {
 3 $("#ProductId").combotree({
 4                 required: true
 5             }).combotree("tree").tree({
 6                 url: '/SeekSupplyInfo/ProductList',
 7                 checkbox: false,
 8                 onBeforeExpand: function (node, param) {
 9                     $(this).tree('options').url = "/SeekSupplyInfo/ProductList?id=" + node.id;
10                 }
11             });
12 });
13 </script>
14 
15 <input id="ProductId" name="ProductId">

后台代码:

这里用的是自己写一个接口,是获得父节点列表

   public ActionResult ProductList(string id)
        {
            var data = BusinessLogicFactory.GetLogicInstance<IProductLogic>().GetChildrenNode(id);
            return Json(data);
        }

数据层:

 public IList<Product> GetChildrenNode(string id)
        {
            //当是根节点的时候
            if (id == "root" || string.IsNullOrEmpty(id))
            {
                return DbQuery.Where(d => string.IsNullOrEmpty(d.ParentId)).OrderBy(d => d.Sort).ToList();
            }
            else
            {
                return DbQuery.Where(d => d.ParentId == id).OrderBy(d => d.Sort).ToList();
            }
        }

逻辑层:

 1  public List<ProductTreeModel> GetChildrenNode(string id)
 2         {
 3             List<ProductTreeModel> result = new List<ProductTreeModel>();
 4             var children = repository.GetChildrenNode(id);
 5             foreach (var item in children)
 6             {
 7                 ProductTreeModel model = new ProductTreeModel
 8                 {
 9                     id = item.Id,
10                     text = item.Name
11                 };
12                 if (HaveChildren(item.Id))
13                 {
14                     model.state = "closed";
15                 }
16                 result.Add(model);
17             }
18             return result;
19         }