easyUI-入门

文章目录

jQuery EasyUI 的简介

1、什么是easyui?

easyui是一种基于jQuery的用户界面插件集合

2、easyui能带给我们什么好处?

1)easyui是个完美支持HTML5网页的完整框架
2)easyui节省网页开发的时间和规模
3)easyui很简单但功能强大

3、 防止页面缓存

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

4、easyui如何使用?
easyUI-入门

layout

布局容器有5个区域:北、南、东、西和中间。中间区域面板是必须的,边缘的面板都是可选的。每个边缘区域面板都可以通过拖拽其边框改变大小,也可以点击折叠按钮将面板折叠起来。布局可以进行嵌套,用户可以通过组合布局构建复杂的布局结构。
easyUI-入门

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/easyui5/themes/default/easyui.css">   //引入easyui.css
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/easyui5/themes/icon.css">    //引入icon.css
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/easyui5/jquery.min.js"></script>    //引入jquery.min.js
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/easyui5/jquery.easyui.min.js"></script>    //引入jquery.easyui.min.js
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/index.js"></script>  //外部的js,方便检查错误。

<title>首页</title>
</head>
<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="height:60px;background:#B3DFDA;padding:10px">north region</div>
	<div data-options="region:'west',split:true,title:'West'" style="width:150px;padding:10px;">
	</div>
	<div data-options="region:'east',split:true,collapsed:true,title:'East'" style="width:100px;padding:10px;">east region</div>
	<div data-options="region:'south',border:false" style="height:50px;background:#A9FACD;padding:10px;">south region</div>
	<div data-options="region:'center',title:'Center'">
	</div>
</body>
</html>

注:jquery.min.js与jquery.easyui.min.js位置不能互换,否则没有效果

tree

树控件在web页面中一个将分层数据以树形结构进行显示。它提供用户展开、折叠、拖拽、编辑和异步加载等功能。
easyUI-入门
接下来我们就取数据库内的数据来展示树
TreeNode(实体类)

private String id;
	private String text;
	private Map<String, Object> attributes = new HashMap<>();
	private List<TreeNode> children = new ArrayList<>();

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public Map<String, Object> getAttributes() {
		return attributes;
	}
	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}
	public List<TreeNode> getChildren() {
		return children;
	}
	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}
	public TreeNode(String id, String text, Map<String, Object> attributes, List<TreeNode> children) {
		super();
		this.id = id;
		this.text = text;
		this.attributes = attributes;
		this.children = children;
	}
	public TreeNode() {
		super();
	}
	@Override
	public String toString() {
		return "TreeNode [id=" + id + ", text=" + text + ", attributes=" + attributes + ", children=" + children + "]";
	}

MenuDao

public class MenuDao extends JsonBaseDao{
	
	/**
	 * 
	 * @param map req.getParameterMap
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<TreeNode> list(Map<String,String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		List<Map<String,Object>> listMenu = this.listMenu(map, pageBean);
		List<TreeNode> treeNodeList = new ArrayList<>();
		menuList2TreeNodeList(listMenu, treeNodeList);
		return treeNodeList;
	}
	
	/**
	 * 查询menu表的数据
	 * @param map
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<Map<String,Object>> listMenu(Map<String,String[]> map,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql = "select * from t_easyui_menu where true";
		String id = JsonUtils.getParamVal(map, "id");
		if(StringUtils.isNotBlank(id)) {
			sql = sql + " and parentid = "+id;
		}
		else {
			sql = sql + " and parentid = -1";
		}
		return super.executeQuery(sql, pageBean);
	}
	
	/**
	 * {Menuid:1,...}
	 * ->{id:1,....}
	 * menu表的数据不符合easyUI树形展示的数据格式
	 * 需要转换成easyUI所能识别的数据格式
	 * @param map
	 * @param treeNode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void menu2TreeNode(Map<String,Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
		treeNode.setId(map.get("Menuid").toString());
		treeNode.setText(map.get("Menuname").toString());
		treeNode.setAttributes(map);

		Map<String, String[]> jspMap = new HashMap<>();
		jspMap.put("id", new String[] {treeNode.getId()});
		List<Map<String, Object>> listMenu = this.listMenu(jspMap, null);
		List<TreeNode> treeNodeList = new ArrayList<>();
		menuList2TreeNodeList(listMenu, treeNodeList);
		treeNode.setChildren(treeNodeList);
	}
	
	/**
	 * [{Menuid:1,...[]},{Menuid:2,...[]}]
	 * ->[{id:1,....[]}]
	 * @param mapList
	 * @param treeNodeList
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	private void menuList2TreeNodeList(List<Map<String,Object>> mapList,List<TreeNode> treeNodeList) throws InstantiationException, IllegalAccessException, SQLException {
		TreeNode treeNode = null;
		for (Map<String,Object> map : mapList) {
			treeNode = new TreeNode();
			menu2TreeNode(map, treeNode);
			treeNodeList.add(treeNode);
		}
	}
}

MenuAction

public class MenuAction extends ActionSupport{
	private MenuDao menuDao = new MenuDao();
	
	public String treeMenu(HttpServletRequest req,HttpServletResponse resp) {
		try {
			List<TreeNode> list = this.menuDao.list(req.getParameterMap(), null);
			ObjectMapper om = new ObjectMapper();
			//将list集合转换成json串
			String jsonStr = om.writeValueAsString(list);
			ResponseUtil.write(resp, jsonStr);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

配置mvc.xml

	<action path="/menuAction" type="com.lst.web.MenuAction">
		<forward name="index" path="/index.jsp" redirect="false" />
	</action>

编写index.js

$('#tt').tree({    
    url:'menuAction.action?methodName=treeMenu'   
});

编写jsp界面

<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="height:60px;background:#B3DFDA;padding:10px">north region</div>
	<div data-options="region:'west',split:true,title:'West'" style="width:150px;padding:10px;">
		后台管理界面的菜单
		<ul id="tt"></ul>
	</div>
	<div data-options="region:'east',split:true,collapsed:true,title:'East'" style="width:100px;padding:10px;">east region</div>
	<div data-options="region:'south',border:false" style="height:50px;background:#A9FACD;padding:10px;">south region</div>
	<div data-options="region:'center',title:'Center'">
	</div>
</body

效果展示:
easyUI-入门

tabs

选项卡显示一批面板。但在同一个时间只会显示一个面板。每个选项卡面板都有头标题和一些小的按钮工具菜单,包括关闭按钮和其他自定义按钮。

点击左边的菜单在右边显示出相对应的页面
只要改动index.js就能够实现这种方式

$(function(){
	$('#tt').tree({    
	    url:'menuAction.action?methodName=treeMenu' ,
	    onClick:function(node){
	    	var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
	    	if($('#menuTabs').tabs('exists',node.text)){     //判断点击的是否已经存在,如果已经存在则移除所点击,并且选中所存在的。
	    		$('#menuTabs').tabs('select',node.text);
	    	}
	    	else{      //反之
	    		$('#menuTabs').tabs('add',{    
		    	    title:node.text,    
		    	    content:content,    
		    	    closable:true,    
		    	    tools:[{ 
		    	        iconCls:'icon-mini-refresh',    
		    	        handler:function(){    
		    	            alert('refresh');    
		    	        }    
		    	    }]    
		    	}); 
	    	}
	    }
	}); 
})

编写jsp

<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="height:60px;background:#B3DFDA;padding:10px">north region</div>
	<div data-options="region:'west',split:true,title:'West'" style="width:150px;padding:10px;">
		后台管理界面的菜单
		<ul id="tt"></ul>
	</div>
	<div data-options="region:'east',split:true,collapsed:true,title:'East'" style="width:100px;padding:10px;">east region</div>
	<div data-options="region:'south',border:false" style="height:50px;background:#A9FACD;padding:10px;">south region</div>
	<div data-options="region:'center',title:'Center'">
		<div id="menuTabs" class="easyui-tabs" style="">   
		    <div title="Tab1" style="padding:20px;display:none;">欢迎使用</div>   
		</div>  
	</div>
</body>

效果展示:
注:404是因为没有该界面
easyUI-入门

刚入门还挺容易的,就是从数据库内取出数据来,有点脑回路不畅。不过还有时间,慢慢来!!!

上一篇:EasyUI:一、入门


下一篇:easyui(一)