Created
August 11, 2020 11:47
-
-
Save llychao/8eaa6186387787d58317698ed2d35c0e to your computer and use it in GitHub Desktop.
树(导航树、权限树)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if($_POST){ | |
var_dump($_POST); | |
die; | |
} | |
function dd(){ | |
$params = func_get_args()?:null; | |
foreach ($params as $k=> $param){ | |
var_dump($param); | |
echo "<hr/>"; | |
} | |
die; | |
} | |
$datas = array( | |
1 => array('id'=>'1','pid'=>0,'name'=>'一级栏目一'), | |
2 => array('id'=>'2','pid'=>0,'name'=>'一级栏目二'), | |
3 => array('id'=>'3','pid'=>1,'name'=>'二级栏目一'), | |
4 => array('id'=>'4','pid'=>1,'name'=>'二级栏目二'), | |
5 => array('id'=>'5','pid'=>2,'name'=>'二级栏目三'), | |
6 => array('id'=>'6','pid'=>3,'name'=>'三级栏目一'), | |
7 => array('id'=>'7','pid'=>3,'name'=>'三级栏目二'), | |
8 => array('id'=>'8','pid'=>5,'name'=>'三级栏目三'), | |
); | |
function findChilds($rows, $pid=0) | |
{ | |
$lists = array(); | |
if (is_array($rows)) { | |
foreach ($rows as $k => $row) { | |
if ($row['pid'] == $pid) $lists[$row['id']] = $row; | |
} | |
} | |
return $lists; | |
} | |
function buildTree($rows, $pid=0, $level = 1,$lprefix='_level',$cprefix = '_child') | |
{ | |
$lists = findChilds($rows, $pid,$level); | |
if (is_array($lists)) { | |
foreach ($lists as $k => &$v) { | |
$v[$lprefix] = $level; | |
$tmp = buildTree($rows, $v['id'],$level + 1); | |
if (null != $tmp) { | |
$lists[$k][$cprefix] = $tmp; | |
} | |
} | |
} | |
return $lists; | |
} | |
function showTree($trees, $title = 'name',$lprefix = '_level', $cprefix = '_child',$icon="|---") | |
{ | |
$formatTree = array(); | |
foreach($trees as $key => $tree){ | |
$title_prefix = ''; | |
if ($tree['pid'] > 0) { | |
$title_prefix = str_repeat($icon, $tree[$lprefix] - 1); | |
} | |
$tree['_showName'] = $title_prefix.$tree[$title]; | |
if(!array_key_exists($cprefix, $tree)){ | |
array_push($formatTree, $tree); | |
}else{ | |
$child = $tree[$cprefix]; | |
unset($tree[$cprefix]); | |
array_push($formatTree, $tree); | |
$middle = showTree($child); //进行下一层递归 | |
$formatTree = array_merge($formatTree, $middle); | |
} | |
} | |
return $formatTree; | |
} | |
$trees = buildTree($datas); | |
$trees = showTree($trees); | |
$html = '<select name="" id="">'; | |
foreach ($trees as $key => $row) { | |
$html .= '<option value="'.$row['id'].'"> '.$row['_showName'].' </option> '; | |
} | |
$html.= '</select>'; | |
echo ($html); | |
$trees = buildTree($datas); | |
$html ='<form method="post">'; | |
foreach ($trees as $key => $row) { | |
$html .= '<input type="checkbox" name="items[]" class="admin-check" data-id="'.$row['id'].'" value="'.$row['id'].'" /> '.$row['name']; | |
if ($row['_child']){ | |
$html .= '<div style="margin-left:50px">'; | |
foreach ($row['_child'] as $child) { | |
$html .= '<input type="checkbox" name="items[]" class="admin-check" data-id="'.$child['id'].'" fid="'.$row['id'].'" value="'.$child['id'].'" /> '.$child['name']; | |
if ($child['_child']){ | |
$html .= '<div style="margin-left:100px">'; | |
foreach ($child['_child'] as $_child) { | |
$html .= '<input type="checkbox" name="items[]" data-id="'.$_child['id'].'" pid="'.$row['id'].'" fid="'.$child['id'].'" value="'.$_child['id'].'" /> '.$_child['name']; | |
} | |
$html .= '</div>'; | |
} | |
} | |
$html .= '</div>'; | |
} | |
} | |
$html.= '<button>提交</button>'; | |
$html.= '</form>'; | |
echo ($html); | |
var_dump($trees); | |
?> | |
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> | |
<script> | |
$('.admin-check').on('click',function(data){ | |
var dataId = $(this).attr('data-id'); | |
var $el = $(this).get(0); | |
if( $el.checked ){ | |
$('input[fid="'+dataId+'"]').prop('checked','checked'); | |
$('input[pid="'+dataId+'"]').prop('checked','checked'); | |
}else{ | |
$('input[fid="'+dataId+'"]').prop('checked', false); | |
$('input[pid="'+dataId+'"]').prop('checked', false); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment