一、了解EasyUI與BootStrap、LayUI的區別
1.EasyUI和LayUI對比
easyui是功能強大但是有很多的組件使用功能是十分強大的,而layui是2016年才出來的前端框架,現在才更新到2.x版本還有很多的功能沒有完善,也還存在一些不穩定的情況,但是layui界面簡約美觀,而且容易上手而且有很多組件在layui的社區里都可以找到
2.LayUI與BootStrap對比
layui是國人開發的一套框架,2016年出來的,現在已更新到2.X版本了。比較新,輕量級,樣式簡單好看。
bootstrap 相對來說是比較成熟的一個框架,現在已經更新到4.X版本。是一個很成熟的框架,這個大部分人一般都用過。
LayUI其實更偏向與后端開發人員使用,在服務端頁面上有非常好的效果
做后台框架。
BootStrap 在前端響應式方面做得很好,PC端和移動端表現都不錯。
做網站不錯。
那么我們這里為什么要講EasyUI的用法呢?
原因有三
1.easyui功能相對強大,幾乎可以滿足你開發中所有的需求
2.easyui發展比較成熟比較穩定,適合在學習中來用
3.easyui是免費的
接下來看幾個EasyUI的案例~
二、layout布局
1.創建布局
(1) 通過標簽創建布局
為<div/>標簽增加名為'easyui-layout'的類ID。
- <div id="cc" class="easyui-layout" style="width:600px;height:400px;">
- <div data-options="region:'north',title:'North Title',split:true" style="height:100px;"></div>
- <div data-options="region:'south',title:'South Title',split:true" style="height:100px;"></div>
- <div data-options="region:'east',iconCls:'icon-reload',title:'East',split:true" style="width:100px;"></div>
- <div data-options="region:'west',title:'West',split:true" style="width:100px;"></div>
- <div data-options="region:'center',title:'center title'" style="padding:5px;background:#eee;"></div>
- </div>
(2) 使用完整頁面創建布局
- <body class="easyui-layout">
- <div data-options="region:'north',title:'North Title',split:true" style="height:100px;"></div>
- <div data-options="region:'south',title:'South Title',split:true" style="height:100px;"></div>
- <div data-options="region:'east',iconCls:'icon-reload',title:'East',split:true" style="width:100px;"></div>
- <div data-options="region:'west',title:'West',split:true" style="width:100px;"></div>
- <div data-options="region:'center',title:'center title'" style="padding:5px;background:#eee;"></div>
- </body>
(3) 創建嵌套布局
注意:嵌套在內部的布局面板的左側(西面)面板是折疊的。
- <body class="easyui-layout">
- <div data-options="region:'north'" style="height:100px"></div>
- <div data-options="region:'center'">
- <div class="easyui-layout" data-options="fit:true">
- <div data-options="region:'west',collapsed:true" style="width:180px"></div>
- <div data-options="region:'center'"></div>
- </div>
- </div>
- </body>
(4)通過ajax讀取內容
布局是以面板為基礎創建的。所有的布局面板都支持異步加載URL內容。使用異步加載技術,用戶可以使自己的布局頁面顯示的內容更多更快。
- <body class="easyui-layout">
- <div data-options="region:'west',href:'west_content.php'" style="width:180px" ></div>
- <div data-options="region:'center',href:'center_content.php'" ></div>
- </body>
三、樹形組件
樹控件可以定義在一個空<ul>元素中並使用Javascript加載數據。
- <ul id="tt"></ul>
- $('#tt').tree({
- url:'tree_data.json'
- });
但是自定義表格的數據不符合easyUI屬性展示的數據格式,需要轉換成easyUI所能識別的格式
所以接下來的方法就至關重要了
- /**
- *
- * @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
- */
- public 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<>();
- //當前節點的id
- 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);
- }
- /**
- *
- * @param mapList
- * @param treeNodeList
- * @throws SQLException
- * @throws IllegalAccessException
- * @throws InstantiationException
- */
- public 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);
- }
- }
接下來要寫的就是web層
- /**
- *
- * @param req
- * @param resp
- * @return
- * @throws Exception
- */
- public String treeMenu(HttpServletRequest req,HttpServletResponse resp) throws Exception {
- List<TreeNode> list= this.menuDao.list(req.getParameterMap(), null);
- ObjectMapper om=new ObjectMapper();
- //將list集合轉換成json串
- String jsonStr= om.writeValueAsString(list);
- //把json串寫到jsp頁面里面去
- ResponseUtil.write(resp, jsonStr);
- return "index";
- }
四、選項卡tabs
創建面板
1. 通過標簽創建選項卡
通過標簽可以更容易的創建選項卡,我們不需要寫任何Javascript代碼。只需要給<div/>標簽添加一個類ID'easyui-tabs'。每個選項卡面板都通過子<div/>標簽進行創建,用法和panel(面板)相同。
- <div id="tt" class="easyui-tabs" style="width:500px;height:250px;">
- <div title="Tab1" style="padding:20px;display:none;">
- tab1
- </div>
- <div title="Tab2" data-options="closable:true" style="overflow:auto;padding:20px;display:none;">
- tab2
- </div>
- <div title="Tab3" data-options="iconCls:'icon-reload',closable:true" style="padding:20px;display:none;">
- tab3
- </div>
- </div>
2. 通過Javascript創建選項卡
下面的代碼演示如何使用Javascript創建選項卡,當該選項卡被選擇時將會觸發'onSelect'事件。
- $('#tt').tabs({
- border:false,
- onSelect:function(title){
- alert(title+' is selected');
- }
- });
添加新的選項卡面板
添加一個新的包含小工具菜單的選項卡面板,小工具菜單圖標(8x8)被放置在關閉按鈕之前。
- // add a new tab panel
- $('#tt').tabs('add',{
- title:'New Tab',
- content:'Tab Body',
- closable:true,
- tools:[{
- iconCls:'icon-mini-refresh',
- handler:function(){
- alert('refresh');
- }
- }]
- });
獲取選擇的選項卡
- // get the selected tab panel and its tab object
- var pp = $('#tt').tabs('getSelected');
- var tab = pp.panel('options').tab; // the corresponding tab object
樹形菜單加tabs效果圖: