树形控件--支持图标和多选框

原创 2007年10月10日 15:04:00

<?php
/**
 * Copyright (C),高西林.
 * File name:class.TreeMenu.php
 * Author:高西林     Version:0.1    Date:2007-10-8
 * Description:树形控件--支持图标方式和checkbox两种风格.
 * Others:邦定到该版本上的数据必须是数组
 * History:
 *  <author>    <time>     <version>       <desc>
 *  
 */
class TreeMenu
{
    public $checkbox = false;
    public $style = '';
    public $datasource ;

    //
    public function __construct()
    {
    }
    public function __destruct()
    {
    }

    /**
  *
  *
  */
    public function createTree()
    {
        if(!($this -> datasource) || count($this -> datasource) <= 0)return ;

        $treehtml = $this -> treeScript();

        $stack = array();
        $parentid = 'MENU_0';
        array_push($stack, $this -> datasource);
        $deep = 0;
        $deep2down = false;
        $deep2up = true;
        $childnodec = array();
        while($current = array_pop($stack)){
            if($deep2up){
                if($deep > 0)
                $treehtml .= '<table class="treestyle" border="0" cellspacing="0" cellpadding="0" id="'.$parentid.'_d"><tr><td>';
                else $treehtml .= '<table class="treestyle" border="0" cellspacing="0" cellpadding="0" ><tr><td>';

                if($deep  == 1){
                    $treehtml .= '<img src="/images/menu/tree_transp.gif" border="0"/></td><td>';
                }
            }

            $curdata = '<table class="treestyle" border="0" cellspacing="0" cellpadding="0"><tr>';

            $i = $deep - 1;
            while($i > 0){
                $curdata .= '<td><img src="/images/menu/tree_line.gif"/></td>';
                $i --;
            }

            $treeline = '/images/menu/tree_blank.gif';
            if($current['childs']){
                $treeline = '/images/menu/tree_minus.gif';
            }
            if( $childnodec[$deep] == 0){
                if($current['childs']){
                    $treeline = '/images/menu/tree_minusl.gif';
                }else{
                    $treeline = '/images/menu/tree_blankl.gif';
                }
            }

            $parentidstr = "";
            if($current['childs']){
                $parentid = 'MEMU_0';
                $j = $deep;
                while($j > 1){
                    $parentid .= '_0';
                    $j--;
                }
                if($j > 0) $parentid .= '_'. $childnodec[$deep];
                $parentidstr = '" id="'. $parentid .'"';
                $checkboxidstr = '" id="'. $parentid .'_"';
            }

            $curdata .= '<td><img src="'.$treeline.'" '.$parentidstr.' class="outline" style="cursor:hand" onClick="myclick(this)"/></td>';
            if($this -> checkbox){
                if($current['childs']){
                    $curdata .= '<td><input '.$checkboxidstr.' type="checkbox"  name="nodeselect[]" value="'
                    .$current['value'].'" onclick="treeboxselect(this)"/></td>';
                }

                else  $curdata .= '<td><input  type="checkbox"  name="nodeselect[]" value="'.$current['value'].'"/></td>';
            }else{
                $curdata .= '<td><img src="'.$current['ico'].'" border="0" WIDTH="19" HEIGHT="17"/></td>';
            }//end if
            if($current['link']) {
                if($current['target'])$target = $current['target'];
                else $target='_self';
                $curdata .='<td colspan="2"><a href="'.$current['link'].'" target="'.$target.'">'.$current['text'].'</a></td></tr></table>';
            }else{
                $curdata .='<td colspan="2">'.$current['text'].'</td></tr></table>';
            }


            $treehtml .=  $curdata;
            if($current['childs']){

                $childs = $current['childs'];
                $childnodec[$deep] -= 1;
                $deep += 1;
                $childnodec[$deep] = 0;
                $deep2up = true;
                $treehtml .= '';
                $childs = array_reverse($childs);
                while($child = current($childs)){

                    $childnodec[$deep] += 1;
                    array_push($stack,$child);

                    next($childs);
                }//end while

            }else{
                $deep2up = false;
            }//end if

            if($childnodec[$deep] <= 0){
                $deep -= 1;
                $deep2down = true;
            }else{
                $childnodec[$deep] -= 1;
                $deep2down = false;
            }//end if

            if($deep2down){
                $treehtml .= '  </td></tr></table>';
            }
        }//end while
        while($deep >= 0){
            $treehtml .= '  </td></tr></table>';
            $deep -= 1;
        }//end while

        return $treehtml;
    }//end function createTreee

    private function treeScript(){
        $treescript = '<script type="text/javascript">';
        $treescript .= '
                     
                       function myclick(srcelement)
                       {
                         var targetid,targetelement;
                         var strbuf;

                       if(srcelement.className=="outline")
                       {
                           targetid=srcelement.id+"_d";
                           targetelement=document.all(targetid);
                           if (targetelement.style.display=="none")
                           {
                              targetelement.style.display=/'/';
                              strbuf=srcelement.src;
                              if(strbuf.indexOf("plus.gif")>-1)
                                srcelement.src="/images/menu/tree_minus.gif";
                             else
                                srcelement.src="/images/menu/tree_minusl.gif";
                            }
                           else
                           {
                               targetelement.style.display="none";
                               strbuf=srcelement.src;
                               if(strbuf.indexOf("minus.gif")>-1)
                                 srcelement.src="/images/menu/tree_plus.gif";
                               else
                                 srcelement.src="/images/menu/tree_plusl.gif";
                             }
                       }
                    }
                    ';
        if($this -> checkbox){
            $treescript .= '
            function treeboxselect(srcelement){
                         var targetid,targetelement;
                         var strbuf;
                         targetid = srcelement.id+"d";
                         targetelement = document.all(targetid);
                         selectnode(targetelement, srcelement.checked)
            }
            function selectnode(node, tostate)
            {
               if(node.nodeName == "INPUT")
               {
                  node.checked = tostate;
                  return;
               }
               var childrens = node.childNodes;
               for(var i = 0; i < childrens.length; i++) {
                 selectnode(node.childNodes[i], tostate); //在每个子节点上进行递归操作
               }
            }
            ';
        }
        $treescript .= '</script>';

        return $treescript;
    }//end function treeScript
}//end class
?>

 

测试代码:

<?php
//include("../weblibs/framework/class.Application.php");
include("../weblibs/framework/libs/class.TreeMenu.php");
$treedata = array(
'ico' => '/images/menu/news.gif',
'text' => 'test1',
'value' => 'value1',
'childs' => array(

           array('ico' => '/images/menu/news.gif',
                 'text' => 'test11',
                 'value' => 'value11',
                 'childs' => array(
                                 array(  'ico' => '/images/menu/news.gif',
                                        'text' => 'test111',
                                        'value' => 'value111',
                                        'link' => '#A',
                                        'target' => 0,
                                        'childs' => 0
                                      ),

                                 array(
                                        'ico' => '/images/menu/news.gif',
                                       'text' => 'test112',
                                       'value' => 'value112',
                                        'link' => '#A',
                                        'target' => 0,
                                       'childs' => 0
                                      )
                                  ),
                           array(
                                 array(  'ico' => '/images/menu/news.gif',
                                        'text' => 'test113',
                                        'value' => 'value113',
                                         'link' => '#A',
                                        'target' => 0,
                                        'childs' => 0
                                       ),

                                 array(
                                        'ico' => '/images/menu/news.gif',
                                        'text' => 'test114',
                                        'value' => 'value114',
                                         'link' => '#A',
                                        'target' => 0,
                                        'childs' => 0
                                      )
                                 )
               ),

           array(  'ico' => '/images/menu/news.gif',
                   'text' => 'test12',
                   'value' => 'value12',
                    'childs' => array(
                                     array(  'ico' => '/images/menu/news.gif',
                                             'text' => 'test141',
                                             'value' => 'value141',
                                              'link' => '#A',
                                              'target' => 0,
                                             'childs' => 0
                                           ),

                                     array(
                                            'ico' => '/images/menu/news.gif',
                                            'text' => 'test142',
                                            'value' => 'value142',
                                             'link' => '#A',
                                              'target' => 0,
                                            'childs' => 0
                                           )
                                     )
                 ),

           array(
                  'ico' => '/images/menu/news.gif',
                   'text' => 'test13',
                   'value' => 'value13',
                  'childs' => 0
                ),
           array(  'ico' => '/images/menu/news.gif',
                   'text' => 'test14',
                   'value' => 'value14',
                   'childs' => array(
                                     array(  'ico' => '/images/menu/news.gif',
                                             'text' => 'test141',
                                             'value' => 'value141',
                                             'link' => '#A',
                                             'target' => 0,
                                             'childs' => 0
                                           ),

                                     array(
                                            'ico' => '/images/menu/news.gif',
                                            'text' => 'test142',
                                            'value' => 'value142',
                                            'link' => '#A',
                                             'target' => 0,
                                            'childs' => 0
                                           )
                                     )
                    ),

             array(  'ico' => '/images/menu/news.gif',
                    'text' => 'test15',
                    'value' => 'value15',
                   'childs' => 0
                    )
          )
);
$tree = new TreeMenu();
$tree -> datasource = $treedata;
$tree ->  checkbox = true;//制定控件风格,如果为false,则显示ico指定的图标,默认为false.
echo $tree->createTree();
?>

运行结果:

test1
test11
test111
test112
test12
test141
test142
test13
test14
test141
test142
test15

Android 实现自定义多级树控件和全选与反选的效果

对于多级树的显示其实就是通过数据中各个节点的关系,通过不同的缩进来达到树的效果。而数据中主要要把握id,父节点pId,name的关系,来显示其效果。 源码地址:http://download.csdn...
  • a123demi
  • a123demi
  • 2014年11月02日 08:22
  • 3716

JQuery 实现简单的复选框树形结构图

其实关于树形结构图,网上有很多插件,已经把全套的都封装好了的;但是最近公司需要做一个带复选框的树形结构,并且最关键的是要嵌入到JSP页面中,传递的数据不是用JSON传递的,是直接通过后端跳JSP页面的...
  • u011113654
  • u011113654
  • 2015年11月15日 22:14
  • 6038

Qt树形控件QTreeView使用2——复选框的设置

通过QStandardItem和QStandardItemModel可以很简单方便的给QTreeView添加节点,但是,许多树形控件都需要树的节点需要一个复选框(checkBox),网上许多资料都是通...
  • czyt1988
  • czyt1988
  • 2014年02月13日 20:24
  • 16435

MFC支持透明选择框的可多选的树形控件

  • 2012年08月23日 08:45
  • 325KB
  • 下载

Qt5下有复选框的树形控件源码

  • 2015年04月27日 16:19
  • 5KB
  • 下载

MFC 树形控件 复选框

  • 2015年06月02日 19:50
  • 22.55MB
  • 下载

QT--复选框树形控件的使用

在Qt中树形控件是QTreeWidget,控件里的树节点为QTreeWidgetItem。控件如下: 具体方法:1. 创建Qt Gui应用,基类选择“QWidget”,类名为“Widget” 2. ...
  • Acharon_L
  • Acharon_L
  • 2015年05月27日 18:26
  • 439

Qt树形控件QTreeView使用2——复选框的设置

通过QStandardItem和QStandardItemModel可以很简单方便的给QTreeView添加节点,但是,许多树形控件都需要树的节点需要一个复选框(checkBox),网上许多资料都是通...
  • czyt1988
  • czyt1988
  • 2014年02月13日 20:24
  • 16435

关于MFC中树形控件(CTreeCtrl)复选框(CheckBox)的显示一致性解决方案

原文地址:http://blog.csdn.net/plzhou/article/details/44922793 关于MFC中树形控件(CTreeCtrl)复选框(CheckBox)的显示一致性解...
  • BleuRever
  • BleuRever
  • 2016年12月15日 16:57
  • 1273

Qt树形控件QTreeView使用1——节点的添加删除操作 复选框的设置

QtreeView是ui中最常用的控件,Qt中QTreeWidget比QTreeView更简单,但没有QTreeView那么灵活(QTreeWidget封装的和MFC的CTreeCtrl很类似,没有m...
  • wcknxx
  • wcknxx
  • 2016年02月22日 15:23
  • 1231
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:树形控件--支持图标和多选框
举报原因:
原因补充:

(最多只允许输入30个字)