`
jialuweb
  • 浏览: 32634 次
  • 性别: Icon_minigender_1
  • 来自: 陕西西安
社区版块
存档分类
最新评论

jquery-treeview

 
阅读更多
html代码:
<div id="divMove">
    <input id="btnAdd" type="button" value="添加" /><input id="btnDel" type="button" value="删除" /></div>
<div id="divTree">
    <ul id="red" class="treeview-red">
    </ul>
</div>


主要的js文件代码:
//--Author:Vincent

$(document).ready(function(){   
//bulid xmlHttpRequest
    var xmlHttp;
    var oClick;//which mouse up
    if(window.ActiveXObject)
    {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)
    {
        xmlHttp=new XMLHttpRequest();
    }
    var post="op=2";//bind data from database
    var url=document.location.href;
    xmlHttp.open("POST",url,true);
    xmlHttp.onreadystatechange=callback;
    xmlHttp.setRequestHeader("Content-Length",post.length);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send(post); 
       
    //add
    $("#btnAdd").click(function(event){
//        var oUL=document.getElementById("red");
//        var oLI=document.createElement("li");
//        var oInput=document.createElement("input");
//       
//        oInput.setAttribute("class","treenode");
//        oInput.setAttribute("value","New Node");
//        oInput.setAttribute("type","text");
//       
//        oLI.appendChild(oInput);
//        oUL.appendChild(oLI);
        var oNode=oClick;
        var array=new Array();
             
        for(var i=0;i<document.childNodes.length;i++)
        {
            if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
            {
                var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
                array=oo;
                oNode=oNode.parentNode.parentNode;
            }
            else
                break;
        }
        if(array.length==0)
        {
            post="op=1&sort="+oClick.value+"|NewNode1.0|NewNode1.0.0";
        }
        else if(array.length==1)
        {
            post="op=1&sort="+array[0]+"|"+oClick.value+"|NewNode1.0.0";
        }
        else if(array.length==2)
        {
            post="op=1&sort="+array[1]+"|"+array[0]+"|NewNode1.0.0";
        }
       
        //post="op=1&sort=NewNode1.0|NewNode1|NewNode1.0.0";//add new data
        xmlHttp.open("POST",url,true);
        xmlHttp.onreadystatechange=callback;
        xmlHttp.setRequestHeader("Content-Length",post.length);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlHttp.send(post);
    });
    //delete
    $("#btnDel").click(function(event){
       
        if(confirm('Are you sure to clear or delete?'))
        { 
            var oNode=oClick;
            var array=new Array();
             
            for(var i=0;i<document.childNodes.length;i++)
            {
                if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
                {
                    var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
                    array=oo;
                    oNode=oNode.parentNode.parentNode;
                }
                else
                    break;
            }
            post="op=4&sort=";
           
            for(var i=0;i<array.length;i++)
            {
                post+=array+"|";
               
                if(i==array.length-1)
                {
                    post=post.substring(0,post.length-1)+"|"+oClick.value;
                }
            }
            if(array.length==0)
            {
                alert('Can not to be deleted!');
                return;
            }
            xmlHttp.open("POST",url,true);
            xmlHttp.onreadystatechange=callback;
            xmlHttp.setRequestHeader("Content-Length",post.length);
            xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlHttp.send(post);
        }
    });
    //web visit callback
    function callback()
    {
        if(xmlHttp.readystate==4)
        {
            if(xmlHttp.status==200)
            {
                display();
            }
            else
            {
                alert("Server Back Status:"+xmlHttp.statusText);
            }
        }
        else//status is not success
        {
            document.getElementById("red").innerHTML="Data Loading";
        }
    }
    function display()
    {
        var index=xmlHttp.responseText.indexOf("<!DOCTYPE");
        var html=xmlHttp.responseText.substring(0,index);
        document.getElementById("red").innerHTML=html;
       
        reset();
    }
       
    function reset()
    {   
        $("#red").treeview({
            animated: "fast",
            collapsed: true,
            unique: true,
            persist: "cookie",
            toggle: function() {
                window.console && console.log("%o was toggled", this);
            }
        });
   
        //double click textbox
        $("#divTree input").dblclick(function(event){
            $(this).addClass("focus");
           
            var oDiv=document.getElementById("divMove");
            oDiv.style.display="none";
        });
        //textbox mouseout
        $("#divTree input").mouseout(function(event){
            $(this).removeClass("focus");
        });
       
        //textbox enter
        $("#divTree input").keydown(function(event){
            if(event.keyCode==13)
            {
                $(this).removeClass("focus");
           
                var oNode=this;
                var array=new Array();
                 
                for(var i=0;i<document.childNodes.length;i++)
                {
                    if(oNode.parentNode.parentNode.parentNode.tagName=="LI")
                    {
                        var oo=oNode.parentNode.parentNode.parentNode.childNodes[1].value;
                        array=oo;
                        oNode=oNode.parentNode.parentNode;
                    }
                    else
                        break;
                }
                post="op=3&sort=";
           
                for(var i=0;i<array.length;i++)
                {
                    post+=array+"|";
               
                    if(i==array.length-1)
                    {
                        post=post.substring(0,post.length-1)+"|"+this.value+"|"+this.defaultValue;
                    }
                }
                if(array.length==0) post+=this.value+"|"+this.defaultValue;
                xmlHttp.open("POST",url,true);
                xmlHttp.onreadystatechange=callback;
                xmlHttp.setRequestHeader("Content-Length",post.length);
                xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                xmlHttp.send(post);
            }
        });
   
        $(".treenode").mouseup(function(event){
       
            var oDiv=document.getElementById("divMove");
       
            oDiv.style.left=event.clientX;
            oDiv.style.top=event.clientY;
            oDiv.style.display="block";
       
            oClick=$(this).get(0);
            //alert(event.clientY+" "+event.clientX);
       
        });
        $("#divTree").mouseover(function(event){
            var oDiv=document.getElementById("divMove");
            oDiv.style.display="none";
        });
    }
});

后台代码:
public partial class Test: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    int op = Int32.Parse(Request.Form["op"]);

                    switch (op)
                    {
                        case 1:
                            AddSort();
                            break;
                        case 2:
                            GetSort();
                            break;
                        case 3:
                            UpSort();
                            break;
                        case 4:
                            DelSort();
                            break;
                    }
                }
                catch (Exception excpt){ }
            }
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        private void DelSort()
        {
            string line = Request.Form["sort"].ToString();
            string[] sorts = line.Split('|').ToArray();

            switch (sorts.Length)
            {
                case 2:
                    DelType(sorts);
                    break;
                case 3:
                    DelContent(sorts);
                    break;
            }
            GetSort();       
        }

        /// <summary>
        /// 删除具体内容
        /// </summary>
        /// <param name="sorts"></param>
        private void DelContent(string[] sorts)
        {
            string type = sorts[0];
            string sort = sorts[1];
            string content = sorts[2];

            MerchandiseSort ms = new MerchandiseSort();
            MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);

            try
            {
                ms.Delete(msInfo);
            }
            catch { }
        }

        /// <summary>
        /// 删除分类类型
        /// </summary>
        /// <param name="sorts"></param>
        private void DelType(string[] sorts)
        {
            string sort = sorts[0];
            string type = sorts[1];
            MerchandiseSort ms = new MerchandiseSort();

            try
            {
                ms.Delete(sort, type);
            }
            catch { }
        }

        /// <summary>
        /// 更新分类
        /// </summary>
        private void UpSort()
        {
            string line = Request.Form["sort"].ToString();
            string[] sorts = line.Split('|').ToArray();

            switch (sorts.Length)
            {
                case 0:
                    return;
                    break;
                case 2:
                    UpCCJOY(sorts);
                    break;
                case 3:
                    UpType(sorts);
                    break;
                case 4:
                    UpContent(sorts);
                    break;
            }
            GetSort();       
        }

        /// <summary>
        /// 更新分类内容
        /// </summary>
        /// <param name="sorts"></param>
        private void UpContent(string[] sorts)
        {
            string type = sorts[0];
            string sort = sorts[1];
            string content = sorts[2];
            string defaultValue = sorts[3];

            MerchandiseSort ms = new MerchandiseSort();
            MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);

            try
            {
                ms.Update(msInfo, defaultValue);
            }
            catch { }
        }

        /// <summary>
        /// 更新分类类型
        /// </summary>
        /// <param name="sorts"></param>
        private void UpType(string[] sorts)
        {
            string sort = sorts[0];
            string type = sorts[1];
            string defaultValue = sorts[2];
            MerchandiseSort ms = new MerchandiseSort();

            try
            {
                ms.Update(sort, type, defaultValue);
            }
            catch { }
        }

        /// <summary>
        /// 更新ccjoy分类
        /// </summary>
        /// <param name="sorts"></param>
        private void UpCCJOY(string[] sorts)
        {
            string sort = sorts[0];
            string defaultValue = sorts[1];
            MerchandiseSort ms = new MerchandiseSort();

            try
            {
                ms.Update(sort, defaultValue);
            }
            catch { }
        }

        /// <summary>
        /// 获取所有分类
        /// </summary>
        private void GetSort()
        {
            MerchandiseSort ms = new MerchandiseSort();
            IList<MerchandiseSortInfo> msInfoes = new List<MerchandiseSortInfo>();

            try
            {
                msInfoes = ms.GetMerchandiseSort();
            }
            catch (Exception excpt){ }

            string rep = GetXml(msInfoes);
            Response.Write(rep);
        }

        /// <summary>
        /// 拼取response
        /// </summary>
        /// <param name="msInfoes"></param>
        /// <returns></returns>
        private string GetXml(IList<MerchandiseSortInfo> msInfoes)
        {
            StringBuilder sb = new StringBuilder();

            foreach (MerchandiseSortInfo msInfo in msInfoes)
            {
                int indexSort = sb.ToString().IndexOf(msInfo.CcjoySort);

                if (indexSort > -1)
                {
                    int indexType = sb.ToString().IndexOf(msInfo.Type, indexSort);
                    int indexUL;
                    int indexNextUL;
                    string tmp = string.Empty;
                    string tmpNew = string.Empty;

                    if (indexType > -1)
                    {
                        indexUL = sb.ToString().IndexOf("</li></ul></li></ul>", indexSort);
                        //indexUL = sb.ToString().IndexOf("</ul>", indexUL + 5);

                        if (indexType > indexUL) indexType = -1;
                    }

                    if (indexType > -1)
                    {
                        indexUL = sb.ToString().IndexOf("<ul>", indexType);
                        indexNextUL = sb.ToString().IndexOf("</ul>", indexUL);
                        tmp = sb.ToString().Substring(indexSort, indexNextUL - indexSort);
                        tmpNew = tmp + string.Format("<li><input class='treenode' value='{0}' type='text' /></li>", msInfo.Content);
                        sb = sb.Replace(tmp, tmpNew);
                    }
                    else
                    {
                        indexUL = sb.ToString().IndexOf("<ul>", indexSort);
                        indexNextUL = sb.ToString().IndexOf("</li></ul></li></ul>", indexUL);
                        //indexNextUL = sb.ToString().IndexOf("</ul>", indexNextUL + 5);
                        tmp = sb.ToString().Substring(indexSort, indexNextUL - indexSort + 15);
                        tmpNew = tmp + string.Format("<li><input class='treenode' value='{0}' type='text' /><ul><li><input class='treenode' value='{1}' type='text' /></li></ul></li>", msInfo.Type, msInfo.Content);
                        sb = sb.Replace(tmp, tmpNew);
                    }
                }
                else
                {
                    sb.Append(string.Format("<li><input class='treenode' value='{0}' type='text' /><ul><li><input class='treenode' value='{1}' type='text' /><ul><li><input class='treenode' value='{2}' type='text' /></li></ul></li></ul></li>", msInfo.CcjoySort, msInfo.Type, msInfo.Content));
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        private void AddSort()
        {
            string line = Request.Form["sort"].ToString();
            string[] sorts = line.Split('|').ToArray();

            if(sorts.Length<3) return;
            string content=sorts[2];
            string type=sorts[1];
            string sort=sorts[0];

            MerchandiseSortInfo msInfo = new MerchandiseSortInfo(sort, type, content);
            MerchandiseSort ms = new MerchandiseSort();

            try
            {
                ms.Add(msInfo);
            }
            catch { }

            GetSort();
        }
    }
分享到:
评论

相关推荐

    dtree、xtree和jquery-treeview构建树型菜单

    在本主题中,我们将深入探讨“dtree”、“xtree”以及“jquery-treeview”这三个JavaScript插件,它们都是用来创建这样的菜单的工具。下面我们将逐一介绍这三个插件,并讨论它们的特点和使用方法。 首先,`dtree`是...

    s2sh+freemarker+jquery+jquery-treeview 无限级树形菜单.rar

    本项目“s2sh+freemarker+jquery+jquery-treeview 无限级树形菜单”就是这样一个解决方案,它整合了多种技术来实现这一功能。 首先,我们来看“s2sh”。这是Struts2、Spring和Hibernate三者的简称,它们分别是MVC...

    jqtreetable jquery-treeview

    标题 "jqtreetable" 和 "jquery-treeview" 提及的是两种用于构建可交互树形结构的JavaScript库,常用于网站界面的导航菜单、目录展示等场景。它们都是基于jQuery库进行开发的,使得在网页上创建动态和可操作的树形...

    jQuery-Treeview.js v1.4 官方版.rar

    **jQuery Treeview.js 插件详解** jQuery Treeview.js 是一款功能强大的JavaScript库,用于将HTML列表转换为可交互的树形结构。这个插件基于流行的jQuery框架,为用户提供了一种简便的方式来展示层次化的数据,如...

    jquery-treeview实例

    《jQuery Treeview 实例详解与应用》 jQuery Treeview 是一个流行且实用的jQuery插件,主要用于将HTML列表转换为可交互的树形结构,它提供了丰富的功能和自定义选项,使得在网页中构建和展示层次化数据变得简单易行...

    jquery-treeview改进.zip

    《jQuery Treeview 插件深度解析与应用实践》 jQuery Treeview 是一款由 Xuanye 开发的高效树形菜单插件,专为 jQuery 1.3+ 版本设计,适用于构建可交互、层次分明的目录结构或数据展示。这款插件以其优秀的性能和...

    jquery-3.1.1.js和jquery-3.1.1.min.js

    本篇文章将深入探讨jQuery 3.1.1版本的两个核心文件——`jquery-3.1.1.js`和`jquery-3.1.1.min.js`,以及它们在Bootstrap Treeview插件中的应用。 首先,我们来看`jquery-3.1.1.js`,这是未经压缩和未优化的源代码...

    jQuery-TreeView

    **jQuery TreeView 插件详解** jQuery TreeView 是一个流行的前端JavaScript库,它允许开发者将HTML列表转换为可交互的树形视图。这个插件是基于jQuery框架构建的,因此,它能够轻松地与其他jQuery组件集成,为网站...

    jquery插件库(jquery.treeview插件库)

    **jQuery Treeview 插件库详解** jQuery Treeview 是一个流行的JavaScript库,它为HTML列表或表格提供了可交互的树状视图。这个插件库主要用于数据组织和展示,尤其适用于需要层次结构显示的信息,如目录、文件系统...

    jquery TreeView源码实例

    在本文中,我们将深入探讨如何使用jQuery库来创建一个功能丰富的TreeView组件,即树状结构的菜单。jQuery TreeView是一个流行的插件,它允许开发者轻松地将数据组织成层次结构,通常用于展示文件系统、目录结构或者...

    bootstrap-treeview压缩包文件

    这个压缩包文件jonmiles-bootstrap-treeview-542f57e包含了实现该功能所需的两个核心文件:`bootstrap-treeview.css`和`bootstrap-treeview.js`。 `bootstrap-treeview.css`是CSS样式表文件,负责定义Bootstrap ...

    动态树的管理程序(基于jQuery Treeview实现)

    在实现的时候也会有很多的方法,例如可以用extjs,jquery treeview等一些js开发包。 但是在开发的时候我种想找个例子来参考下,发现有很多这样的例子,包括extjs和jquery treeview的官方也有,但结合java后台实现的...

    bootstrap-treeview史上最强例子

    强烈建议初次使用bootstrap的人参考,例子很全...bootstrap树控件使用bootstrap-treeview.js实现树前边多选框checkbox 并实现, 0、获取选中的项 1、点击父级的多选框,自己默认选中 2、全选 3、反选 4、展开、 5、折叠

    Jquery TreeView 树形插件

    **jQuery TreeView 插件详解** 在Web开发中,为了展示层次结构的数据,树形视图是一种非常实用的布局方式。jQuery TreeView插件就是这样一个轻量级且强大的工具,它能够将HTML列表转化为交互式的树形结构,使得用户...

Global site tag (gtag.js) - Google Analytics