欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

jquery zTree异步加载、模糊搜索简单实例分享_jquery

程序员文章站 2022-03-08 21:40:52
...
本文实例为大家讲解了jquery zTree树插件的基本使用方法,具体内容如下

一、节点模糊搜索功能:搜索成功后,自动高亮显示并定位、展开搜索到的节点。

二、节点异步加载:1、点击展开时加载数据;2、选中节点时加载数据。
前台代码如下:


    后台代码(后台返回Json数据):

     public void SelStudent()
     {
     set("getStudentsJsonUrl", to(GetStudentsJson));
     }
    
     public void GetStudentsJson()
     {
     List> dicList = new List>();
    
     string level = ctx.Post("level");
     string id = ctx.Post("id");
     if (strUtil.IsNullOrEmpty(id))
     {
     #region 加载班级
     //获取当前登录用户
     Sys_User user = AdminSecurityUtils.GetLoginUser(ctx);
     //获取当前用户关联的老师
     Edu_Teacher teacher = edu_TeacService.FindByUserId(user.Id);
     //获取班级集合
     List list = edu_ClaNameFlowService.GetListByTeacherId(teacher.Id);
     foreach (Edu_ClaNameFlow item in list)
     {
      Dictionary dic = new Dictionary();
      dic.Add("id", "level0" + item.Calss.Id.ToString());
      dic.Add("pId", "0");
      dic.Add("name", item.Gra.Name + item.Calss.Name);
      dic.Add("isParent", "true");
      dicList.Add(dic);
     }
     #endregion
     }
     else
     {
     if (level == "0")
     {
      //加载学生
      List list = edu_StudService.GetListByClassId(id.Replace("level0", ""));
      foreach (Edu_Student item in list)
      {
      Dictionary dic = new Dictionary();
      dic.Add("id", "level1" + item.Id.ToString());
      dic.Add("pId", id);
      dic.Add("name", item.Name);
      dic.Add("isParent", "false");
      dicList.Add(dic);
      }
     }
     }
    
     echoJson(dicList);
     }
    

    三、基于cookie实现zTree树刷新后,展开状态不变

    1、除了引用jQuery和zTree的JS外,引用cookie的JS:

    复制代码 代码如下:

    2、JS代码:

    $(function () {
     //ztree设置
     var setting = {
      data: {
       simpleData: {
        enable: true,
        idKey: "id",
        pIdKey: "pId",
        rootPId: null
       }
      },
      callback: {
       onExpand: onExpand,
       onCollapse: onCollapse
      }
     };
    
     $.ajax({
      type: "POST",
      url: "/Tech/TemplateTypeManage/GetData",
      success: function (data) {
       if (data && data.length != 0) {
        $.fn.zTree.init($("#tree"), setting, data);
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var cookie = $.cookie("z_tree" + window.location);
        if (cookie) {
         z_tree = JSON2.parse(cookie);
         for (var i = 0; i  -1) z_tree.splice(index, 1);
     }
     $.cookie("z_tree" + window.location, JSON2.stringify(z_tree))
    }
    
    

    以上就是关于树插件zTree异步加载、模糊搜索简单实例讲解,希望对大家的学习有所帮助。