zoukankan      html  css  js  c++  java
  • 用js查找treeview的节点,并自动展开搜索到的节点

    第一步,查找treeview的节点

      var key = $("#txtQuery").val();
                    var searchElement = $("a:contains('" + key + "')");
                    if (searchElement.length == 0) {
                        alert("未找到");
                        return;
                    }
    

     第二步:展开节点

    1、查找它所有的上级节点

    2、如果节点没有展开,则模拟点击事件

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
        CodeBehind="WebForm2.aspx.cs" Inherits="hyfw.WebForm2" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
        <title></title>
        <script type="text/javascript">
            function checkIsExpand(id) {
                var reg = /treet(\d+)/;
                var result = reg.exec(id);
                var index = result[1];
                var expandState = ContentPlaceHolder1_tree_Data.expandState.value.charAt(index)
                if (expandState == "e")
                    return true;
                else
                    return false;
            }
    
    
    
            $(document).ready(function () {
                $("#btnSearch").click(function () {
                    var key = $("#txtQuery").val();
                    var searchElement = $("a:contains('" + key + "')");
                    if (searchElement.length == 0) {
                        alert("未找到");
                        return;
                    }
                    var fid = searchElement[0].name;
                    var parentArray = fid.split(",");
                    for (var i = 0; i < parentArray.length; i++) {
                        var treeA = $("#" + parentArray[i]);
                        if (treeA.length > 0) {
                            var treets = treeA.siblings();
                            if (treets.length == 0) {
                                treets = treeA.parent();
                            }
                            var treet = treets[0];
                            var id = treet.id;
                            var isExpand = checkIsExpand(id);
                            if (!isExpand) {
                                id = id.replace("treet", "treen");
                                $("#" + id).get(0).click();
                            }
                        }
                    }
                })
            })
        </script>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <input type="text" id="txtQuery" /><input type="button" id="btnSearch" value="查询" />
        <asp:TreeView ID="tree" runat="server">
        </asp:TreeView>
    </asp:Content>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace hyfw
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    List<MemberGroup> groups = GetMemberGroups();
                    TreeNode rootNode = GetNode(10, "深圳市人民医院", "0,10");
                    tree.Nodes.Add(rootNode);
                    BuildTree(rootNode, groups, 10);
                    tree.CollapseAll();
                }
            }
    
            void txtSearch_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            protected void BuildTree(TreeNode node, List<MemberGroup> groups, int fId)
            {
                List<MemberGroup> childGroups = groups.Where(g => g.GroupFId == fId).ToList();
                if (childGroups.Count > 0)
                {
                    foreach (MemberGroup group in childGroups)
                    {
                        TreeNode childNode = GetNode(group.GroupId, group.GroupName, group.Path);
                        childNode.SelectAction = TreeNodeSelectAction.None;
                        node.ChildNodes.Add(childNode);
                        BuildTree(childNode, groups, group.GroupId);
                    }
                }
            }
    
            private static TreeNode GetNode(int groupId, string groupName, string path)
            {
                TreeNode childNode = new TreeNode() { Value = groupId.ToString() };
                childNode.Text = "<a id='" + groupId.ToString() + "' name='" + path + "' href=\"#\" >" + groupName + "</a>";
                return childNode;
            }
    
            public List<MemberGroup> GetMemberGroups()
            {
                List<MemberGroup> memberGroups = new List<MemberGroup>();
                memberGroups.Add(new MemberGroup() { GroupFId = 0, GroupId = 10, GroupName = "深圳市人民医院", Path = "0" });
                memberGroups.Add(new MemberGroup() { GroupFId = 10, GroupId = 1010, GroupName = "体检病人", Path = "0,10" });
                memberGroups.Add(new MemberGroup() { GroupFId = 10, GroupId = 1011, GroupName = "住院病人", Path = "0,10" });
                memberGroups.Add(new MemberGroup() { GroupFId = 10, GroupId = 1012, GroupName = "门诊病人", Path = "0,10" });
                memberGroups.Add(new MemberGroup() { GroupFId = 1010, GroupId = 101010, GroupName = "集团用户", Path = "0,10,1010" });
                memberGroups.Add(new MemberGroup() { GroupFId = 1010, GroupId = 101011, GroupName = "个人用户", Path = "0,10,1010" });
                memberGroups.Add(new MemberGroup() { GroupFId = 101010, GroupId = 10101010, GroupName = "福田中学", Path = "0,10,1010,101010" });
                memberGroups.Add(new MemberGroup() { GroupFId = 101010, GroupId = 10101011, GroupName = "南山中学", Path = "0,10,1010,101010" });
                memberGroups.Add(new MemberGroup() { GroupFId = 101010, GroupId = 10101012, GroupName = "电信", Path = "0,10,1010,101010" });
                memberGroups.Add(new MemberGroup() { GroupFId = 1011, GroupId = 101110, GroupName = "泌尿外科", Path = "0,10,1011" });
                memberGroups.Add(new MemberGroup() { GroupFId = 101110, GroupId = 10111011, GroupName = "心内科", Path = "0,10,1011,101110" });
                memberGroups.Add(new MemberGroup() { GroupFId = 101110, GroupId = 10111012, GroupName = "膀胱癌", Path = "0,10,1011,101110" });
                memberGroups.Add(new MemberGroup() { GroupFId = 101110, GroupId = 10111013, GroupName = "前列腺癌", Path = "0,10,1011,101110" });
                return memberGroups;
            }
        }
    
        public class MemberGroup
        {
            public int GroupId { get; set; }
    
            public string GroupName { get; set; }
    
            public int GroupFId { get; set; }
    
            public string Path { get; set; }
        }
    }
    
  • 相关阅读:
    爬虫入门
    读写文件操作
    列表的操作
    课后习题小练
    Python切片
    逗号的麻烦
    字符串学与练
    Turtle的学习
    FTL(FreeMarker)基础
    java反射机制基础
  • 原文地址:https://www.cnblogs.com/50614090/p/2433370.html
Copyright © 2011-2022 走看看