easyui入门
一、什么是easyui
- easyui是基于jQuery、Angular.、Vue和React的用户界面组件的集合。
- easyui提供了构建现代交互式javascript应用程序的基本功能。
- 使用easyui,您不需要编写许多javascript代码,通常通过编写一些HTML标记来定义用户界面。
- 完整的HTML5网页框架。
- 使用easyui开发你的产品时可以大量节省你的时间和规模。
- easyui使用非常简单但功能非常强大。
二、jQuery EasyUI有以下特点:
1、基于jquery用户界面插件的集合
2、为一些当前用于交互的js应用提供必要的功能
3、EasyUI支持两种渲染方式分别为javascript方式(如:$(’#p’).panel({…}))和html标记方式(如:class=“easyui-panel”)
4、支持HTML5(通过data-options属性)
5、开发产品时可节省时间和资源
6、简单,但很强大
7、支持扩展,可根据自己的需求扩展控件
8、目前各项不足正以版本递增的方式不断完善
代码演示:
用到的src文件:
导入jquery-easyui-1.5.1:
Dao包:
MenuDao.Java:
1、查询数据库所有数据用于easyui的tree树形展示(但是直接得来的数据格式easyui不识别)
2、递归查询节点集合,形成子父节点关系,具备层次结构
3、转格式
package com.qiuwenfan.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.qiuwenfan.entity.TreeNode;
import com.qiuwenfan.util.JsonBaseDao;
import com.qiuwenfan.util.JsonUtils;
import com.qiuwenfan.util.PageBean;
import com.qiuwenfan.util.StringUtils;
/**
* 1、查询数据库所有数据用于easyui的tree树形展示(但是直接得来的数据格式easyui不识别)
* 2、递归查询节点集合,形成子父节点关系,具备层次结构
* 3、转格式
* @author qiuwenfan
*
*/
public class MenuDao extends JsonBaseDao {
/**
* List<TreeNode>加上ObjcetMapper可以转换成easyui的tree控件识别的json串
* @param map
* @param pageBean
* @return
* @throws Exception
*/
public List<TreeNode> listTreeNode(Map<String, String[]> map, PageBean pageBean) throws Exception{
List<Map<String, Object>> listMenu = this.listMenu(map, null);
List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
this.listMapToListTreeNode(listMenu, listTreeNode);
return listTreeNode;
}
/**
* List<Map<String,Object>>
* ->【{Menuid:001,Menuname:学生管理,children:[]},{Menuid:001,Menuname:学生管理}】 ————多条数据
* 接下来要递归查询子节点的集合存入当前节点
*
* @param map
* @param pageBean
* @return
* @throws Exception
*/
public List<Map<String,Object>> listMenu(Map<String, String[]> map, PageBean pageBean) throws Exception{
String sql = "select * from t_easyui_menu where true ";
String id = JsonUtils.getParamVal(map, "Menuid");
if(StringUtils.isNotBlank(id)) {
sql += " and parentid="+id;
}else {
sql += " and parentid=-1";
}
return super.executeQuery(sql, pageBean);
}
/**
* 需要将后台数据库查出来的数据格式转换成前台easyUI所识别的数据
* @param map
* @param treeNode
* @throws Exception
*/
public void mapToTreeNode(Map<String,Object> map, TreeNode treeNode) throws Exception {
treeNode.setId(map.get("Menuid").toString());
treeNode.setText(map.get("Menuname").toString());
treeNode.setAttributes(map);
Map<String, String[]> childMap = new HashMap<String, String[]>();
childMap.put("Menuid", new String[] {treeNode.getId()});
// 查询出当前节点所拥有的子节点的集合
List<Map<String, Object>> listMenu = this.listMenu(childMap, null);
List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
this.listMapToListTreeNode(listMenu, listTreeNode);
treeNode.setChildren(listTreeNode);
}
public void listMapToListTreeNode(List<Map<String, Object>> list, List<TreeNode> listTreeNode) throws Exception {
TreeNode treeNode = null;
for (Map<String, Object> map : list) {
treeNode = new TreeNode();
this.mapToTreeNode(map, treeNode);
listTreeNode.add(treeNode);
}
}
}
Entity包:
TreeNode.java:
针对easyui属性展示的json格式进行了实体类的描述
package com.qiuwenfan.entity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 针对easyui属性展示的json格式进行了实体类的描述
* @author qiuwenfan
*
*/
public class TreeNode {
private String id;
private String text;
private List<TreeNode> children = new ArrayList<TreeNode>();
private Map<String, Object> attributes = new HashMap<String, Object>();
public TreeNode() {
super();
}
public TreeNode(String id, String text, List<TreeNode> children, Map<String, Object> attributes) {
super();
this.id = id;
this.text = text;
this.children = children;
this.attributes = attributes;
}
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 List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
@Override
public String toString() {
return "TreeNode [id=" + id + ", text=" + text + ", children=" + children + ", attributes=" + attributes + "]";
}
}
Conf文件:
Mvc.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- <action path="/regAction" type="test.RegAction">
<forward name="failed" path="/reg.jsp" redirect="false" />
<forward name="success" path="/login.jsp" redirect="true" />
</action> -->
<action path="/menuAction" type="com.qiuwenfan.web.MenuAction">
</action>
<action path="/userAction" type="com.zking.web.UserAction">
<forward name="index" path="/index.jsp" redirect="false" />
</action>
</config>
Js文件:
Index.Js:
注:通过菜单去打开不同的tab页
var content = '<iframe scrolling="no" frameborder="0" src="'+menuUrl+'" width="99%" height="99%"></iframe>';
$(function(){
$('#tt').tree({
url:'menuAction.action?methodName=menuTree',
onClick: function(node){
// alert(node.attributes.menuURL); // 在用户点击的时候提示
var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
if($("#menuTab").tabs('exists',node.text)){
$("#menuTab").tabs('select',node.text)
}else{
$('#menuTab').tabs('add',{
title:node.text,
content:content,
closable:true
});
}
}
});
})
主页:
Index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/black/easyui.css">
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath }/static/easyui5/themes/icon.css">
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/easyui5/jquery.easyui.min.js"></script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/static/js/index.js"></script>
<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;">
左侧的菜单栏加载区域
<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="menuTab" class="easyui-tabs" style="height: 800px;">
<div title="首页" style="padding: 20px; display: none;">
默认首页展示内容
</div>
</div>
</div>
</body>
</html>