`

一个树状视图的JavaScript

阅读更多
来自互联网

//========================================
//Envrionment to hold Listeners
//========================================
tv_listeners = new Array() ;
function listener( type , handler )   {
this.type = type ;
this.handler = handler ;
this.id = tv_listeners.length ;
tv_listeners[ tv_listeners.length ] = this ;
}

function addListener( type , handler )  {
    new listener( type , handler ) ; 
}
//=== END =====

//=========================================
// Hold the top item
//=========================================
tv_topnodeitem = null ;
//===== END =======

//=========================================
//Hold nodeitems , and supply a nodeitem Register
//=========================================
nodeitems = new Array() ;
function nodeitemRegister( obj )   {
    nodeitems[ nodeitems.length ] = obj ;
return nodeitems.length - 1 ;
}
//=== END =======

//=================================
//Custom a stack
//Class         : stack
//metheds    :  get()
//                    put( obj )
//=================================
function stack() {
    this.value = new Array() ;
this.cursor = 0 ;
}

function stack_get()  {
     this.cursor = this.cursor - 1 ;
  return this.value[ this.cursor ] ;
}

function stack_put( obj )   {
      this.value[ this.cursor ] = obj ;
      this.cursor = this.cursor + 1 ;
}

stack.prototype.get = stack_get ;
stack.prototype.put = stack_put ;
//=======END ==========

//=========================================
// Define a public stack
//=========================================
userstack = new stack() ;
//====== END ===========

//=========================================
//Image List
//=========================================
treeview_box_0_none = "images/4_clos.gif"  ;
treeview_box_0_line = "images/4_none.gif" ;
treeview_box_2_open = "images/2_open.gif" ;
treeview_box_2_none = "images/2_none.gif" ;
treeview_box_2_close = "images/2_clos.gif" ;
treeview_box_1_open = "images/3_open.gif" ;
treeview_box_1_none = "images/3_none.gif" ;
treeview_box_1_close = "images/3_clos.gif" ;

//===============================================
//Class : nodeitem
//status------------------------1:two-direction       0:nobox       0: disactivite
//                              2:three-0direction     1:close-box   1: activite
//                                                    2:open-box                   
//===============================================
function nodeitem( parentkey , key , lable , img )  {
    this.lable = lable ;
this.key = key ;
    this.parent = findNode( parentkey ) ;
if( this.parent != null )  {
     aa = this.parent.status ;
     if( aa.substring( 1 , 2 ) == "0" )
         this.parent.status = aa.substring( 0 , 1 ) + "1" + aa.substring( 2 , 3 ) ;
     if( this.parent.maxsubitem != null )
         this.parent.maxsubitem.status = "2" + this.parent.maxsubitem.status.substring( 1 , 3 ) ;
        this.parent.subitems[ this.parent.subitems.length ] = this ;
        this.parent.maxsubitem = this ;
    }
    else  {
     if( tv_topnodeitem != null )   {
       alert( "不能有两个顶项!" ) ;
    return ;
        }
        tv_topnodeitem = this ;
    }
  
this.img = img ;
this.tag = null ;
    this.status = "100" ;
this.subitems = new Array() ;
this.maxsubitem = null ;
    this.id = nodeitemRegister( this ) ;

//**********************
this.questionId = 0;
this.description = "";
//this.url = null;
//**********************

//added by msb for the sort and move up/down
/*if ( this == tv_topnodeitem )
{
  this.nodeIndex = 0;
} else {
  this.nodeIndex = this.parent.subitems.length;
}*/
//end added

}

//added by msb for the sort and move up/down
function nodeitem_moveUp() {
if (this == tv_topnodeitem) return; //topitem

ssubitems = this.parent.subitems;
for ( i=0; i<ssubitems.length; i++ ) {
  if( ssubitems[i] == this ) {
   break;
  }
}
if (i==0) return;
ssubitems[i] = ssubitems[i-1];
ssubitems[i-1] = this;
if (i==ssubitems.length-1) {
  ssubitems[i-1].status = "2" + ssubitems[i-1].status.substring(1, 3);
  ssubitems[i].status = "1" + ssubitems[i].status.substring(1, 3);
}
/*
itemTemp = this;
ssubitems[this.nodeIndex-1] */
/* for ( i=0; i<ssubitems.length; i++ ) {
  if( ssubitems[i] != null && ssubitems[i].nodeIndex == (this.nodeIndex-1) )
   previousitem = ssubitems[i]
}
previousitem.nodeIndex = this.nodeIndex;
this.nodeIndex = this.nodeIndex -1;
swap(this,previousitem);
*/
//label_on_click(this.id);
this.parent.refresh();

lable_on_click(this.id);
}//moveUp()

function nodeitem_moveDown() {
if (this == tv_topnodeitem) return; //topitem

ssubitems = this.parent.subitems;
for ( i=0; i<ssubitems.length; i++ ) {
  if( ssubitems[i] == this ) {
   break;
  }
}
if (i==ssubitems.length-1) return;
ssubitems[i] = ssubitems[i+1];
ssubitems[i+1] = this;
if (i==ssubitems.length-2) {
  ssubitems[i+1].status = "1" + ssubitems[i+1].status.substring(1, 3);
  ssubitems[i].status = "2" + ssubitems[i].status.substring(1, 3);
}

this.parent.refresh();
 
lable_on_click(this.id);
}//moveDown()

/*function swap (item1, item2) {
nodeitems[item1.id] = item2;
nodeitems[item2.id] = item1;
idTemp = item1.id;
item1.id = item2.id;
item2.id = idTemp;
}*/

//end added

function nodeitem_setTag( obj ) {
    this.tag = obj ;
}

function nodeitem_getTag() {
    return this.tag ;
}

function nodeitem_show()  {
str = "<span id = 'preface" + this.id + "'><table border='0' cellspacing='0' cellpadding='0'><tr><td>" ;
    str_f = "" ;
    for( j = this.parent ; j != null ; j = j.parent )  {
     if( j.status.substring( 0 , 1 ) == 1 )
      str_f = "<img src = '" + treeview_box_0_none + "' align='absmiddle'>" + str_f ;
        else
      str_f = "<img src = '" + treeview_box_0_line + "' align='absmiddle'>" + str_f ;
    }
    str = str + str_f ;
    str += "<img id = 'box" + this.id + "' nodeid = '" + this.id + "' src = '" ;
    switch( this.status.substring( 0 , 2 ) )   {
        case "10" : str += treeview_box_1_none ; break ;
        case "11" : str += treeview_box_1_close ; break ;
        case "12" : str += treeview_box_1_open ; break ;
        case "20" : str += treeview_box_2_none ; break ;
        case "21" : str += treeview_box_2_close ; break ;
        case "22" : str += treeview_box_2_open ; break ;
    }
    str += "' align='absmiddle' onclick='box_on_click(this)'>" ;
    if( this.img == "" )
     str += this.img ;
    else
     str += "<img src = '" + this.img + "' align='absmiddle' width='16' height='16'>" ;
    str += "</td><td><table border='0' cellspacing='1' cellpadding='1' style='font-size:9pt; color:#333333' id='lablePanel" + this.id + "'><tr><td ondblclick = 'lable_on_dblclick(" + this.id + ")' onclick='lable_on_click(" + this.id + ")' style='cursor:hand' id='f_lablePanel" + this.id + "' nowrap>" + this.lable + "</td></tr></table></td></tr></table>" ; 
str += "</span><span id = 'tv_panel_" + this.id + "' style='display:" ;
if( this.status.substring( 1 , 2 ) == '2' )
    str += "" ;
else
    str += "none" ;
 
    str += "'></span>" ;

if(this.parent == null ) 
      for(var i in document.all){
         //if (document.all[i].tagName == "BODY")//tagName (标签)
           if(document.all[i].id=="show")//找到所有id再找出id为show的那个
          {
        document.all[i].insertAdjacentHTML( "AfterBegin" , str ) ;
        //在后面直接插入html在开始标签之后
              break
          }
       }
else  
document.all( "tv_panel_" + this.parent.id ).insertAdjacentHTML( "BeforeEnd" , str ) ;
    for( m = 0 ; m < this.subitems.length ; m ++ )
    if( this.subitems[ m ] != null )  {
     userstack.put( m ) ;
     this.subitems[ m ].show() ;
     m = userstack.get() ;
    }
}

function nodeitem_refresh()  {
str = "<table border='0' cellspacing='0' cellpadding='0'><tr><td>" ;
    str_f = "" ;
    for( j = this.parent ; j != null ; j = j.parent )  {
     if( j.status.substring( 0 , 1 ) == 1 )
     str_f = "<img src = '" + treeview_box_0_none + "' align='absmiddle'>" + str_f ;
        else
     str_f = "<img src = '" + treeview_box_0_line + "' align='absmiddle'>" + str_f ;
    }
str = str + str_f ;
    str += "<img id = 'box" + this.id + "' nodeid = '" + this.id + "' src = '" ;
    switch( this.status.substring( 0 , 2 ) )   {
         case "10" : str += treeview_box_1_none ; break ;
         case "11" : str += treeview_box_1_close ; break ;
         case "12" : str += treeview_box_1_open ; break ;
         case "20" : str += treeview_box_2_none ; break ;
         case "21" : str += treeview_box_2_close ; break ;
         case "22" : str += treeview_box_2_open ; break ;
    }
    str += "' align='absmiddle' onclick='box_on_click(this)'>" ;
    if( this.img == "" )
     str += this.img ;
    else
     str += "<img src = '" + this.img + "' align='absmiddle' width='16' height='16'>" ;
    str += "</td><td><table border='0' cellspacing='1' cellpadding='1' style='font-size:9pt; color:#333333' id='lablePanel" + this.id + "'><tr><td ondblclick = 'lable_on_dblclick(" + this.id + ")' onclick='lable_on_click(" + this.id + ")' style='cursor:hand' id='f_lablePanel" + this.id + "' nowrap>" + this.lable + "</td></tr></table></td></tr></table>" ; 
document.all( "preface" + this.id ).innerHTML = str ;
    document.all( "tv_panel_" + this.id ).innerHTML = "" ;
for( m = 0 ; m < this.subitems.length ; m ++ )
    if( this.subitems[ m ] != null )  {
     userstack.put( m ) ;
     this.subitems[ m ].show() ;
     m = userstack.get() ;
       }
}
function nodeitem_remove()  {
    pparent = this.parent ;
if( pparent == null )   {
        removenodeitem( this.id ) ;
        for(var i in document.all){
           if (document.all[i].tagName == "BODY")
             {
      document.all[i].innerHTML = "" ;
               break
             }
          }
  return ;
}
lastsubitem = null ;
for( i = 0 ; i < pparent.subitems.length ; i ++ )
    if( pparent.subitems[ i ] != null ) 
     if ( pparent.subitems[ i ] == this )
         pparent.subitems[ i ] = null ;
           else
         lastsubitem = pparent.subitems[ i ] ;

    pparent.maxsubitem = lastsubitem ;
if( lastsubitem == null )  
        pparent.status = pparent.status.substring( 0 , 1 ) + "0" + pparent.status.substring( 2 , 3 ) ;
    else  
     pparent.maxsubitem.status = "1" + pparent.maxsubitem.status.substring( 1 , 3 ) ;
     removenodeitem( this.id ) ;

//added by msb for move up/down
arrTemp = new Array();
j = 0;
for ( i=0; i<pparent.subitems.length; i++ ) {
  if ( pparent.subitems[i] != null ) {
   arrTemp[j] = pparent.subitems[i];
   j++;
  }
}
this.parent.subitems = arrTemp;
//end added

  pparent.refresh() ;
  //tv_topnodeitem.refresh() ;
}

function removenodeitem( id )   {
    curitem = nodeitems[ id ] ;
nodeitems[ id ] = null ;
for( m = 0 ; m < curitem.subitems.length ; m ++ )
      if( curitem.subitems[ m ] != null )   {
     userstack.put( m ) ;
     removenodeitem( curitem.subitems[ m ].id ) ;
     m = userstack.get() ;
       }
}

function nodeitem_boxclick()  {
     if( this.status.substring( 1 ,2 ) == "0" )
       return ;
  if( this.status.substring( 1 ,2 ) == "1" )  
        this.open() ;
  else 
        this.close() ;
}

function nodeitem_close()  {
     this.status = this.status.substring( 0 , 1 ) + "1" + this.status.substring( 2 , 3 ) ;
     document.all( "tv_panel_" + this.id ).style.display = "none" ;
     eval( "document.all( 'box' + this.id ).src = treeview_box_" + this.status.substring( 0 , 1 ) +"_close" ) ;
}

function nodeitem_open()  {
     this.status = this.status.substring( 0 , 1 ) + "2" + this.status.substring( 2 , 3 ) ;
     document.all( "tv_panel_" + this.id ).style.display = "" ;
     eval( "document.all( 'box' + this.id ).src = treeview_box_" + this.status.substring( 0 , 1 ) +"_open" ) ;
}

//added by msb for the move up/down
nodeitem.prototype.moveUp = nodeitem_moveUp;
nodeitem.prototype.moveDown = nodeitem_moveDown;
//end added

nodeitem.prototype.show = nodeitem_show ;
nodeitem.prototype.refresh = nodeitem_refresh ;
nodeitem.prototype.boxclick = nodeitem_boxclick ;
nodeitem.prototype.close = nodeitem_close ;
nodeitem.prototype.open = nodeitem_open ;
nodeitem.prototype.remove = nodeitem_remove ;
nodeitem.prototype.setTag = nodeitem_setTag ;
nodeitem.prototype.getTag = nodeitem_getTag ;

//==========================================================
// Public Methods
//==========================================================
function showTV()  {
     tv_topnodeitem.show() ;
}

function findNode( key )   {
pppp = null;
    for( i = 0 ; i < nodeitems.length ; i ++ ) {
    if( nodeitems[ i ] != null ) {
       if( nodeitems[ i ].key == key ) {
          pppp = nodeitems[ i ] ;
    }
    }
}
    return pppp ;
}

function addNode(  parentkey , key , lable , img  )   {
    return new nodeitem( parentkey , key , lable , img ) ;
}

function deleteNode(  key )   {
    curNode = findNode( key ) ;
if( curNode == null )
   return false ;
    curNode.remove() ;
    return true ;
}
//====== END ================

//===========================================================
// Events
//===========================================================
function box_on_click( obj )  {
    nodeitems[ obj.nodeid ].boxclick() ;
}

tv_curlable = null ;
tv_curlable_f = null ;

function lable_on_click( id )  {
       key = nodeitems[ id ].key ;
       if( nodeitems[ id ].parent == null )
       parentkey = "" ;
       else
       parentkey = nodeitems[ id ].parent.key ;
          
    if( tv_curlable != null )  {
     tv_curlable.bgColor = "transparent" ;
     tv_curlable.style.color = "#333333" ;
     tv_curlable_f.bgColor = "transparent" ;
       }
        tv_curlable = document.all("lablePanel"+id) ;
     tv_curlable.bgColor = "#000000" ;
     tv_curlable.style.color = "#FFFFFF" ;
        tv_curlable_f = document.all("f_lablePanel"+id) ;
     tv_curlable_f.bgColor = "#888888" ;

    for( i = 0 ; i < tv_listeners.length ; i ++ ) 
       if( tv_listeners[ i ].type == "click" )  {
      h = tv_listeners[ i ].handler ;
      eval( h + "( '" + key + "' , '" + parentkey + "' ) ; " ) ;
           }

}

function lable_on_dblclick( id ) {
  key = nodeitems[ id ].key ;
       if( nodeitems[ id ].parent == null )
       parentkey = "" ;
       else
       parentkey = nodeitems[ id ].parent.key ;
         
    if( tv_curlable != null )  {
     tv_curlable.bgColor = "transparent" ;
     tv_curlable.style.color = "#333333" ;
     tv_curlable_f.bgColor = "transparent" ;
       }
        tv_curlable = document.all("lablePanel"+id) ;
     tv_curlable.bgColor = "#000000" ;
     tv_curlable.style.color = "#FFFFFF" ;
        tv_curlable_f = document.all("f_lablePanel"+id) ;
     tv_curlable_f.bgColor = "#888888" ;
    for( i = 0 ; i < tv_listeners.length ; i ++ ) 
       if( tv_listeners[ i ].type == "dblclick" )  {
      h = tv_listeners[ i ].handler ;
      eval( h + "( '" + key + "' , '" + parentkey + "' ) ; " ) ;
           }
}
分享到:
评论

相关推荐

    树形结构javascript代码

    对于"dtree",这可能是一个特定的树形结构库或框架,用于创建菜单或树状视图。这类库通常会提供更高级的功能,如节点的添加、删除、展开、折叠、搜索以及事件监听等。例如,可能会有以下API: ```javascript // ...

    dhtmlxTree树形控件JavaScript

    接着,创建一个HTML元素作为树形控件的容器,通常是一个`&lt;div&gt;`标签,并指定一个唯一的ID。 2. **初始化树形控件** 使用JavaScript来初始化dhtmlxTree。这通常在DOM加载完成后执行,可以使用`window.onload`或`...

    JavaScript实现网页树形结构

    分栏设计是模仿资源管理器界面的一个关键点,这通常通过CSS布局实现,如使用Flexbox或Grid布局来创建多列视图。每一栏可以展示树形结构的不同部分,或者显示其他相关信息,如文件预览、属性等。 实现这样的树形结构...

    bootstrap treeview可搜索下拉树形

    4. **可搜索下拉**:这个功能意味着在树形视图上方有一个搜索框,用户可以输入文本进行过滤。通过监听`keyup`事件,可以实时更新树形视图,只显示与搜索文本匹配的节点。 5. **滚动条**:在描述中提到了滚动条,这...

    树形下拉多选框

    1. **树结构**:呈现为标准的树形视图,用户可以展开和折叠节点以查看和导航数据层次。 2. **包含Checkbox的多选树形结构**:在每个节点前添加了复选框,用户可以通过勾选复选框来选择多个树节点,这在需要选取多个...

    使用jsTree实现js树形结构

    通过深入学习其配置选项、事件、插件和 API,你可以创建出满足各种需求的树形视图。无论你是新手还是经验丰富的开发者,jsTree 都能提供简洁而强大的解决方案。结合提供的示例代码和文档,你将能够轻松地在项目中...

    js树形结构实现

    在JavaScript编程中,树形结构是一种非常常见的数据结构,它模拟了自然界中的树状...结合HTML和DOM操作,我们可以创建动态的、交互式的树形视图,提升用户体验。理解并熟练掌握这一技术对于前端开发者来说至关重要。

    一个js树形菜单

    本项目是一个基于JavaScript实现的树形菜单,旨在提供一种灵活且易于定制的解决方案。 1. **基础概念** - **树形结构**:树形结构是数据结构的一种,它模拟了自然界中的树状模型,具有分支和层次关系。 - **菜单*...

    多叉树结合JavaScript树形控件实现无限级树形结构(一种构建多级有序树形结构JSON(或XML)数据源的方法)

    2. Ext JS 框架:这是一种富客户端开发框架,其 TreePanel 组件常用于实现树形视图,支持 TreeNode 和 AsyncTreeNode,后者用于动态加载树节点。 3. 动态生成树节点:两种策略,一次性生成所有节点和逐级加载,前者...

    一个不错的js树形

    2. **工具** - 提示这可能是一个可复用的工具或库,开发者可以将其集成到自己的项目中,快速实现树形视图功能,提高开发效率。 【文件名称列表】 1. **tree.htm** - 这个文件很可能是展示树形结构的HTML页面,其中...

    用JavaScript+CSS美化一个Select框

    这个效果真的很不错,用JavaScript+CSS美化一个Select框,美化的最终效果像一个下拉菜单一样,带有清新的蓝色背景,从此让你的Select不再灰蒙蒙的,非常好看。

    React-reduxdevtoolslogmonitor带一个树视图的ReduxDevTools日志监控器

    `redux-devtools-log-monitor`是一个专门为Redux开发者设计的工具,它为Redux DevTools提供了一个具有树形视图的日志监控器,极大地提升了调试和理解应用状态变化的效率。本文将深入探讨这个工具的功能、工作原理...

    树形菜单和结合表格的树形菜单

    它提供了两种显示模式:原始的树形视图和嵌入表格的视图,支持简体中文和英文,这使得它具有良好的国际化兼容性。 在实际应用中,开发者可以通过调用TableTree4J_V1.2RE的API来创建和管理树形菜单。例如,可以初始...

    js树形源代码(javascript)

    "js树形源代码"即为利用JavaScript实现的这种树形结构的代码示例,它能够帮助开发者在JSP(JavaServer Pages)上创建动态的、可交互的树形视图。 在JavaScript中,实现树形结构通常涉及以下关键知识点: 1. 数据...

    树形穿梭框

    在树形穿梭框中,这两个列表显示为树状结构,用户可以选择一个或多个节点,并将其从一边移到另一边。 3. **Vue.js框架**: Vue.js是一种轻量级的JavaScript前端框架,以其易学易用、组件化和高性能而受到开发者...

    bootstrap树形下拉框 下拉框树形菜单

    `bootstrap-treeview.js`是专门设计用来创建交互式树形视图的JavaScript库,它可以轻松地与Bootstrap的其他元素集成,包括下拉框(select)。 要实现这个功能,首先需要在HTML中创建一个基础的下拉框结构,然后利用...

    bootstrap树形下拉框 下拉框树形菜单(修改版)

    `bootstrap-treeview.js`是一个基于Bootstrap的树形视图插件,它可以方便地将JSON数据转换为可交互的树形结构。这个修改版的下拉框树形菜单可能是在原生的Bootstrap下拉组件基础上进行了定制,增加了对树状数据的...

    树形下拉列表框

    3. **CSS样式**:为了使下拉列表看起来更像一个树形视图,可以通过CSS来调整元素的样式,如添加三角箭头图标,设置展开/折叠时的样式变化等。 4. **异步加载**:对于大型数据集,为了提高性能,可以采用异步加载...

    nodejs做的一个树形菜单框架和数据库文件

    在这个特定的项目中,"nodejs做的一个树形菜单框架和数据库文件"是一个利用Node.js构建的系统,它实现了树形菜单结构,这种结构常用于Web应用程序的左侧导航栏,以组织和展示多级的菜单项。用户可以通过点击这些菜单...

Global site tag (gtag.js) - Google Analytics