easyui的data-options在动态构建Html时的使用

 var count=20000;
        function AddAdv() {
           var  tmp='<div class="qddt_sc"><div class="qddt_sc_ul"><ul><li class="qddt_sc_li1"><p><input type="hidden"/><input type="hidden" id="imgUrl'+count+'"/><img src="/images/glht_imges/qddt_sc.png" id="img'+count+'"></p><p class="btn_con1 hei1"><span class="sp_401">上传</span><input type="file" value="" name="" size="0" class="input_41" id="'+count+'"/></p></li>';
           tmp+='<li class="qddt_sc_li2 ggmb"><p><span class="red">*</span>标题:&nbsp;<input type="text" class="qddt_input1"></p><p><span class="red">*</span>类别:<span class="btlbsx_sel"><input id="ct'+count+'" value="0" class="easyui-combotree" style="width: 200px;"></span></p><p><span class="red">*</span>顺序:<span class="btlbsx_sel">';
           tmp+='<input type="text" class="qddt_input1" value=""></span></p></li></ul></div><p class="bcsc"><input type="submit" value="保存" class="bc" οnclick="save(this,\'imgUrl' + count + '\')"><input type="submit" value="删除" class="sc1" οnclick="del(this,\'imgUrl' + count + '\')"></p></div>';
          
            $("#Advs").append(tmp);
            $('#ct'+count).combotree({url:"/App/TypeTree"});
            BindUpload();    
            count=Number(count)+1;
        }

这个count是用于给类别控件设置不同的id值,当点击界面的新增按钮时就会调用此方法,将新的html内容追加到界面的某个div里面,如果将data-options方法写在html内容里面的话,只有页面刚加载时候才会去调用对应的后台方法,然后成功绑定数据;而当点击新增按钮时,后台方法不会执行,所以重新写一次如下代码:

$('#ct'+count).combotree({url:"/App/TypeTree"});

这样就会去请求后台方法获取数据;

树形数据列表需要是json格式,如:

 [HttpPost]
        public JsonResult TypeTree()
        {
            var r = Context.LoadAPPTree();
            return Json(new TreeNode[] { r });
        }

 public static TreeNode CreateAPP(List<DataService.Catalog> lst)
        {
            TreeNode tn = new TreeNode { id = 0, text = "请选择" };
            tn.LoadAPPChildren(lst,1);
            return tn;
        }

 private void LoadAPPChildren(List<DataService.Catalog> lst,int level)
        {
            if (level == 1)
            {
                children = lst.Where(o => o.PId == null && (o.IsMainMenu == 1 || o.IsRootMenu == 1)).Select(o => new TreeNode { id = o.Id, text = o.Description }).ToList();
                children.ForEach(o => o.LoadAPPChildren(lst,2));
            }
            else
            {
                children = lst.Where(o => o.PId ==id&&(o.IsMainMenu == 1 || o.IsRootMenu == 1)).Select(o => new TreeNode { id = o.Id, text = o.Description }).ToList();
            }
        }

 

这个问题让我折腾了很久,终于弄出来了,所以与大家分享下!!

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页