1. 程式人生 > >ssm+easyiu+實顯分頁+增刪改查寫後臺

ssm+easyiu+實顯分頁+增刪改查寫後臺

easyui分頁圖:

前段程式碼:

注意:

這裡要分清楚裡面他們的差別,不然傳入的ID就不正確,就進不了Controller裡面的刪除方法

如果刪除一行用getselected,如果刪除多行用getselections,傳入的就是一個數組

 

     //獲取一行的所有元素
     var row=$("#dg").datagrid("getSelected");
     //獲取所有選中行的所有元素
      var selectedRows=$("#dg").datagrid("getSelections");

很煩,用下面這種方法不能很好的展示easyui裡面的修改和刪除的圖示,所以暫且這樣寫著,以後學到更好的方法再改

 

//行內新增連結
         function formatOper(val,row,index){
            var b="";
            b+= '<a href="javascript:openBlogTypeModifyDialog()"  rel="external nofollow" onclick="editUser('+index+')" ><img src="static/jquery-easyui-1.5.4.2/themes/icons/pencil.png" />修改</a>    ';
            b+= '<a href="javascript:deleteBlogType()"  rel="external nofollow"  ><img src="static/jquery-easyui-1.5.4.2/themes/icons/cancel.png" />刪除</a>';
            return b;
        }

點選新增按鈕的時候發現裡面顯示有預設值,然後不知道為什麼用

$("#resName").val("");

這種方式清空不了新增彈框的預設值,用這種才解決       

 

$("#isParent").textbox("setValue", "");                               

<%@ 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">
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>系統選單管理</title>
<link rel="stylesheet" type="text/css" href="static/jquery-easyui-1.5.4.2/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="static/jquery-easyui-1.5.4.2/themes/icon.css">
<script type="text/javascript" src="static/jquery-easyui-1.5.4.2/jquery.min.js"></script>
<script type="text/javascript" src="static/jquery-easyui-1.5.4.2/jquery.easyui.min.js"></script>
<script type="text/javascript" src="static/jquery-easyui-1.5.4.2/locale/easyui-lang-zh_CN.js"></script>
<style type="text/css">

</style>
<script type="text/javascript">    
        //模糊查詢,再次載入資料
        function searchBlog(){
            $("#dg").datagrid('load',{
                "resName":$("#s_title").val()
            });
            /* alert($("#s_title").val()); */
        }
        //行內新增連結
         function formatOper(val,row,index){
            /* alert("ddd"); */
            var b="";
            b+= '<a href="javascript:openBlogTypeModifyDialog()"  rel="external nofollow" onclick="editUser('+index+')" ><img src="static/jquery-easyui-1.5.4.2/themes/icons/pencil.png" />修改</a>    ';
            b+= '<a href="javascript:deleteBlogType()"  rel="external nofollow"  ><img src="static/jquery-easyui-1.5.4.2/themes/icons/cancel.png" />刪除</a>';
            return b;
        }
         //修改
         function openBlogTypeModifyDialog(){
            var selectedRows=$("#dg").datagrid("getSelections");
            if(selectedRows.length!=1){
                $.messager.alert("系統提示","請選擇一個要修改的系統選單類別!");
                return;
            }
            var row=selectedRows[0];
            $("#dlg").dialog("open").dialog("setTitle","修改系統選單類別資訊");
            $("#fm").form("load",row);
            url="${request.contextPath}/yuanguoRDP1/save?resId="+row.resId;
            //alert(row.resId);
        }
         
         
         //新增
         function addResource(){
            $("#dlg").dialog("open").dialog("setTitle","新增選單資訊");
            url="${request.contextPath}/yuanguoRDP1/save";
            //resetValue();//清空資料
        }
         
         //儲存
        function saveBlogType(){
            $("#fm").form("submit",{
                url:url,
                onSubmit:function(){
                    return $(this).form("validate");
                },
                success:function(result){
                    var result=eval('('+result+')');
                    if(result.success){
                        $.messager.alert("系統提示","儲存成功!");
                        resetValue();//清空資料
                        $("#dlg").dialog("close");
                        $("#dg").datagrid("reload");
                    }else{
                        $.messager.alert("系統提示","儲存失敗!");
                        return;
                    }
                }
            });
        }
         
        //關閉
        function closeBlogTypeDialog(){
            $("#dlg").dialog("close");
            resetValue();
        }
        
        //清除
        function resetValue(){
            //alert("開始清除!");
            /*這種方式並不能清空輸入框的值
            $("#isParent").val("");
            $("#resName").val("");
            $("#resLink").val("");
            $("#sortNum").val(""); */
            $("#isParent").textbox("setValue", "");
            $("#resName").textbox("setValue", "");
            $("#resLink").textbox("setValue", "");
            $("#sortNum").textbox("setValue", "");
            //alert("清除成功!"+row.resName);
        }

         
         /* function editUser(index){
              $('#dg').datagrid('selectRow',index);// 關鍵在這裡
              var row = $('#dg').datagrid('getSelected');
              if (row){
                $('#dlg').dialog('open').dialog('setTitle','修改學生資訊');
                $('#fm').form('load',row);
                url = '${ctx}updateStudent.do?id='+row.id;
              }
         }  */
         //刪除
         function deleteBlogType(){
        
                //獲取一行的所有元素
                var row=$("#dg").datagrid("getSelected");
                //獲取所有選中行的所有元素
                var selectedRows=$("#dg").datagrid("getSelections");
                //alert(row.resName+"@@"+row.resId);
                
                /* if(selectedRows.length==0){
                    $.messager.alert("系統提示","請選擇要刪除的資料!");
                    return;
                }  */
                /*  var strIds=[];
                for(var i=0;i<selectedRows.length;i++){
                    strIds.push(selectedRows[i].id);
                }
                var ids=strIds.join(",");   */
                $.messager.confirm("系統提示","您確定要刪除這<font color=red>"+selectedRows.length+"</font>條資料嗎?",function(r){
                    if(r){
                         $.post("${request.contextPath}/yuanguoRDP1/deleteResource",
                                 {resId:row.resId},function(result){
                            if(result.success){
                                if(result.exist){
                                    $.messager.alert("系統提示",result.exist);
                                }else{
                                    $.messager.alert("系統提示","資料已成功刪除!");                            
                                }
                                $("#dg").datagrid("reload");
                             }else{
                                $.messager.alert("系統提示","資料刪除失敗!");
                            }
                        },"json");
                    }
                });
            }
        
          
</script>
</head>
<body style="margin: 1px">
<table id="dg" title="系統選單管理" class="easyui-datagrid"
  fitColumns="true" pagination="true" rownumbers="true"  singleSelect="true"
  url="${request.contextPath}/yuanguoRDP1/Resourcelist" fit="true" toolbar="#tb">
  <thead>
      <tr>
          <!-- <th field="cb" checkbox="true" align="center"></th> -->
          <th field="resName" width="200" align="center">選單名稱</th>
          <th field="resLink" width="50" align="center">連結</th>
          <th field="sortNum" width="50" align="center" >排序號</th>
          <th field="status" width="100" align="center" >狀態</th>
          <th field="_operate"width="80" align="center" formatter="formatOper">操作</th>
      </tr>
  </thead>
</table>

<div id="tb">
    <div>
        <a href="javascript:addResource()" class="easyui-linkbutton" iconCls="icon-add" plain="true">新增選單</a>
    </div>
    <div>
         選單名稱 <input type="text" id="s_title" size="20"  onkeydown="if(event.keyCode==13) searchBlog()"/>
        <a href="javascript:searchBlog()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜尋</a>
    </div>
</div>


<div id="dlg" class="easyui-dialog" style="width: 400px;height: 300px;padding: 10px 20px" closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post">
        <table cellspacing="8px">
            <tr>
                <td>上級選單</td>
                <td>
                <select name="isParent" id="isParent" class="easyui-combobox" >
                  <option value="1"  selected = "selected" >父節點</option>
                  <option value="0">否</option>
                </select>
                </td>
            </tr>
            <tr>
                <td>選單名稱</td>
                <td><input type="text" name="resName"  id="resName" class="easyui-textbox" style="width:100%;" required="true" data-options="prompt:'請填寫選單名稱'" ></td>
            </tr>
            <tr>
                <td>連結</td>
                <td><input type="text" name="resLink" id="resLink" class="easyui-textbox" style="width:100%;" required="true"  data-options="prompt:'請填寫連結'"></td>
            </tr>
            <tr>
                <td>排序號</td>
                <td><input type="text" name="sortNum"  id="sortNum" class="easyui-textbox" style="width:100%;" required="true" data-options="prompt:'請填寫序號'"></td>
            </tr>
        </table>
    </form>
</div>

<div id="dlg-buttons">
    <a href="javascript:saveBlogType()" class="easyui-linkbutton" iconCls="icon-ok">儲存</a>
    <a href="javascript:closeBlogTypeDialog()" class="easyui-linkbutton" iconCls="icon-cancel">關閉</a>
</div>
</body>

</html>

 

 

後臺程式碼如下

 

 

控制層Controller

    /**
     * 分頁查詢顯示系統選單
     * */
    @RequestMapping("/Resourcelist")
    public String list(Resource resource,HttpServletRequest request,HttpServletResponse response)throws Exception{
        
        int page  =Integer.parseInt(request.getParameter("page")) ;// 第幾頁
        int rows  = Integer.parseInt(request.getParameter("rows"));//存放每頁記錄數
        PageBean pageBean=new PageBean(page,rows);
        
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("resName",StringUtils.formatLike(resource.getResName()) );
        map.put("start",pageBean.getStart());// 始頁第一條數
        map.put("size", pageBean.getPageSize());// 每頁記錄數
        
        //設定前臺狀態,把數字轉化成文字
        List<Resource>  resList=resourceService.list(map);
        for (Resource resource2 : resList) {
            if(Integer.parseInt(resource2.getStatus())==1){
                resource2.setStatus("正常");
            }else{
                resource2.setStatus("禁用");
            }
        }
        long total=resourceService.getTotal(map);
        
        //System.out.println("zhe???"+total);
        JSONObject result=new JSONObject();
        result.put("rows", resList);//存放每頁記錄數
        result.put("total",total);//存放總記錄樹
        //System.out.println(result);
        ResponseUtil.write(response,result);
        return null;
    }
    
    /**
     * 新增或者修改系統選單
     * */
    @RequestMapping("/save")
    public String save(Resource resource,HttpServletResponse response)throws Exception{
        int resultTotal=0;
        if(resource.getResId()==null){
            resultTotal=resourceService.addResource(resource);
        }else{
            resultTotal=resourceService.updateResource(resource);
        }
        JSONObject result=new JSONObject();
        if(resultTotal>0){
            result.put("success", true);
        }else{
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }
    

    /**
     * 系統選單刪除
     */
    @RequestMapping("/deleteResource")
    public String delete(@RequestParam(value="resId",required=false)String resId,HttpServletResponse response)throws Exception{
        System.out.println("進來了!!!!");
        System.out.println(resId);
        int resultTotal=0;
        resultTotal=resourceService.DeleatResource(Integer.parseInt(resId));
        System.out.println(resultTotal);
        JSONObject result=new JSONObject();
        if(resultTotal>0){
            result.put("success", true);
        }else{
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }
    
    @RequestMapping("/tolist")
    public String listGG(){
        
        return "resource";

    }

 

 

service,dao層分頁,新增,修改,刪除:

 

 

 

/**
     * 分頁查詢選單管理
     * @param map
     * @return
     */
    public List<Resource> list(Map<String,Object> map);
    //查詢系統選單總數
    Long  getTotal(Map<String, Object> map);
    //修改系統選單
    Integer  updateResource(Resource resource);
    //新增系統選單
    Integer addResource(Resource resource);
    //刪除系統選單資訊
    Integer DeleatResource(Integer resId);

mapper.xml

<select id="list" parameterType="Map"  resultMap="BaseResultMap">
        select * from ta_autho_resource where is_delete='0'
        
            <if test="resName!=null and resName!=''">
                and res_name LIKE CONCAT('%',#{resName},'%')
            </if>
            <if test="resLink!=null and resLink!='' ">
                and res_link=#{resLink}
            </if>
            <if test="sortNum!=null and sortNum!='' ">
                and sort_num=#{sortNum}
            </if>
            <if test="status!=null and status!='' ">
                and status=#{status}
            </if>
            <if test="isParent!=null and isParent!='' ">
                and is_parent =#{isParent}
            </if>
            <if test="isDelete!=null and isDelete!='' ">
                and is_parent =0
            </if>
        
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
</select>
<select id="getTotal" parameterType="Map" resultType="Long">
        select count(*) from ta_autho_resource where is_delete='0'
        <if test="resName!=null and resName!=''">
                and res_name LIKE CONCAT('%',#{resName},'%')
            </if>
            <if test="resLink!=null and resLink!='' ">
                and res_link=#{resLink}
            </if>
            <if test="sortNum!=null and sortNum!='' ">
                and sort_num=#{sortNum}
            </if>
            <if test="status!=null and status!='' ">
                and status=#{status}
            </if>
            <if test="isParent!=null and isParent!='' ">
                and is_parent =#{isParent}
            </if>
            <if test="isDelete!=null and isDelete!='' ">
                and is_parent =0
            </if>    
</select>

<update id="updateResource" parameterType="com.yuanguo.systemManage.entity.Resource">
 update ta_autho_resource
    <set >
      <if test="resName != null" >
        RES_NAME = #{resName,jdbcType=VARCHAR},
      </if>
      <if test="parentResId != null" >
        PARENT_RES_ID = #{parentResId,jdbcType=INTEGER},
      </if>
      <if test="resLink != null" >
        RES_LINK = #{resLink,jdbcType=VARCHAR},
      </if>
      <if test="sortNum != null" >
        SORT_NUM = #{sortNum,jdbcType=INTEGER},
      </if>
      <if test="remark != null" >
        REMARK = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="resType != null" >
        RES_TYPE = #{resType,jdbcType=CHAR},
      </if>
      <if test="registerTime != null" >
        REGISTER_TIME = #{registerTime,jdbcType=TIMESTAMP},
      </if>
      <if test="registerUserId != null" >
        REGISTER_USER_ID = #{registerUserId,jdbcType=INTEGER},
      </if>
      <if test="updateTime != null" >
        UPDATE_TIME = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUserId != null" >
        UPDATE_USER_ID = #{updateUserId,jdbcType=INTEGER},
      </if>
      <if test="isParent != null" >
        IS_PARENT = #{isParent,jdbcType=INTEGER},
      </if>
      <if test="status != null" >
        STATUS = #{status,jdbcType=CHAR},
      </if>
    </set>
    where RES_ID = #{resId,jdbcType=INTEGER}
</update>
<insert id="addResource" parameterType="com.yuanguo.systemManage.entity.Resource">
        INSERT INTO ta_autho_resource (is_parent,res_name,res_link,sort_num) VALUES(#{isParent},#{resName},#{resLink},#{sortNum})    
</insert>
<update id="DeleatResource" parameterType="com.yuanguo.systemManage.entity.Resource">
        update ta_autho_resource set is_delete=1 where RES_ID = #{resId,jdbcType=INTEGER}
</update>   

 

原始碼