问题描述
easyui datagrid关于数据的显示问题有一个easyui datagrid需要显示的是系统用户的角色名称 但是角色的信息是在一个关联关系表中维护的 如何取到这个角色名称并显示在easyui datagrid的数据表格中
解决方案
$(function(){var lastIndex;$('#user').datagrid({url:'user/selectAll.action',columns : [ [ {title : 'User ID',field : 'id',width : 100,hidden:true}, {title : '用户名',field : 'userName',width : 150,editor:'text'},{title : '邮箱',field : 'email',width : 150,editor:'text'},{title : '地址',field : 'address',width : 150,editor:'text'}, {title : '密码',field : 'password',width : 150,editor:'text'}, {title : '创建时间',field : 'createTime',width : 150},{title : '所属角色',field : 'rid',width : 150,formatter : function(value, rowData, rowIndex) {if (rowData.rname) {return rowData.rname;}},editor : {type : 'combobox',options : {url : 'role/combox.action',valueField : 'rid',textField : 'rname'}}} ] ],toolbar:[{text:'append',iconCls:'icon-add',width:'auto',height:'auto',handler:function(){$('#user').datagrid('endEdit', lastIndex);$('#user').datagrid('appendRow',{userName:'',password:'',email:'',address:'',rid:'',createTime:''});lastIndex = $('#user').datagrid('getRows').length-1;$('#user').datagrid('selectRow', lastIndex);$('#user').datagrid('beginEdit', lastIndex);}},'-',{text:'delete',iconCls:'icon-remove',handler:function(){var row = $('#user').datagrid('getSelected');if (row){var index = $('#user').datagrid('getRowIndex', row);$('#user').datagrid('deleteRow', index);$.ajax({url:'user/userDelete.action',type:'POST',data:{"user.id":row.id},success:function(data){$('#user').datagrid().load("user/selectAll.action");}});}}},'-',{text:'accept',iconCls:'icon-save',handler:function(){$('#user').datagrid('acceptChanges');var row = $('#user').datagrid('getSelected');$.ajax({url:'user/userAdd.action',type:'POST',data:{"user.userName":row.userName,"user.password":row.password,"user.email":row.email,"user.address":row.address,"role.rid":row.rid},success:function(data){$('#user').datagrid().load("user/selectAll.action");}});}},'-',{text:'导出Excel',iconCls:'icon-add',width:'auto',height:'auto',handler:function(){//$.ajax({//url:'user/exportExcel.action',//type:'POST',//success:function(data){// alert("success");//}//});window.location.href = 'user/exportExcel.action';}}],onBeforeLoad:function(){$(this).datagrid('rejectChanges');},onClickRow:function(rowIndex){if (lastIndex != rowIndex){$('#user').datagrid('endEdit', lastIndex);$('#user').datagrid('beginEdit', rowIndex);}lastIndex = rowIndex;}});}); public String selectAll() { PageModel<User> pm = userService.findAll(); jsonMap.put("total", pm.getTotal()); List jsonList=new ArrayList(); for(User user:pm.getList()){ Map map=new HashMap(); UserRole userRole=userService.findRole(user.getId()); map.put("id", user.getId()); map.put("userName", user.getUserName()); map.put("email", user.getEmail()); map.put("address", user.getAddress()); map.put("password", user.getPassword()); map.put("createTime", user.getCreateTime()); map.put("rid", userRole.getRole().getRid()); map.put("rname", userRole.getRole().getRname()); jsonList.add(map); System.out.println("姓名:"+user.getUserName()+"角色:"+userRole.getRole().getRname()); } <action name="selectAll" class="userAction" method="selectAll"> <result type="json"><param name="root">jsonMap</param><param name="includeProperties">^total,^rows[d+].id,^rows[d+].userName,^rows[d+].address,^rows[d+].password,^rows[d+].createTime,^rows[d+].email,^rows[d+].rid,^rows[d+].rname</param><param name="noCache">true</param><param name="contentType">text/html</param></result> </action> jsonMap.put("rows", jsonList); return SUCCESS; }
解决方案二:
首先是你需要查询出所有的角色名称,至于如何从关联表里查询,这个需要你自己查询出来。一般情况下你可以把查询出来的角色名称放到一个集合里,然后还回一个datagrid需要的一个json格式就可以了。
解决方案三:
你查询出角色后,字节按照datagrid的json格式拼成json字符串返回即可