ホームページ  >  記事  >  バックエンド開発  >  テーブル表示無制限分類(PHP版)_PHPチュートリアル

テーブル表示無制限分類(PHP版)_PHPチュートリアル

WBOY
WBOYオリジナル
2016-07-21 15:17:29824ブラウズ

TreeTable は、セルの行と列を結合することで無限のレベルを実現し、階層構造をより適切に表示できます。
1. ID/PID/NAME の配列を構築し、後でデータベースを通じて動的データを生成します。ツリー アルゴリズム

をクリックしてコードをコピーしてください コードは次のとおりです:

array(
* 1 => array('id'=>'1','parentid'=>0) ,'name'= >'第 1 レベルの列 1'),
* 2 => array('id'=>'2','parentid'=>0,'name'=>'First -レベル列 2'),
* 3 => array('id'=>3','parentid'=>1,'name'=>'第 2 レベル列 1'),
* 4 => array('id '=>'4','parentid'=>1,'name'=>'第 2 レベルの列 2')、
* 5 => =>'5','parentid '=>2,'name'=>'第 2 レベルの列 3'),
* 6 => array('id'=>'6','parentid '=>3,'name'= >'第 3 レベルの列 1'),
* 7 => array('id'=>7','parentid'=>3,'name' =>'第 3 レベルの列 2')
* )

2. TreeTable クラス ライブラリをインポートします。
コードをコピー コードは次のとおりです:

import('@.ORG.Util.TableTree') //Thinkphp importメソッド

3. TreeTable HTMLコードを生成します
コードは次のとおりです:

$treeTable->init($treearr);
echo $treeTable->get_treetable();

注: get_treetable() はテーブル本体部、 のみを生成します。 独自に作成してください。
完全なコード
コードをコピー コードは次のとおりです:

/**
* ファイル名: TreeTable.class.php
* 作成者: run.gao 312854458@qq.com 日付: 2012-07-24 23:22 GMT+8
* 説明: ユニバーサル テーブルの無制限の分類
**/
/**
* 無制限の分類のテーブル表示は、分類の所属関係をよりよく反映できるテーブル形式でワイヤレス分類を表現することです。
* 使用方法:
* 1. 分類をインスタンス化します
* $treeTable = new。 TreeTable();
* 2. 分類を初期化します。$treearr は多次元配列であり、id、parentid、name フィールドが含まれている必要があります
* $treeTable->init($treearr); 3. 無制限の分類 HTML コードを取得します。
* echo $treeTable-> ;get_treetable();
**/
class TreeTable {
/**
* ツリー構造の生成に必要な 2 次元配列
* @var 配列
*/
public $arr = array();
* テーブルの列数
* @var int
*/
public $columns = 0;
/**
* テーブルの行数
* @var int
*/
public $rows = 0;
/**
* TreeTable データを初期化します
* @param array 2 次元配列
* array(
* 1 => array('id'=>'1','parentid'=>0,'name'=> ' 第 1 レベルの列 1'),
* 2 => array('id'=>2','parentid'=>0,'name'=>'第 1 レベルの列 2'),
* 3 => array('id'=>'3','parentid'=>1,'name'=>'第 2 レベルの列 1')、
* 4 => id'=> ;'4','parentid'=>1,'name'=>'第 2 レベルの列 2'),
* 5 => array('id'=>5', 'parentid'=>2,'name'=>'第 2 レベルの列 3'),
* 6 => array('id'=>6','parentid'=>3,' name'=>' 第 3 レベルの列 1'),
* 7 => array('id'=>'7','parentid'=>3,'name'=>'第 3 レベル列 2')
* )
*/
public function init($arr=array()){
if(!is_array($arr)) return false;
foreach ($arr as $k=>$v) {
$this->arr[$v['id']] = $v;
foreach ($this->arr as $k => $v){
$this->arr[$k]['column'] = $this->get_level($v['id'] ); // Y 軸の位置
$this->arr[$k]['arrchildid'] = $this->get_arrchildid($v['id']); // すべての子ノード
$this- > ;arr[$k]['arrparentid'] = $this->get_arrparentid($v['id']); // すべての親ノード
$this->arr[$k]['child_bottom_num'] = $this->get_child_count($v['id']) // すべての基礎となる要素ノード
}
$this->columns = $this->get_columns() // 行の総数
$ this-> ;rows = $this->get_rows(); //列の総数
// arrparentid と ID 番号で並べ替えます
$this->sort_arr(); $k = > $v){
$this->arr[$k]['row'] = $this->get_row_location($v['id']); // X 軸の位置
$ this->arr[$k]['rowspan'] = $v['child_bottom_num']; // マージされた行数
$this->arr[$k]['colspan'] = $v[' child_bottom_num'] == 0 ? $this->columns - $v['column'] + 1 : 0; //マージする列の数
}
return $this->get_tree_arr()
/ **
* 配列を取得する
**/
パブリック関数 get_tree_arr(){
return is_array($this->arr) ? $this->arr : false;
/**
* arrparentid/id 番号で配列を並べ替えます
**/
パブリック関数 sort_arr (){
// 並べ替えるフィールドへ
foreach ($this->arr as $k => $v){
$order_pid_arr[$k] = $v['arrparentid']
$order_iscost[]; = $v['sort '];
$order_id_arr[$k] = $v['id'];
// 最初に arrparentid で並べ替え、次に ID 番号で並べ替えます
array_multisort(
$order_pid_arr, SORT_ASC, SORT_STRING ,
$order_iscost , SORT_DESC, SORT_NUMERIC,
$order_id_arr, SORT_ASC, SORT_NUMERIC,
$this->arr);
// 各ノードのレベルを取得します
for ($column = 1; $column <= $this->gt ;column; $column++) {
$row_level = 0;
foreach ($this->arr as $key => $node){
if ($node['column'] == $column){
$ row_level++;
$this->arr[$key]['column_level'] = $row_level;
}
}
}
// ID をキー名として再計算します
foreach ($this->arr as $k= > $v) {
$arr[$v['id']] = $v;
$this->arr = $arr;
/**
* 親配列を取得します
* @param int
* @return array
*/
パブリック関数 get_parent ($ myid){
$newarr = array();
if(!isset($this->arr[$myid])) false を返します
$pid = $this->arr[$myid]['親 ID' ];
$pid = $this->arr[$pid]['parentid'];
if(is_array($this->arr)){
foreach($this->arr として $id) => ; $a){
if($a['parentid'] == $pid) $newarr[$id] = $a;
}
}
return $newarr;
/**
* 子配列を取得します
* @param int
* @return array
*/
パブリック関数 get_child($myid){
$a = $newarr = array();
if(is_array($this->arr)){
foreach($this->arr as $id => ; $ a){
if($a['parentid'] == $myid) $newarr[$id] = $a;
}
$newarr : false;
/**
* 現在のノードのレベルを取得します
* @param $myid 現在のノード ID 番号
**/
public function get_level($myid, $init = true){
static $level = 1;
if($init) $level = 1;
if ($this->arr[$myid]['parentid']) {
$level++;
$this->get_level($this->arr[$myid]['parentid'], false);
}
$level を返します。
}
/**
* 現在のノードのすべての基礎となるノード (子ノードのないノード) の数を取得します
* @param $myid ノード ID 番号
* @param $init 最初のロード用の静的変数
**/
public function get_child_count($myid, $init = true){
static $count = 0;
if($init) $count = 0;
if(!$this->get_child($myid) && $init) return 0;
if($childarr = $this->get_child($myid)){
foreach ($childarr as $v){
$this->get_child_count($v['id'], false);
}
}else{
$count++;
}
$count を返します。
}
/**
* ノードのすべての子ノードの ID 番号を取得します
* @param $catid ノード ID 番号
* @param $init 最初のロードで状況を静的に初期化します
**/
public function get_arrchildid($myid, $init = true) {
static $childid;
if($init) $childid = '';
if(!is_array($this->arr)) false を返します。
foreach($this->arr as $id => $a){
if($a['parentid'] == $myid) {
$childid = $childid ? $childid.','.$a['id'] : $a['id'];
$this->get_arrchildid($a['id'], false);
}
}
$childid を返す ;
}
/**
* このノードのすべての親ノードの ID 番号を取得します
* @param $id ノード ID 番号
**/
public function get_arrparentid($id, $arrparentid = '') {
if(!is_array($this->arr)) return false;
$parentid = $this->arr[$id]['parentid'];
if($parentid > 0) $arrparentid = $arrparentid ? $parentid.','.$arrparentid : $parentid;
if($parentid) $arrparentid = $this->get_arrparentid($parentid, $arrparentid);
$arrparentid を返します;
}
/**
* ノードの行位置を取得します
* @param $myid ノード ID 番号
*/
public function get_row_location($myid){
$nodearr = $this->arr;
// 获取一节点が存在する行の位置
foreach ($nodearr as $key => $node){
if($myid == $node['id']) {
$node_row_count = 0;
$arrparentid =explode(',', $node['arrparentid']);
// すべての父节点が現在の节点層次の最下層节点等以下0の元素
foreach ($arrparentid as $pid){
foreach ($nodearr as $node_row){
if($node_row['column'] == $ nodearr[$pid]['column'] && $nodearr[$pid]['column_level'] > $node_row['column_level'] && $node_row['child_bottom_num'] == 0){
$node_row_count ++;
}
}
}
// すべての現在の节点および节点层次(rowid_level)は現在の节点層の次の数より小さい
foreach ($nodearr as $node_row){
if($node['column'] == $node_row[ 'column'] && $node_row['column_level'] < $node['column_level']){
$node_row_count += $node_row['child_bottom_num'] ? $node_row['child_bottom_num'] : 1;
}
}
$node_row_count++;
休憩;
}
}
return $node_row_count;
}
/**
* テーブル内の行数を取得します
**/
パブリック関数 get_rows(){
$row = 0;
foreach ($this->arr as $key => $node){
if($node['child_bottom_num'] == 0){
$rows++; // 总行数
}
}
return $rows;
}
/**
* テーブル内の列の数を取得します
**/
パブリック関数 get_columns(){
$columns = 0 ;
foreach ($this->arr as $key => $node){
if($node['column'] > $columns){
$columns = $node['column']; // 总列数
}
}
return $columns;
}
/**
* カテゴリの表の表示形式を取得(ヘッダーを除く)
**/
パブリック関数 get_treetable(){
$table_string = '';
for($row = 1; $row rows; $row++){
$table_string .= "rt";
foreach ($this->arr as $v){
if($v['row'] == $row){
$rowspan = $v['rowspan'] ? "rowspan='{$v['rowspan']}'" : '';
$colspan = $v['colspan'] ? "colspan='{$v['colspan']}'" : '';
$table_string .= "rtt
{$v['name']}
";
}
}
$table_string .= "rt";
}
$table_string を返します。
}
}
?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/325722.html技術記事 TreeTable は、ユニットの組み合わせとリストの組み合わせによって、無制限の階層でも使用できる優れた表示階層構造を実現します。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。