ligerUI---下拉菜单(menubar)动态显示(从后台获取数据)
写在前面:
ligerui的下拉菜单是有点丑的,这也是没有办法的事。。。。。。。。这里主要记录下,如何从后台获取数据进行菜单显示。
有两种方式:1.使用json数组来动态添加 2.字符串拼接。 其实这两中方法原理都差不多,只是实现的形式不同而已。
对于下拉菜单,首先要去看ligerui中api给的demo,看它的数据格式是怎样的,然后再根据对应的数据格式来进行动态实现。
形如这样的数据格式:
{text:"功能管理",url:"www",click:itemclick,menu:{items:[{text:"projectOne",url:"ww",click:itemclick,children:[{ text: 'Excel', click: itemclick }]}]}}
稍作解释:当一级菜单有子菜单时,会有一个menu属性,里面包含一个items,当二级菜单有子菜单时,会有一个children属性,当三级菜单有子菜单,也是会有一个children属性
后面以此类推都是含有children属性。故,我们可以从二级菜单开始这里写一个递归函数来进行调用就可以 这样就可以进行不限层级的调用了
1.json数组动态添加:
前台jsp页面:
var menuData ;
$.ajax({
url : '${baseURL}/showFunctionMenu.action?',
type : 'POST',
async:false, //同步请求
success : function(data){
//将"itemclick" 替换为itemclick 多个替换
var reg=new RegExp("\"itemclick\"","g"); //创建正则RegExp对象
data = data.replace(reg,"itemclick");
//字符串转为json
//alert(data);
menuData = eval('(' + data +')');
}
});
mainMenu = $("#mainMenu").ligerMenuBar({
width: 300,
items: menuData, //这里的menuData是上面从后台接收的数据
});
//每个菜单对应的点击函数
function itemclick(item){
//弹出对应的菜单的url地址
alert(item.url);
//对于如何做,根据自己的项目来
}
后台:
private JSONArray jsonArray;
public JSONArray getJsonArray() {
return jsonArray;
} public String showFunctionMenu() throws Exception{ //先静态设置Function 可以根据自己程序的需要 这个时候可以在对应的实体类中配置一对多的关系然后从
//数据库中进行相关查询来初始化菜单也可以
jsonArray = new JSONArray();
//初始化Function 静态设值 调用initFuncs方法 我放在了实体类里面
List<FunctionVO> menus = new FunctionVO().initFuncs(); for (FunctionVO item : menus) {
JSONObject json = new JSONObject(); json.put("text", item.getName());
json.put("id",item.getId());
json.put("url",item.getUrl());
json.put("click","itemclick"); //如果一级菜单有子菜单 就添加一个menu属性
if (item.getChildren() != null && item.getChildren().size() != 0) {
JSONArray jChildFunc = new JSONArray();
for (FunctionVO OneLevelChildFunction : item.getChildren()) {
//调用toMenu方法 方法我放在了实体类里面
String strMenu = OneLevelChildFunction.toMenu(OneLevelChildFunction);
System.out.println("strOneLeve:" + strMenu);
jChildFunc.add(strMenu);
}
//添加url click方法
json.put("menu", "{width: 160,items:" + jChildFunc.toString() + "}");
}
jsonArray.add(json.toString());
}
System.out.println(jsonArray.toString());
return SUCCESS;
}
实体类:因为是父菜单与子菜单(可以在实体类里面加一个属性children集合,然后配置一对多的关系,就可以进行关联查询了,我这里没有做关联关系的配置,只是静态设值了)
public class FunctionVO22 {
private Integer id;
private String name;
private String url;
private String des;
private Integer fId;
//可以根据具体需要 配置一对多的关联关系
private List<FunctionVO22> children;
public FunctionVO22(){}
public FunctionVO22(Integer id, String name, String url, String des, Integer fId, List<FunctionVO22> children) {
this.id = id;
this.name = name;
this.url = url;
this.des = des;
this.fId = fId;
this.children = children;
}
public String toMenu(FunctionVO22 childrenFunctions) throws Exception{
JSONObject json=new JSONObject();
json.put("text",childrenFunctions.getName());
json.put("id",childrenFunctions.getId());
json.put("url",childrenFunctions.getUrl());
json.put("click","itemclick");
JSONArray childrenJson=new JSONArray();
if(childrenFunctions.getChildren()!=null && childrenFunctions.getChildren().size() != 0){
for(FunctionVO22 child:childrenFunctions.getChildren()){
//递归调用toMenu方法
childrenJson.add(toMenu(child));
}
json.put("children", childrenJson);
}
return json.toString();
}
/**
* Function 初始化 静态设值
* @return
*/
public List<FunctionVO22> initFuncs() {
List<FunctionVO22> menus = new ArrayList<FunctionVO22>();
List<FunctionVO22> baseInfoMenus = new ArrayList<FunctionVO22>();
List<FunctionVO22> componentMenus = new ArrayList<FunctionVO22>();
//设置一级菜单值 静态设置
baseInfoMenus.add(new FunctionVO22(11, "Edit ComponentTpe", "findComponentType","Edit ComponentTpe",1,new ArrayList<FunctionVO22>()));
baseInfoMenus.add(new FunctionVO22(12, "Edit Category List", "findCategory","Edit Category List",1,new ArrayList<FunctionVO22>()));
baseInfoMenus.add(new FunctionVO22(13, "Edit Vendor List", "findVendor","Edit Vendor List",1,new ArrayList<FunctionVO22>()));
baseInfoMenus.add(new FunctionVO22(14, "Edit Customer List", "findCustomer","Edit Customer List",1,new ArrayList<FunctionVO22>()));
baseInfoMenus.add(new FunctionVO22(15, "Edit Device List", "findDevice","Edit Device List",1,new ArrayList<FunctionVO22>()));
baseInfoMenus.add(new FunctionVO22(16, "Edit Language List", "findLanguage","Edit Language List",1,new ArrayList<FunctionVO22>()));
baseInfoMenus.add(new FunctionVO22(17, "Edit Os List", "findOs","Edit Os List",1,new ArrayList<FunctionVO22>()));
baseInfoMenus.add(new FunctionVO22(18, "Edit Arch List", "findArch","Edit Arch List",1,new ArrayList<FunctionVO22>()));
componentMenus.add(new FunctionVO22(21, "Edit Component", "manageComponent_edit","Edit Component",2,null));
componentMenus.add(new FunctionVO22(22, "Component List", "manageComponent_list","Component List",2,null));
menus.add(new FunctionVO22(1, "BaseInfo Management",null,"BaseInfo Management",0,baseInfoMenus));
menus.add(new FunctionVO22(2, "Component Management",null,"Component Management",0,componentMenus));
menus.add(new FunctionVO22(3, "Project Management",null,"Project Management",0,new ArrayList<FunctionVO22>()));
return menus;
}
//省略对应的get set方法
//......
}
其实也没什么多说的,只要获得的数据格式是符合ligerui中菜单中的格式,即可了。
2.字符串拼接(这里我只做到了二级,并没有做无限层级的添加,其实这个也是可以的,只要找到格式的规律,写一个满足条件的递归函数来调用就可以了):
前台页面:
var menuData ;
$.ajax({
url : '${baseURL}/showFunctionMenu2.action?',
type : 'POST',
async:false, //同步请求
success : function(data){
//alert(data);
//字符串转为json
menuData = eval('(' + data +')'); }
}); mainMenu = $("#mainMenu").ligerMenuBar({
width: 300,
items: menuData, }); function itemclick(item){
alert(item.url); }
后台action:
public void showFunctionMenu() throws Exception{
//现在固定为二级菜单
//采用拼接的方式 将ligerMenuBar需要的数据发送到前台
StringBuilder json = new StringBuilder();
json.append("[");
//获取所有的一级Function
List<Function> functionList = functionService.getAllOneLevel();
//生成一级Function
//遍历拼接所有的一级Function
for(int i=0;i<functionList.size();i++ ){
json.append("{text:\"");
json.append(functionList.get(i).getFunctionDes());
json.append("\",url:\"");
json.append(functionList.get(i).getFunctionUrl());
json.append("\",click:itemclick,");
//json.append("{text:\"功能管理\",url:\"www\",click:itemclick}");
//获取一级Function对应的二级Function
List<Function> childrenList = functionService.getAllTwoLevel(functionList.get(i).getFunctionId());
//有二级Function
if(childrenList.size()>0){
// json.append("menu:{items:[{text:\"projectOne\",url:\"ww\",click:\"ee\",}],},");
json.append("menu:{items:[");
for(int i2=0;i2<childrenList.size();i2++){
//{ text: 'Project Management', url:"",click:"",menu:{items:[{text:'projectOne',url:"",click:"",},{text:'project2'},]}],},},
json.append("{text:\"");
json.append(childrenList.get(i2).getFunctionDes());
json.append("\",url:\"");
json.append(childrenList.get(i2).getFunctionUrl());
json.append("\",click:itemclick,},");
}
json.append("],},");
}
json.append("},");
}
json.append("]");
//因为是拼接的字符串 这里可以直接使用流来将传递字符串 而不用json格式的数据去传递
httpServletResponse.setContentType("text/html;charset=utf-8");
PrintWriter printWriter=httpServletResponse.getWriter();
printWriter.write(json.toString());
System.out.println(json.toString());
}
好啦,我要困死了,就这样吧 就当笔记记录下吧。。。。。。。。
成功截图:(api中的下拉菜单的样式很丑,这里自己稍作了样式的调整)

ligerUI---下拉菜单(menubar)动态显示(从后台获取数据)的更多相关文章
- 为joomla加入下拉菜单的方法
用 Joomla! 建站的大多数站长都须要在站点前台使用下拉菜单(dropdown menu),或者叫弹出菜单(slide menu),由于这样能够在有限的页面空间上公布很多其它的导航菜单,而且能够进 ...
- 为joomla加入�下拉菜单的方法
用 Joomla! 建站的大多数站长都须要在站点前台使用下拉菜单(dropdown menu),或者叫弹出菜单(slide menu),由于这样能够在有限的页面空间上公布很多其它的导航菜单,而且能够进 ...
- select下拉菜单反显不可改动,且submit能够提交数据
首先通过后台funcA()将下拉菜单反显不可改动的数据response到disable.jsp页面,disable.jsp: <script> var data1=${result.obj ...
- 【easyui-combobox】下拉菜单自动补全功能,Ajax获取远程数据源
这个是针对easyUI的下拉菜单使用的,Ajax获取远程数据源 HTML 页面 <input id="uname" name="uname" class= ...
- Excel添加下拉菜单
一.选中需要下拉菜单的单元格 二.数据--数据校验 三 .选择序列,填写来源 四.保存
- Excel应用----制作二级下拉菜单【转】
应用: 原始数据源是两列的源数据,那该如何制作二级下拉菜单, 当然可以将这两列的数据源,转换成上面的那种格式,再用上面的方法来制作. 今天教大学的方法是直接通过这种两列式的数据源来制作下拉菜单,如果A ...
- js面向对象封装级联下拉菜单列表
本实例开发的级联下拉菜单是根据已有json数据创建的DOM元素.点击文本框后,显示一级菜单.如果菜单中包含子菜单,菜单右侧会有指示箭头.点击菜单之后,会再显示下一级菜单,以此类推.当菜单下无子菜单时, ...
- JS实现带复选框的下拉菜单
这段时间在做后台的时候需要一个可以复选的下拉菜单,用到的是easyUI中的combo的Demo,先看看官方easyUI:http://www.jeasyui.com/documentation/ind ...
- Django分析之三级下拉菜单选择省/市/县
今天遇到了一个一直想做却没有机会去做的功能,今天完成了便记录下来. 那这次是具体是个什么功能呢?其实还是很简单的效果,就是在用户注册的时候可以选择省/市/县,很简单的一个小功能. 那现在就开始了~首先 ...
随机推荐
- typescript 的 polyfill 学习
我们知道typescript 是ES 超集.这意味着,不仅仅ES 的各种语法特性都会包括,还能保证通过typescript的编译服务可以很方便的转成ES向下兼容的版本,这得意于typescript强大 ...
- Python学习笔记(四)
Python学习笔记(四) 作业讲解 编码和解码 1. 作业讲解 重复代码瘦身 # 定义地图 nav = {'省略'} # 现在所处的层 current_layer = nav # 记录你去过的地方 ...
- JavaScript之“创意时钟”项目
“时钟展示项目”说明文档(文档尾部附有相应代码) 一.最终效果展示: 二.项目亮点 1.代码结构清晰明了 2.可以实时动态显示当前时间与当前日期 3.界面简洁.美观.大方 4.提高浏览器兼容性 三.知 ...
- mysql主从复制的异步复制与同步复制
异 步复制:MySQL本身支持单向的.异步的复制.异步复制意味着在把数据从一台机器拷贝到另一台机器时有一个延时 – 最重要的是这意味着当应用系统的事务提交已经确认时数据并不能在同一时刻拷贝/应用到从机 ...
- mysql单独可连接,php连接mysql失败之 Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
此种解决方案使用场景: 1,mysql单独可以启动而且远程工具也可以连接 2,php无法连接. 3,find / -name mysql.sock 可以找到文件路径 4,报错 Can't connec ...
- 【深度学习系列】PaddlePaddle之手写数字识别
上周在搜索关于深度学习分布式运行方式的资料时,无意间搜到了paddlepaddle,发现这个框架的分布式训练方案做的还挺不错的,想跟大家分享一下.不过呢,这块内容太复杂了,所以就简单的介绍一下padd ...
- 【NOIP2015提高组】Day2 T1 跳石头
题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 N 块岩石(不含起点和终 点的岩石).在比赛过程中,选手们将从 ...
- (转)[疯狂Java]NIO:Channel的map映射
原文出自:http://blog.csdn.net/lirx_tech/article/details/51396268 1. 通道映射技术: 1) 其实就是一种快速读写技术,它将通道所连接的数据节点 ...
- 从项目中总结的js知识点
1. 数字字符串和数字进行比较可以得出正确结果,却不能正确判断是否在一个数字数组中.如以下程序: var s = '8', n = 8, arr = [1,2,8,9]; console.log(s= ...
- LeetCode 245. Shortest Word Distance III (最短单词距离之三) $
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...