首页 > 其他分享 >Ztree实现搜索框自动检索功能

Ztree实现搜索框自动检索功能

时间:2022-12-16 09:56:09浏览次数:46  
标签:自动检索 string btmid ztree Ztree 搜索 result var children

ztree自定搜索框添加搜索功能,注:菜单内容一次性加载,后端数据使用父子嵌套的json数据

 

 前段代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <link href="plugins/layui-v2.5.6/css/layui.css" rel="stylesheet" />
    <link href="plugins/ztree/css/demo.css" rel="stylesheet" />
    <link href="plugins/ztree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
    <!--<link href="plugins/ztree/css/metroStyle/metroStyle.css" rel="stylesheet" />-->
    <script src="plugins/XBBase.js"></script>
    <script src="plugins/ztree/js/jquery-1.4.4.min.js"></script>
    <script src="plugins/ztree/js/jquery.ztree.core.js"></script>
    <script src="plugins/ztree/js/jquery.ztree.excheck.js"></script>
    <script src="plugins/layui-v2.5.6/layui.js"></script>
  /*父节点折叠关闭图标样式*/
    <style type="text/css">
        .ztree li span.button.pIcon01_ico_open {
            margin-right: 2px;
            background: url(plugins/ztree/css/zTreeStyle/img/diy/1_open.png) no-repeat scroll 0 0 transparent;
            vertical-align: top;
            *vertical-align: middle
        }

        .ztree li span.button.pIcon01_ico_close {
            margin-right: 2px;
            background: url(plugins/ztree/css/zTreeStyle/img/diy/1_open.png) no-repeat scroll 0 0 transparent;
            vertical-align: top;
            *vertical-align: middle
        }

        .ztree li span.button.pIcon02_ico_open {
            margin-right: 2px;
            background: url(plugins/ztree/css/zTreeStyle/img/diy/1_close.png) no-repeat scroll 0 0 transparent;
            vertical-align: top;
            *vertical-align: middle
        }

        .ztree li span.button.pIcon02_ico_close {
            margin-right: 2px;
            background: url(plugins/ztree/css/zTreeStyle/img/diy/1_close.png) no-repeat scroll 0 0 transparent;
            vertical-align: top;
            *vertical-align: middle
        }
    </style>
</head>
<body>
    <div class="layui-container">
        <form class="layui-form" style="padding: 5px;" action="">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input id="key" type="text" name="title" required lay-verify="required" placeholder="请输入搜索内容" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn layui-btn-sm" id="btnSearch">刷新</button>
                </div>
            </div>
        </form>
        <ul id="treeDemo" class="ztree"></ul>
    </div>
    <script>

        var tree,
            util,
            btmid,
            xbdep,
            jdmc,
            key,
            zNodesMap,
            zNodes,
            curNode,
            setting = {
                view: {
                    selectedMulti: true
                },
                check: {
                    enable: true
                },
                data: {
                    key: {
                        checked: "checked",
                        children: "children",
                        name: "title"
                    }
                },
                callback: {
                    //beforeCheck: beforeCheck,
                    onCheck: onCheck
                }
            };;
        layui.use(['tree', 'util'],
            function () {
                tree = layui.tree, util = layui.util;

                btmid = getQueryString(window.location.href, "btmid");
                xbdep = getQueryString(window.location.href, "xbdep");
                jdmc = getQueryString(window.location.href, "jdmc");

                loadPage(btmid);
          //文本框绑定搜索事件
                key = $("#key");
                key.bind("propertychange", searchNode)
                    .bind("input", searchNode);
            });
     //重载数据
        $("#btnSearch").click(function () {
            loadPage(btmid);
        });
    //获取点击节点数据自动保存
        function onCheck(e, treeId, treeNode) {
            console.log(treeNode);
            var roleids = [];
            if (treeNode.children.length > 0) {
                roleids = getChildren(treeNode.children, treeNode.checked);
            } else {
                roleids.push({ roleid: treeNode.id, ischeck: treeNode.checked });
            }
            console.log(roleids);
            $.post(url,
                { btmid: btmid, xbdep: decodeURI(xbdep), jdmc: decodeURI(jdmc), roleids: roleids },
                function (result) {
                    console.log(result);

                });
        }
    //递归获取最后一级叶子节点
        function getChildren(list, ischeck, returnValue = []) {
            for (let i in list) {
                if (Object.prototype.hasOwnProperty.call(list, i)) {
                    var item = list[i];
                    if (item.children.length > 0) {
                        returnValue.push(...getChildren(item.children, ischeck, []));
                    } else {
                        returnValue.push({ roleid: item.id, ischeck: ischeck });
                    }
                }
            }
            return returnValue;
        }
        function loadPage(btmid) {
            $.ajax({
                url: url,
                type: 'get',
                dataType: 'json',
                success: function (result) {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    if (result.code === 1) {
              //初始化ztree
                        $.fn.zTree.init($("#treeDemo"), setting, result.data);
                        zNodes = result.data;
                    }
                }
            });
        }
    //搜索方法
        function searchNode(e) {
            var value = $.trim(key.get(0).value);
            var result = getSearchChildren(zNodes, value);
            //console.log(result);
            // 每次查询后都重新初始化 zTree
            $.fn.zTree.init($("#treeDemo"), setting, result);
        }
      
//递归查询
function getSearchChildren(list, value) {
  var filterList = [];
  list.forEach((ele) => {
    if (ele.children.length > 0) {
      var curele = JSON.parse(JSON.stringify(ele));
      curele.children = [];
      var curResult = getSearchChildren(ele.children, value);

      if (curResult.length) {
        curele.children = curResult;
        //搜索后节点默认打开         curele.open = true;         filterList.push(curele);       }       if (!curResult.length && curele.title.indexOf(value) > -1) {
        //节点默认打开         curele.open = true;         filterList.push(curele);       }     }     else {       if (ele.title.indexOf(value) > -1) {         ele.open = true;         filterList.push(ele);       }     }   });   return filterList; } </script> </body> </html>

后端代码:

public ActionResult GetTreeList(string btmid, string xbdep, string jdmc)
        {
            var model = workFlow_Extend.GetListXbInfo(btmid, xbdep);
            string valstr = string.Empty;
            if (model != null)
            {
                var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(model.XBGCPZ);
                dict.TryGetValue(jdmc, out valstr);
            }
      //注:数据量不多情况,把所有数据读取到集合中,减少每次子节点查询对数据库的访问
            var allList = workFlow_Extend.GetRoleByPID("");
            var result = allList.Where(s => s.PARENTROLEID is null);
            List<TreeViewModel> list = new List<TreeViewModel>();
            foreach (var item in result)
            {
                bool ischeck = valstr.Contains(item.ROLEID);
                var pTree = new TreeViewModel
                {
                    id = item.ROLEID,
                    title = item.ROLENAME,
                    Checked = ischeck,
                    iconSkin = "pIcon01"
                };

                pTree.children = CreateChildTree(item.ROLEID, valstr, allList);
                list.Add(pTree);
            }
            return JsonNew(new { code = 1, data = list });
        }
        //添加子分类
        private List<TreeViewModel> CreateChildTree(string pid, string valstr, List<SFS_ROLEINFO> allList)
        {
            List<TreeViewModel> nodeList = new List<TreeViewModel>();

            var children = allList.Where(s => s.PARENTROLEID == pid); //workFlow_Extend.GetRoleByPID(pid);
            foreach (var citem in children)
            {
                bool ischeck = valstr.Contains(citem.ROLEID);
                var cTree = new TreeViewModel
                {
                    id = citem.ROLEID,
                    title = citem.ROLENAME,
                    Checked = ischeck,
                    iconSkin = "pIcon02"
                };
                cTree.children = CreateChildTree(citem.ROLEID, valstr, allList);
                nodeList.Add(cTree);
            }
            return nodeList;
        }

public class TreeViewModel
    {
        public string title { get; set; }
        public string id { get; set; }

        public List<TreeViewModel> children { get; set; }

        [JsonProperty("checked")]
        public bool Checked { get; set; }

        public string iconSkin { get; set; }

    }

 

标签:自动检索,string,btmid,ztree,Ztree,搜索,result,var,children
From: https://www.cnblogs.com/MrLiu90/p/16986529.html

相关文章

  • 【Linux】Linux命令大全——解压、目录、文件、搜索等
    文件目录​​1、常用命令​​​​常用Linux命令的基本使用​​​​2、查阅命令帮助信息​​​​2.1help帮助信息​​​​2.2man手册​​​​3、目录常用命令​​​​3.1......
  • 这就是搜索引擎(8) 网页去重
    1.背景1.1重复网页的类型在互联网中,近似重复网页(NearDuplicateWebPage)的数量占网页总数的比例高达29%,完全相同的页面占全部页面的22%,其中根据内容和布局又可以分......
  • mysql 实现加密搜索
    前言:我们经常遇到由于安全考虑,数据库中一些用户信息比如手机号、身份证号等信息加密保存,但又有一些需要查询匹配的操作,比如手机号模糊匹配的场景,此时就无法直接使用模糊查询......
  • 在线直播系统源码,实现搜索后界面显示商品列表效果
    在线直播系统源码,实现搜索后界面显示商品列表效果activity_main.xml <?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.c......
  • 拼多多 2020校招 多多的电子字典(字典树前缀搜索,DP)
    多多鸡打算造一本自己的电子字典,里面的所有单词都只由a和b组成。每个单词的组成里a的数量不能超过N个且b的数量不能超过M个。多多鸡的幸运数字是K,它打算把所有满足条件的......
  • 剑指offer 二叉搜索树的第k个结点(二叉搜索树的中序遍历)
    题目描述给定一棵二叉搜索树,请找出其中的第k小的结点。例如,(5,3,7,2,4,6,8)  中,按结点数值大小顺序第三小结点的值为4。解题思路:我们知道二叉搜索树的中序遍历就是排序好的数列......
  • 永不失效的磁力搜索神器
    使用磁力网站搜索资源的人都有一个苦恼的地方,就是磁力网站是不是就失效了,或者是被禁止了给大家推荐一个我一直在用的,包含了很多磁力网站,最大的特点就是能清晰地知道那些网......
  • 新论文推荐:Auto-Keras:自动搜索深度学习模型的网络架构和超参数
    Auto-Keras是一个开源的自动机器学习库,由美国德州农工大学(TexasA&MUniversity)助理教授胡侠和他的两名博士生:金海峰、QingquanSong提出。Auto-Keras的终极目标是允许所......
  • ES搜索- term与match区别&bool查询
    term属于精确匹配,只能查单个词,tems可以匹配多个词(满足其中之一词的都会被搜索出来),多个词如果要同时匹配使用bool的must(must中带多个term);match进行搜索的时候,会先进行分......
  • 81. 搜索旋转排序数组 II
    已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。在传递给函数之前,nums 在预先未知的某个下标 k(0<=k<nums.length)上进行了 旋转 ,使数组变为......