jquery.edatagrid(可編輯datagrid)的使用


用spring+springmvc+mybatis+mysql實現簡單的可編輯單元格,首先是頁面效果圖:

其中,“編號”列是不可編輯的,“暫緩措施”是可以自由編輯的,主要html組成:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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}/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/jquery.edatagrid.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/common.js"></script>
<script type="text/javascript">
 
 $(function(){
     $("#dg").edatagrid({
        url:'${pageContext.request.contextPath}/customerReprieve/list.do?lossId=${param.lossId}',
        saveUrl:'${pageContext.request.contextPath}/customerReprieve/save.do?customerLoss.id=${param.lossId}',
        updateUrl:'${pageContext.request.contextPath}/customerReprieve/save.do',
        destroyUrl:'${pageContext.request.contextPath}/customerReprieve/delete.do'
     });
 });
 
 function confirmLoss(){
     $.messager.prompt('系統提示', '請輸入流失原因:', function(r){
            if (r){
                $.post("${pageContext.request.contextPath}/customerLoss/confirmLoss.do",{id:'${param.lossId}',lossReason:r},function(result){
                    if(result.success){
                         $.messager.alert("系統提示","執行成功!");
                    }else{
                        $.messager.alert("系統提示","執行失敗!");
                    }
                },"json");
            }
        });
 }
 
</script>
<title>Insert title here</title>
</head>
<body style="margin: 15px">
 
 <table id="dg" title="客戶流失暫緩措施管理" style="width:800px;height:250px"
   toolbar="#toolbar" idField="id" rownumbers="true" fitColumns="true" singleSelect="true">
   <thead>
       <tr>
           <th field="id" width="50">編號</th>
           <th field="measure" width="300" editor="{type:'validatebox',options:{required:true}}">暫緩措施</th>
       </tr>
   </thead>
 </table>
 
 <div id="toolbar">
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:$('#dg').edatagrid('addRow')">添加</a>
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:$('#dg').edatagrid('destroyRow')">刪除</a>
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="javascript:$('#dg').edatagrid('saveRow');$('#dg').edatagrid('reload')">保存</a>
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-undo" plain="true" onclick="javascript:$('#dg').edatagrid('cancelRow')">撤銷行</a>
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-confirm" plain="true" onclick="javascript:confirmLoss()">確認流失</a>
 </div>
</body>
</html>
edatagrid中定義了四個url屬性,代表四種操作的請求路徑,分別為url(列表查詢url)、saveUrl(更新保存url)、updateUrl(新增保存url)、deleteUrl(刪除url)

主要的controller實現:
/**
 * 客戶流失暫緩措施Controller層
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/customerReprieve")
public class CustomerReprieveController {
    
    @Resource
    private CustomerReprieveService customerReprieveService;
     
    /**
     * 分頁條件查詢客戶流失暫緩措施
     * @param page
     * @param rows
     * @param s_customerReprieve
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value="lossId",required=false)String lossId,HttpServletResponse response)throws Exception{
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("lossId", lossId);
        List<CustomerReprieve> customerReprieveList=customerReprieveService.find(map);
        JSONObject result=new JSONObject();
        JsonConfig jsonConfig=new JsonConfig();
        jsonConfig.setExcludes(new String[]{"customerLoss"});
        JSONArray jsonArray=JSONArray.fromObject(customerReprieveList,jsonConfig);
        result.put("rows", jsonArray);
        ResponseUtil.write(response, result);
        return null;
    }
    
    /**
     * 添加或者修改客戶流失暫緩措施
     * @param customerReprieve
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    public String save(CustomerReprieve customerReprieve,HttpServletResponse response)throws Exception{
        int resultTotal=0; // 操作的記錄條數
        if(customerReprieve.getId()==null){
            resultTotal=customerReprieveService.add(customerReprieve);
        }else{
            resultTotal=customerReprieveService.update(customerReprieve);
        }
        JSONObject result=new JSONObject();
        if(resultTotal>0){
            result.put("success", true);
        }else{
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }
    
    /**
     * 刪除客戶流失暫緩措施
     * @param ids
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(value="id")String id,HttpServletResponse response)throws Exception{
        customerReprieveService.delete(Integer.parseInt(id));
        JSONObject result=new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }
}

CustomerReprieveService(接口及實現類)主要實現:

/**
 * 客戶流失暫緩措施Service接口
 * @author Administrator
 *
 */
public interface CustomerReprieveService {

    
    /**
     * 查詢客戶流失暫緩措施集合
     * @param map
     * @return
     */
    public List<CustomerReprieve> find(Map<String,Object> map);
    
    
    /**
     * 獲取總記錄數
     * @param map
     * @return
     */
    public Long getTotal(Map<String,Object> map);
    
    /**
     * 修改客戶流失暫緩措施
     * @param customerReprieve
     * @return
     */
    public int update(CustomerReprieve customerReprieve);
    
    /**
     * 添加客戶流失暫緩措施
     * @param customerReprieve
     * @return
     */
    public int add(CustomerReprieve customerReprieve);
    
    /**
     * 刪除客戶流失暫緩措施
     * @param id
     * @return
     */
    public int delete(Integer id);
    
}
/**
 * 客戶流失暫緩措施Service實現類
 * @author Administrator
 *
 */
@Service("customerReprieveService")
public class CustomerReprieveServiceImpl implements CustomerReprieveService{

    @Resource
    private CustomerReprieveDao CustomerReprieveDao;
    
    @Override
    public List<CustomerReprieve> find(Map<String, Object> map) {
        return CustomerReprieveDao.find(map);
    }

    @Override
    public Long getTotal(Map<String, Object> map) {
        return CustomerReprieveDao.getTotal(map);
    }

    @Override
    public int update(CustomerReprieve customerReprieve) {
        return CustomerReprieveDao.update(customerReprieve);
    }

    @Override
    public int add(CustomerReprieve customerReprieve) {
        return CustomerReprieveDao.add(customerReprieve);
    }

    @Override
    public int delete(Integer id) {
        return CustomerReprieveDao.delete(id);
    }

}

接下來是dao層實現:

/**
 * 客戶流失暫緩措施Dao接口
 * @author Administrator
 *
 */
public interface CustomerReprieveDao {

    
    /**
     * 查詢客戶流失暫緩措施集合
     * @param map
     * @return
     */
    public List<CustomerReprieve> find(Map<String,Object> map);
    
    
    /**
     * 獲取總記錄數
     * @param map
     * @return
     */
    public Long getTotal(Map<String,Object> map);
    
    /**
     * 修改客戶流失暫緩措施
     * @param customerReprieve
     * @return
     */
    public int update(CustomerReprieve customerReprieve);
    
    /**
     * 添加客戶流失暫緩措施
     * @param customerReprieve
     * @return
     */
    public int add(CustomerReprieve customerReprieve);
    
    /**
     * 刪除客戶流失暫緩措施
     * @param id
     * @return
     */
    public int delete(Integer id);
    
}

因為采用的是mybatis進行ORM映射,所以不必手動寫sql,主要映射文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.CustomerReprieveDao">

    <resultMap type="CustomerReprieve" id="CustomerReprieveResult">
        <result property="id" column="id"/>
        <result property="measure" column="measure"/>
        <association property="customerLoss" column="lossId" select="com.dao.CustomerLossDao.findById"></association>
    </resultMap>
    
    <select id="find" parameterType="Map" resultMap="CustomerReprieveResult">
        select * from t_customer_reprieve
        <where>
            <if test="lossId!=null and lossId!='' ">
                and lossId = #{lossId}
            </if>
        </where>
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
    </select>

    
    <select id="getTotal" parameterType="Map" resultType="Long">
        select count(*) from t_customer_reprieve
        <where>
            <if test="lossId!=null and lossId!='' ">
                and lossId = #{lossId}
            </if>
        </where>
    </select>
    
    <insert id="add" parameterType="CustomerReprieve">
        insert into t_customer_reprieve values(null,#{customerLoss.id},#{measure})
    </insert>
    
    <update id="update" parameterType="CustomerReprieve">
        update t_customer_reprieve
        <set>
            <if test="measure!=null and measure!='' ">
                measure=#{measure},
            </if>
        </set>
        where id=#{id}
    </update>
    
    <delete id="delete" parameterType="Integer">
        delete from t_customer_reprieve where id=#{id}
    </delete>
    
</mapper> 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM