array->xml->xmltree 搞定,不过xmtree是别人写好的,嘿嘿
解决方案 »
- PHP数组的双循环问题
- 求助一个在多页面间提交php数据的问题
- 请问Warning: ftp_put() [function.ftp-put]: Opening BINARY mode data connection
- php smarty 疑问
- 程序中设置系统权限的时候, 为什么变量取值都要是2几次平方?
- php 导出excel格式
- PHP
- 2003年了,您还没有自己的网站吗?
- 请教关于设置phpmyadmin的密码问题,高分请教!!
- php session问题
- PHP电台,实时传播,自己有电台,如何播放
- Warning: mysql_connect() [function.mysql-connect]: Access denied for user ...
http://life161.web-48.com/t.php代码:<?PHP
header("content-type:text/html;charset=utf-8");$cats = array(
array(
'id' => 1,
'name' => '学术和教育',
'children' => array(
array(
'id' => 9,
'name' => '自然科学',
'children' => null,
),
array(
'id' => 8,
'name' => '社会科学',
'children' => null,
),
array(
'id' => 23,
'name' => '哲学',
'children' => null,
),
// ...
),
),
array(
'id' => 3,
'name' => '科技与发明',
'children' => array(
array(
'id' => 4,
'name' => '航天科技',
'children' => null,
),
array(
'id' => 5,
'name' => '火箭技术',
'chileren' => null,
),
array(
'id' => 6,
'name' => '卫星技术',
'children' => null,
),
// ...
),
),
// ...
);echo "<script type=\"text/javascript\">var myarr=".json_encode($cats).";</script>\n";
//echo json_encode($cats) ;
?>
<style type="text/css">
ul { list-style-type: none; margin:0px; width:100px}
ul li a{ display:block; background:#ccc; }
ul li a:hover{ background:#999; }
</style>
<script type="text/javascript">
var out='' ;function showout(arr){
for(var i=0;i<arr.length;i++){
if(arr[i]['children']!=null){
out += "</ul><li class='item'><a href='#'><b>"+arr[i]['name']+"</b></a></li><ul class='list'>";
showout(arr[i]['children']);
}else{
out += "<li class='item'><a href='#'><b>"+arr[i]['name']+"</b></a></li>";
}
}
return out ;
}
var outstr = "<ul class='list'>"+showout(myarr)+"</ul>";
document.write(outstr);
</script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="ul.js"></script>
ul.js:$(document).ready( function() {
$("li + ul").hide();
$("li").click( function(){
//alert($(this).text());
$(this).find("+ul.list").toggle();
});
});
自己看着很郁闷,请高手指点。
[{
"id":1,
"code":"01",
"name":"name1",
"addr":"address1",
"col4":"col4 data",
"iconCls":"icon-ok",
"children":[{
"id":2,
"code":"0101",
"name":"name11",
"addr":"address11",
"checked":true
},{
"id":3,
"code":"0102",
"name":"name12",
"addr":"address12",
"state":"closed"
}]
},{
"code":"02",
"name":"Languages abc",
"addr":"address2",
"col4":"col4 data",
"state":"closed",
"children":[{
"code":"0201",
"name":"Java",
"col4":"col4 data",
"state":"closed",
"children":[{
"code":"02013",
"name":"jdk1"
},{
"code":"02014",
"name":"jdk2"
}]
},{
"code":"0202",
"name":"C#",
"col4":"col4 data"
}]
}]
这个结构我觉得整体应用上应该比PHP来得更实际些!
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>php</title><script type="text/javascript"> var treeData = [{
id: 1,
name: "学术和教育",
children: [
{
id: 2,
name: "自然科学",
children: null
},
{
id: 3,
name: "社会科学",
children: [
{
id: 4,
name: "建筑学",
children: null
}
]
},
{
id: 4,
name: "哲学",
children: [
{
id: 4,
name: "建筑学",
children: null
}
]
}
]
},
{
id: 5,
name: "科技与发明",
children: null
}]; (function() {
var isIE = /msie/i.test(navigator.userAgent) && !window.opera; function createElement(tagName, styles, props) {
var tag = document.createElement(tagName);
if (styles) {
for (var styleName in styles) { if (isIE && styleName == "cssFloat") {
styleName = "styleFloat";
}
tag.style[styleName] = styles[styleName];
}
} if (props) {
for (var prop in props) {
tag[prop] = props[prop];
}
} return tag;
} function addNode(currentObj, parentNode) { var dlTag = createElement("dl");
var ddTag = createElement("dd", {
cursor: "pointer"
}, {
id: currentObj.id
});
var textNode = document.createTextNode(currentObj.name);
var childTag = createElement("div");
var children = currentObj.children;
if (children) {
for (var index = 0; index < children.length; index++) {
addNode(children[index], childTag);
}
} ddTag.onclick = function(e) {
var event = e || window.event; if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
var childrenDiv = this.getElementsByTagName("div")[0]; if (childrenDiv.style.display == "none") {
childrenDiv.style.display = "block";
} else {
childrenDiv.style.display = "none";
}
};
ddTag.appendChild(textNode);
ddTag.appendChild(childTag);
dlTag.appendChild(ddTag);
parentNode.appendChild(dlTag);
} function initDisplay(container) {
var childrenDivs = container.getElementsByTagName("div"); for (var index = 0; index < childrenDivs.length; index++) {
childrenDivs[index].style.display = "none";
}
} JTree = function(containerId, datas) {
var container = document.getElementById(containerId); for (var index = 0; index < datas.length; index++) {
addNode(datas[index], container);
}
initDisplay(container);
};
})();
window.onload = function() {
new JTree("container", treeData);
};
</script></head>
<body>
<div id="container"></div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>php</title><script type="text/javascript"> var treeData = [{
id: 1,
name: "学术和教育",
children: [
{
id: 2,
name: "自然科学",
children: null
},
{
id: 3,
name: "社会科学",
children: [
{
id: 4,
name: "建筑学",
children: null
}
]
},
{
id: 4,
name: "哲学",
children: [
{
id: 4,
name: "建筑学",
children: null
}
]
}
]
},
{
id: 5,
name: "科技与发明",
children: [{
id: 6,
name: "导弹",
children: [
{
id: 4,
name: "流体力学",
children: null
}
]
}]
}]; (function() {
var isIE = /msie/i.test(navigator.userAgent) && !window.opera; function createElement(tagName, styles, props) {
var tag = document.createElement(tagName);
if (styles) {
for (var styleName in styles) { if (isIE && styleName == "cssFloat") {
styleName = "styleFloat";
}
tag.style[styleName] = styles[styleName];
}
} if (props) {
for (var prop in props) {
tag[prop] = props[prop];
}
} return tag;
} function addNode(currentObj, parentNode) { var dlTag = createElement("dl");
var ddTag = createElement("dd", {
cursor: "pointer"
}, {
id: currentObj.id
});
var textNode = document.createTextNode(currentObj.name);
var childTag = createElement("div", {display: "none"});
var children = currentObj.children;
if (children) {
for (var index = 0; index < children.length; index++) {
addNode(children[index], childTag);
}
} ddTag.onclick = function(e) {
var event = e || window.event; if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
var childrenDivs = this.getElementsByTagName("div"); if (childrenDivs[0] && childrenDivs[0].style.display == "none") {
childrenDivs[0].style.display = "block";
} else {
for (var index = 0; index < childrenDivs.length; index++) {
childrenDivs[index].style.display = "none";
}
}
};
ddTag.appendChild(textNode);
ddTag.appendChild(childTag);
dlTag.appendChild(ddTag);
parentNode.appendChild(dlTag);
} JTree = function(containerId, datas) {
var container = document.getElementById(containerId); for (var index = 0; index < datas.length; index++) {
addNode(datas[index], container);
}
};
})();
window.onload = function() {
new JTree("container", treeData);
};
</script></head>
<body>
<div id="container"></div>
</body>
</html>
这个我弄过几个,没什么难度,不过确实很需要比较全面的知识
需要逆着去推,知道怎样可以构造一个收缩展开的html+css, 攥写配套js, 然后要写个php类来能够根据传入的数组构造html进行输出. 自己弄实现,不依赖现成的,会有个地方比较费时间
弄那种虚线的对准的css
输出html直接用php比较方便.用函数,输入一个符合特定要求的数组后,递归输出html当然也可以php输出json, 用js在前端组装效果,不过这样的话操作比较复杂