laytpl 왼쪽 메뉴 동적 무한극 렌더링

25154 단어
layui의 tpljs에서 구현된 왼쪽 메뉴 동적 무한극 렌더링
<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="layui/css/layui.css">
	</head>
	<style type="text/css">
		.box{border:1px solid #ccc;margin:2px;padding:5px;margin-left:20px;}
	  ul li{
	    list-style-type: disc;
	  }
	  ul li li li{
	    padding-left: 5px;
	  }
	  </style>
	<body>
		<div class="layui-container">
			<div id="view"></div>
		</div>
		<script id="demo" type="text/html">
			{{# var fn = function(a){ var result=""; result=layui.laytpl(childtpl.innerHTML).render(a); return result; }; }}
			<h3>{{ d.title }}</h3>
			<div class="box">
				<ul class="layui-nav layui-nav-tree" lay-filter="test">
					{{# layui.each(d.list, function(index, item){ }}
					<li class="layui-nav-item">
						<a href="javascript:;">{{item.modname}}</a>
						{{# if(item.child!=null){ }} {{ fn(item.child) }} {{# } }}
					</li>
					{{# }); }} {{# if(d.list.length === 0){ }}     {{# } }}
			</div>
			</ul>
		</script>
		<script id="childtpl" type="text/html">
			{{# var fn = function(a){ var result=""; result=layui.laytpl(childtpl.innerHTML).render(a); return result; }; }}
			<ul class="layui-nav-child">
				{{# layui.each(d, function(index, item){ }}
				<li class="layui-nav-item">
					<a href="javascript:;">{{item.modname}}</a>
					{{# if(item.child!=null){ }} {{ fn(item.child) }} {{# } }}
				</li>
				{{# }); }}
			</ul>
		</script>

		<script type="text/javascript" src="layui/layui.js"></script>
		<script type="text/javascript">
			layui.use(["laytpl", "element"], function() {
				var laytpl = layui.laytpl,
					$ = layui.jquery,
					element = layui.element;

				var data = { //  
					"title": "Layui    ",
					"list": [
						{ "modname": "  ", "alias": "layer", "site": "layer.layui.com" },
						{
							"modname": "  ",
							"alias": "form",
							"site": "layui.com",
							child: [{
								"modname": "   ",
								"alias": "c-form",
								child: [
									{ "modname": " -    ", "alias": "s-V-form" },
									{
										"modname": " -    ",
										"alias": "s-H-form",
										child: [
											{ "modname": " - -    ","alias": "s-V-form" }
										]
									}
								]
							}]
						}, { "modname": "  ", "alias": "layer", "site": "layer.layui.com" }, {
							"modname": "  ",
							"alias": "form",
							"site": "layui.com",
							child: [{
								"modname": "   ",
								"alias": "c-form",
								child: [
									{ "modname": " -    ", "alias": "s-V-form" }, { "modname": " -    ", "alias": "s-H-form" }
								]
							}]
						}
					]
				}
				var getTpl = demo.innerHTML,
					view = document.getElementById('view');
				laytpl(getTpl).render(data, function(html) {
					view.innerHTML = html;
					element.render();
				});

			});
		</script>

	</body>

</html>

본문 원작자:https://fly.layui.com/jie/45471/

좋은 웹페이지 즐겨찾기