1. 程式人生 > >動態節點樹 多級分類 php jquery ajax json mysql 點選進入選單

動態節點樹 多級分類 php jquery ajax json mysql 點選進入選單

直接生成的呼叫動態節點樹

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" type="text/css" href="/theme/3/style.css" />
<link rel="stylesheet" type="text/css" href="/theme/3/menu_left.css" />
<script language="JavaScript" src="/inc/js/menu_left.js"></script>
<script language="JavaScript" src="/inc/js/hover_tr.js"></script>
<script type="text/javascript" src="/inc/js_lang.php"></script>
</head>
<body>
<ul>
   <li><a href="javascript:;" onclick="clickMenu('1');" target="dept_main" title="點選伸縮列表" id="link_1"><span>部門列表</span></a></li>
   <div id="module_1" class="moduleContainer treeList" style=""><div id="tree"></div>
<link rel="stylesheet" type="text/css" href="/images/org/ui.dynatree.css">
<script type="text/javascript" src="/inc/js_lang.php"></script>
<script type="text/javascript" src="/inc/js/tree.js"></script>
<script type="text/javascript">
   var tree = new Tree("tree", "/inc/dept_list/tree.php?DEPT_ID=0&PARA_URL=/general/system/dept/dept_edit.php&e=&PARA_TARGET=dept_main&PRIV_NO_FLAG=1&PARA_ID=&PARA_VALUE=&showButton=0&MODULE_ID=&MANAGE_FLAG=&DEPT_PRIV=1&USER_SELECT_FLAG=", '/images/org/', false, 3, {"minExpandLevel":2});
   tree.BuildTree();
</script>
</div>
</ul>
</body>
</html>

————————————————————————————————————————————————————————————————————————————

生成json的php檔案tree.php

<?php


function deptListTree( $PARENT_ID )
{
  global $connection;
  global $LOGIN_USER_ID;
  global $LOGIN_DEPT_ID;
  global $LOGIN_USER_PRIV;
  global $PARA_URL;
  global $PARA_TARGET;
  global $PARA_ID;
  global $PARA_VALUE;
  global $showButton;
  global $DEPT_PRIV;
  global $DEPT_ID_STR;
  global $MODULE_ID;
  global $USER_SELECT_FLAG;
  global $MANAGE_FLAG;
  global $MYOA_CHARSET;
  $query = "SELECT * from DEPARTMENT where DEPT_PARENT='".$PARENT_ID."'  order by DEPT_NO";
  $cursor1 = exequery( $connection, $query );
  while ( $ROW = mysql_fetch_array( $cursor1 ) )
  {
    $DEPT_ID1 = $ROW['DEPT_ID'];
    $DEPT_NAME1 = $ROW['DEPT_NAME'];
    $DEPT_NAME1 = htmlspecialchars( $DEPT_NAME1 );
    $DEPT_NAME1 = stripslashes( $DEPT_NAME1 );
    $IS_ORG = $ROW['IS_ORG'];
    if ( $DEPT_PRIV == 4 )
    {
      if ( $DEPT_ID1 == $LOGIN_DEPT_ID )
      {
        $DEPT_PRIV1 = 1;
      }
      else
      {
        $DEPT_PRIV1 = 0;
      }
    }
    else
    {
      $DEPT_PRIV1 = is_dept_priv( $DEPT_ID1, $DEPT_PRIV, $DEPT_ID_STR, $USER_SELECT_FLAG );
    }
    if ( $DEPT_PRIV1 == 0 )
    {
      if ( $DEPT_PRIV == "" )
      {
        $DEPT_PRIV = getpostprivbyuserid( $LOGIN_USER_ID );
      }
    }
    if ( !( $DEPT_PRIV == 6 ) )
    {
      $TITLE = $DEPT_PRIV1 == 1 ? "{$DEPT_NAME1}" : $DEPT_NAME1;
      $URL = $TARGET = $JSON = "";
      if ( $PARA_URL != "" && $DEPT_PRIV1 == 1 )
      {
        if ( $PARA_ID == "" )
        {
          $URL = "{$PARA_URL}?DEPT_ID={$DEPT_ID1}&MODULE_ID={$MODULE_ID}";
        }
        else
        {
          $URL = "{$PARA_URL}?DEPT_ID={$DEPT_ID1}&MANAGE_FLAG={$MANAGE_FLAG}&MODULE_ID={$MODULE_ID}&{$PARA_ID}=".str_replace( ".", "&", $PARA_VALUE );
        }
        $TARGET = $PARA_TARGET;
      }
      $CHILD_COUNT = 0;
      $query = "SELECT 1 from DEPARTMENT where DEPT_PARENT='".$DEPT_ID1."'";
      $cursor2 = exequery( $connection, $query );
      if ( $ROW1 = mysql_fetch_array( $cursor2 ) )
      {
        ++$CHILD_COUNT;
      }
      $IS_LAZY = FALSE;
      if ( 0 < $CHILD_COUNT )
      {
        $JSON = "/inc/dept_list/tree.php?DEPT_ID=".$DEPT_ID1."&MANAGE_FLAG={$MANAGE_FLAG}&PARA_URL={$PARA_URL}&PARA_TARGET={$PARA_TARGET}&PARA_ID={$PARA_ID}&PARA_VALUE={$PARA_VALUE}&showButton={$showButton}&MODULE_ID={$MODULE_ID}&DEPT_PRIV={$DEPT_PRIV}&USER_SELECT_FLAG={$USER_SELECT_FLAG}";
        $IS_LAZY = TRUE;
      }
      $ONCHECK = $showButton && $DEPT_PRIV1 == "1" ? "click_node" : "";
      $DEPT_ARRAY[] = array(
        "title" => td_iconv( $TITLE, $MYOA_CHARSET, "utf-8" ),
        "isFolder" => TRUE,
        "isLazy" => $IS_LAZY,
        "key" => "dept_".$DEPT_ID1,
        "dept_id" => $DEPT_ID1,
        "icon" => $IS_ORG == 1 ? "org.png" : FALSE,
        "url" => td_iconv( $URL, $MYOA_CHARSET, "utf-8" ),
        "tooltip" => td_iconv( $DEPT_NAME1, $MYOA_CHARSET, "utf-8" ),
        "json" => td_iconv( $JSON, $MYOA_CHARSET, "utf-8" ),
        "target" => $TARGET,
        "onCheck" => td_iconv( $ONCHECK, $MYOA_CHARSET, "utf-8" )
      );
    }
  }
  return $DEPT_ARRAY;
}

include_once( "inc/auth.php" );
include_once( "inc/utility_all.php" );
include_once( "inc/utility_org.php" );
ob_end_clean( );
$PARENT_ID = $DEPT_ID;
if ( $MODULE_ID != "" )
{
  if ( $DEPT_PRIV == "2" )
  {
    $query1 = "SELECT DEPT_ID from MODULE_PRIV where UID='".$LOGIN_UID."' and MODULE_ID='{$MODULE_ID}'";
    $cursor1 = exequery( $connection, $query1 );
    if ( $ROW = mysql_fetch_array( $cursor1 ) )
    {
      $DEPT_ID_STR = $ROW['DEPT_ID'];
    }
  }
}
else if ( $MODULE_ID == "" && $DEPT_PRIV == "2" )
{
  $query = "SELECT POST_DEPT from USER where UID='".$LOGIN_UID."'";
  $cursor = exequery( $connection, $query );
  if ( $ROW = mysql_fetch_array( $cursor ) )
  {
    $DEPT_ID_STR = $ROW['POST_DEPT'];
  }
}
if ( $PARENT_ID == 0 )
{
  include_once( "inc/cache/Cache.php" );
  $UNIT_NAME = $td_cache->get( "SYS_UNIT" );
  $ONCHECK = $showButton && $DEPT_PRIV == "1" ? "click_node" : "";
  $DEPT_ARRAY = deptlisttree( $PARENT_ID );
  $ORG_ARRAY = array(
    "title" => td_iconv( $UNIT_NAME, $MYOA_CHARSET, "utf-8" ),
    "isFolder" => TRUE,
    "isLazy" => FALSE,
    "expand" => TRUE,
    "key" => "dept_0",
    "dept_id" => "0",
    "icon" => "root.png",
    "tooltip" => td_iconv( $UNIT_NAME, $MYOA_CHARSET, "utf-8" ),
    "children" => $DEPT_ARRAY,
    "onCheck" => td_iconv( $ONCHECK, $MYOA_CHARSET, "utf-8" )
  );
}
else
{
  $ORG_ARRAY = deptlisttree( $PARENT_ID );
}
echo json_encode( $ORG_ARRAY );
?>

輸出結果:

{}  個json格式資料

————————————————————

menu_left呼叫

include_once( "inc/auth.php" );
ob_end_clean( );
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n<title></title>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"/theme/";
echo $LOGIN_THEME;
echo "/style.css\" />\r\n<link rel=\"stylesheet\" type=\"text/css\" href=\"/theme/";
echo $LOGIN_THEME;
echo "/menu_left.css\" />\r\n<script language=\"JavaScript\" src=\"/inc/js/menu_left.js\"></script>\r\n<script language=\"JavaScript\" src=\"/inc/js/hover_tr.js\"></script>\r\n<script type=\"text/javascript\" src=\"/inc/js_lang.php\"></script>\r\n";
echo $SCRIPT != "" ? "<script>\n".$SCRIPT."</script>" : "";
echo "</head>\r\n<body>\r\n";
if ( is_array( $MENU_HEAD ) )
{
  echo "<div class=\"";
  echo $MENU_HEAD['class'] == "" ? "head" : $MENU_HEAD['class'];
  echo "\">";
  if ( $MENU_HEAD['img'] )
  {
    echo "<img src=\"";
    echo $MENU_HEAD['img'];
    echo "\" align=\"absMiddle\">";
  }
  echo $MENU_HEAD['text'];
  echo "</div>\r\n";
}
echo "<ul>\r\n";
$I = 0;
foreach ( $MENU_LEFT as $MENU )
{
  ++$I;
  echo "   <li><a href=\"";
  echo $MENU['href'] == "" ? "javascript:;" : $MENU['href'];
  echo "\" onclick=\"";
  echo $MENU['onclick'] == "clickMenu" ? "clickMenu('".$I."');" : $MENU['onclick'];
  echo "\" target=\"";
  echo $MENU['target'] == "" ? "user_main" : $MENU['target'];
  echo "\" title=\"";
  echo $MENU['title'] == "" ? $MENU['text'] : $MENU['title'];
  echo "\" id=\"link_";
  echo $I;
  echo "\"";
  echo !$MENU['onclick'] && $MENU['module'] && !stristr( $MENU['module_style'], "display:none" ) ? " class=\"active\"" : "";
  echo "><span>";
  if ( $MENU['img'] != "" )
  {
    echo "<img src=\"";
    echo $MENU['img'];
    echo "\" width=\"16\" height=\"16\" align=\"absmiddle\"> ";
  }
  echo $MENU['text'];
  echo "</span></a></li>\r\n";
  if ( is_array( $MENU['module'] ) )
  {
    while ( list( $key, $value ) = each( &$MENU['module'] ) )
    {
      $$key = $value;
    }
    echo "   <div id=\"module_";
    echo $I;
    echo "\" class=\"moduleContainer treeList\" style=\"";
    echo $MENU['module_style'];
    echo "\">";
    include_once( $include_file );
    echo "</div>\r\n";
  }
  else if ( $MENU['module'] != "" )
  {
    echo "   <div id=\"module_";
    echo $I;
    echo "\" class=\"moduleContainer\" style=\"";
    echo $MENU['module_style'];
    echo "\">";
    echo $MENU['module'];
    echo "</div>\r\n";
  }
}
echo "</ul>\r\n";
if ( $MENU_BOTTOM )
{
  echo "  <div id=\"MenuBottom\">";
  echo $MENU_BOTTOM;
  echo "</div>\r\n";
}
echo "</body>\r\n</html>\r\n";
?>