码迷,mamicode.com
首页 > 其他好文 > 详细

金甲防线服务项目笔记:新增、编辑与删除

时间:2017-09-07 21:36:09      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:str   data   sse   edits   exec   16px   equals   selection   获取   

一、新增:

  //添加序列号
    function addSerialNumber(obj){
        var type = $("input[name=‘serialNumber‘]:checked").val();
        showWindow($(obj).attr("title"),"genuineSerialNumberManagementAction_addUI.do?genuineSerialNumberManagement.type="+type,"500","320",true);
    }

1、后台新增:由于需要传入type来辨别是正版还是非正版,所以在  addUI  上需要将type  get出来,然后传给前台页面,前台页面通过下面这样去获取

var type = <s:property value="genuineSerialNumberManagement.type"></s:property>;
  //新增序列号UI
    public String addUI(){
        if(genuineSerialNumberManagement.getType() != null && !"".equals(genuineSerialNumberManagement.getType())){
            getRequest().setAttribute("type", genuineSerialNumberManagement.getType());
        }
        return RETURN_ADDUI;
    }

2、addUI页面:表单里面需要加上隐藏域,否则后台那边收不到值

<form id="genuineSerialNumberManagementAction_add">
            <table id="itemTable" align="center">
                <tr><input type="hidden" id="type" name="genuineSerialNumberManagement.type"></tr>
                <tr><input type="hidden" id="softId" name="genuineSerialNumberManagement.softId"></tr>
                <tr>
                    <td class="rightAlign"><label>软件名:</label></td>
                    <td><input type="text" id="softDisplayName" name="genuineSerialNumberManagement.softDisplayName" class="inputText {required:true}" /> <label id=requiredLabel>*</label></td>
                </tr>
                <tr>
                    <td class="rightAlign"><label>版本号:</label></td>
                    <td><input type="text" id="version" name="genuineSerialNumberManagement.version" class="inputText {required:true}" /> <label id=requiredLabel>*</label></td>
                </tr>
                <tr>
                    <td class="rightAlign"><label>序列号:</label></td>
                    <td><input type="text" id="serialNumber" name="genuineSerialNumberManagement.serialNumber" class="inputText {required:true}" /> <label id=requiredLabel>*</label></td>
                </tr>
                <tr style="line-height: 60px;">
                    <td colspan="2" align="center">
                        <button class="layerButton" id="buttonSubmit">
                            <span>提交</span>
                        </button>
                        <button class="layerButton" id="closeBT">
                            <span>取消</span>
                        </button>
                    </td>
                </tr>
            </table>
        </form>
$(function(){
        var type = <s:property value="genuineSerialNumberManagement.type"></s:property>;
        $("#type").val(type);
        $("#softId").val(11223344);
        
        $("#buttonSubmit").click(function(){
            var softDisplayName = $("#softDisplayName").val(),
                version = $("#version").val(),
                softId = $("#serialNumber").val(),
                type = $("#type").val(),
                serialNumber = $("#serialNumber").val();
            $("#genuineSerialNumberManagementAction_add").form("submit",{
                method : "POST",
                url : "genuineSerialNumberManagementAction_add.do?disc=genuineSerialNumberManagement_add&genuineSerialNumberManagement.softDisplayName="
                        + escape(escape(softDisplayName)) + "&genuineSerialNumberManagement.version=" + escape(escape(version))
                        + "&genuineSerialNumberManagement.serialNumber=" + escape(escape(serialNumber))
                        + "&genuineSerialNumberManagement.type=" + type
                        + "&genuineSerialNumberManagement.softId=" + softId,
                timeout : 3000000,
                success : function(data){
                    var jsondata = eval("(" + data + ")");
                    if(jsondata.msg == success){
                        if(type == 1){
                            getDeskTopObj().$(#inside_tableElement).datagrid(insertRow,{
                                index:0,
                                row: {
                                    id : jsondata.id,
                                    softDisplayName : jsondata.softDisplayName,
                                    version : jsondata.version,
                                    serialNumber : jsondata.serialNumber
                                }
                            });
                        }else{
                            getDeskTopObj().$(#inside_tableElement_no).datagrid(insertRow,{
                                index:0,
                                row: {
                                    id : jsondata.id,
                                    softDisplayName : jsondata.softDisplayName,
                                    version : jsondata.version,
                                    serialNumber : jsondata.serialNumber
                                }
                            });
                        }
                        showMsg("", "添加成功!", true);                    
                    }else{
                        showMsg("", "添加失败!", false);
                    }
                }
            })
            return false;//禁止表单提交事件,防止2次请求
        })
        
    });

注意表单提交的时候因为有默认的submit,所以请求会提交2次,需要阻止表单的默认提交事件。

3、后台新增功能:

  //新增序列号
    public void add(){
        log.info("【正版序列号管理--新增序列号:开始】");
        this.msg = RESULT_FAIL;
        GenuineSerialNumberManagement temp = null;
        String resultInfo = "";
        try {
            this.msg = RESULT_FAIL;
            if(genuineSerialNumberManagement.getSoftDisplayName() != null && !"".equals(genuineSerialNumberManagement.getSoftDisplayName())){
                genuineSerialNumberManagement.setSoftDisplayName(EscapeUnescape.unescape(genuineSerialNumberManagement.getSoftDisplayName()));
            }
            if(genuineSerialNumberManagement.getVersion() != null && !"".equals(genuineSerialNumberManagement.getVersion())){
                genuineSerialNumberManagement.setVersion(EscapeUnescape.unescape(genuineSerialNumberManagement.getVersion()));
            }
            if(genuineSerialNumberManagement.getSerialNumber() != null && !"".equals(genuineSerialNumberManagement.getSerialNumber())){
                genuineSerialNumberManagement.setSerialNumber(EscapeUnescape.unescape(genuineSerialNumberManagement.getSerialNumber()));
            }
            if(genuineSerialNumberManagement.getType() != null && !"".equals(genuineSerialNumberManagement.getType())){
                genuineSerialNumberManagement.setType(genuineSerialNumberManagement.getType());
            }
            if(genuineSerialNumberManagement.getSoftId() != null && !"".equals(genuineSerialNumberManagement.getSoftId())){
                genuineSerialNumberManagement.setSoftId(genuineSerialNumberManagement.getSoftId());
            }
            genuineSerialNumberManagementService.save(genuineSerialNumberManagement);
            //自动生成xml
            genuineSerialNumberManagementService.createXML();
            this.msg = RESULT_SUCCESS;
            
            temp = (GenuineSerialNumberManagement)BeanUtils.cloneBean(genuineSerialNumberManagement);
            temp.setId(genuineSerialNumberManagement.getId());
            resultInfo = "{\"msg\":\""+ this.msg +"\","
                    + "\"id\":\"" + temp.getId() +"\","
                    + "\"softDisplayName\":\"" + temp.getSoftDisplayName() +"\","
                    + "\"version\":\"" + temp.getVersion() +"\","
                    + "\"serialNumber\":\"" + temp.getSerialNumber()+"\"}";
            
        } catch (Exception e) {
            log.info("【正版序列号管理--新增序列号:新增失败】");
            this.msg = RESULT_FAIL;
            e.printStackTrace();
        }finally{
            print(resultInfo);
            log.info("【正版序列号管理--新增序列号:结束】");
        }
    }

二、编辑:

  //修改序列号
    function editSerialNumber(obj){
        var type = $(input[name="serialNumber"]:checked).val();
        if(type == 1){
            var rows = $("#inside_tableElement").datagrid(getSelections);
        }else{
            var rows = $("#inside_tableElement_no").datagrid(getSelections);
        }
        if (rows.length < 1) {
            showAlertDialog("信息确认", "请选取记录!");
        } else if (rows.length > 1) {
            showAlertDialog("信息确认", "目前只支持单条记录删除,请去掉多选项!");
        } else {
            showWindow($(obj).attr("title"),"genuineSerialNumberManagementAction_editUI.do?genuineSerialNumberManagement.id="+rows[0].id,"500","320",true);
        }
    }

注意:编辑表单里面需要加上id,并且每行加上value,设置为库里的值,这样编辑的时候不至于为空

<form id="genuineSerialNumberManagementAction_edit">
            <!-- ID -->
            <input id="id" type="hidden" name="genuineSerialNumberManagement.id" value="<s:property value=‘genuineSerialNumberManagement.id‘/>">
            <table id="itemTable" align="center">
                <tr></tr>
                <tr></tr>
                <tr>
                    <td class="rightAlign"><label>软件名:</label></td>
                    <td><input type="text" id="softDisplayName" name="genuineSerialNumberManagement.softDisplayName" 
                    value="<s:property value=‘genuineSerialNumberManagement.softDisplayName‘ />" class="inputText {required:true}" /> <label id=requiredLabel>*</label></td>
                </tr>
                <tr>
                    <td class="rightAlign"><label>版本号:</label></td>
                    <td><input type="text" id="version" name="genuineSerialNumberManagement.version"
                    value="<s:property value=‘genuineSerialNumberManagement.version‘ />" class="inputText {required:true}" /> <label id=requiredLabel>*</label></td>
                </tr>
                <tr>
                    <td class="rightAlign"><label>序列号:</label></td>
                    <td><input type="text" id="serialNumber" name="genuineSerialNumberManagement.serialNumber"
                    value="<s:property value=‘genuineSerialNumberManagement.serialNumber‘ />" class="inputText {required:true}" /> <label id=requiredLabel>*</label></td>
                </tr>
                <tr style="line-height: 60px;">
                    <td colspan="2" align="center">
                        <button class="layerButton" id="buttonSubmit">
                            <span>提交</span>
                        </button>
                        <button class="layerButton" id="closeBT">
                            <span>取消</span>
                        </button>
                    </td>
                </tr>
            </table>
        </form>
$(function(){
        $("#buttonSubmit").click(function(){
            $.doAjax({
                isSend : $("#genuineSerialNumberManagementAction_edit").valid(),
                url : "${basePath}/genuineSerialNumberManagementAction_edit.do",
                //dataType:"json",
                data : $("#genuineSerialNumberManagementAction_edit").serialize(),
                success : function(data) {
                    if (data == success) {
                        getDeskTopObj().editRow( {
                            softDisplayName : $("#softDisplayName").val(),
                            version : $("#version").val(),
                            serialNumber : $("#serialNumber").val()
                         });
                        showMsg("", "修改成功!", true);
                    } else {
                        showMsg("", "修改失败!", false);
                    }
                },
                error : function() {
                    showMsg("", "修改失败!", false);
                }
            });
            return false;
        });
    });

编辑后台方法:通过id去查找到数据,然后把查到的数据更新为页面传过来的数据

  //修改序列号
    public void edit() {
        log.info("【正版序列号管理--修改序列号:开始】");
        this.msg = RESULT_FAIL;
        try {
            this.msg = RESULT_FAIL;
            if (genuineSerialNumberManagement != null) {
                GenuineSerialNumberManagement findGenuineSerialNumberManagement = genuineSerialNumberManagementService.queryById(genuineSerialNumberManagement.getId());
                if (findGenuineSerialNumberManagement != null) {
                    findGenuineSerialNumberManagement.setSoftDisplayName(genuineSerialNumberManagement.getSoftDisplayName());
                    findGenuineSerialNumberManagement.setVersion(genuineSerialNumberManagement.getVersion());
                    findGenuineSerialNumberManagement.setSerialNumber(genuineSerialNumberManagement.getSerialNumber());
                    genuineSerialNumberManagementService.update(findGenuineSerialNumberManagement);
                    //自动生成xml,删除原有的重新生成
                    genuineSerialNumberManagementService.createXML();
                    this.msg = RESULT_SUCCESS;
                }
            }
        } catch (Exception e) {
            this.msg = RESULT_ERROR;
            log.error("【正版序列号管理--修改序列号:修改失败】", e);
        }finally{
            print(this.msg);
            log.info("【正版序列号管理--修改序列号:结束】");
        }
    }

三、删除

  //删除序列号
    function deleteSerialNumber(obj){
        var type = $(input[name="serialNumber"]:checked).val();
        var $table;
        if(type == 1){
            var trIds = getSelectedIds("#inside_tableElement");
            $table = $("#inside_tableElement");
        }else{
            var trIds = getSelectedIds("#inside_tableElement_no");
            $table = $("#inside_tableElement_no");
        }
        if (checkRecordSize(trIds, false)) {
            var title = $(obj).attr("title");
            showConfirmDialog(title, "删除后数据将无法恢复,您确定要删除吗?", function() {
                $.doAjax({
                    url : "${basePath}/genuineSerialNumberManagementAction_delete.do",
                    data : {
                        "selectedIds" : trIds.join(";")
                    },
                    success : function(data) {
                        if (data == success) {
                            deleteRows();
                            showMsg("", "删除成功!", true);
                            $table.datagrid("reload");
                        }else{
                            showMsg("", "删除失败!", false);
                        }
                    },
                    error : function() {
                        showMsg("", "删除失败!", false);
                    }
                });
            });
        }
    }
  //获取选中行的id
    function getSelectedIds(id){
        var ids = [];
        var rows = $(id).datagrid(getSelections);
        for(var i=0;i<rows.length;i++){ 
            ids.push( jQuery.trim( rows[i].id ) );
        }
        return ids;
    }

后台删除方法:

  //删除序列号
    public void delete() {
        log.info("【正版序列号管理--删除序列号:开始】");
        this.msg = RESULT_FAIL;
        try {
            this.msg = RESULT_FAIL;
            Integer[] ids = new Integer[selectedIds.split(";").length];
            if (selectedIds.length() > 0) {
                String[] delIds = new String[selectedIds.split(";").length];//声明一个数组保存要删除的id
                for (int i = 0; i < selectedIds.split(";").length; i++) {
                    delIds[i] = selectedIds.split(";")[i];
                    ids[i] = Integer.parseInt(String.valueOf(delIds[i]));
                }
                genuineSerialNumberManagementService.deleteByIds(ids);
                // 自动生成xml,删除原有的重新生成
                genuineSerialNumberManagementService.createXML();
                this.msg = RESULT_SUCCESS;
            }
        } catch (Exception e) {
            this.msg = RESULT_ERROR;
            log.error("【正版序列号管理--删除序列号:删除失败】", e);
        } finally {
            print(this.msg);
            log.info("【正版序列号管理--删除序列号:结束】");
        }
    }

我们可以看下deleteByIds,继承自BaseService,实现于BaseServiceImpl

  @Override
    public void deleteByIds(Integer[] ids) {
        if (ids != null && ids.length == 1) {
            baseDao.delete(ids[0]);
        } else if (ids != null && ids.length > 0) {
            baseDao.deleteByIds(ids);
        }
    }
  @Override
    public void deleteByIds(Integer[] ids) {
        this.getSession().createQuery(" DELETE " + this.clazz.getName() + " this WHERE this.id IN(:ids) ")//
                .setParameterList("ids", ids)//
                .executeUpdate();
    }

 

金甲防线服务项目笔记:新增、编辑与删除

标签:str   data   sse   edits   exec   16px   equals   selection   获取   

原文地址:http://www.cnblogs.com/goloving/p/7491765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!