验证中...
助力高校计算机教育 —— 码云为老师推出免费高校版,高达 200 人的协作团队
语言: PHP
分类: Web开发技术
最后更新于 2018-06-14 20:03
gistfile1.txt
原始数据 复制代码
<?php
/**
* Created by PhpStorm.
* User: liyuchao
* Date: 2017/11/28
* Time: 10:04
*/
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>

评论列表( 0 )

你可以在登录后,发表评论

10_float_left_people 10_float_left_close