2013-04-12 2 views
0

현재 폼을 열린 상태로 유지하면서 [Enter 키]를 사용하여 그리드에 레코드를 저장하는 작업을 수행했습니다. jqgrid wikiDoc에 대한 이해는 clearAfterAdd가 다음 레코드의 항목에 대한 모달 형식의 필드를 지우지 만이 이벤트는 작동하지 않습니다. 누구나 통찰력을 발휘할 수 있습니까? 여기에 내 코드가있다.clearAfterAdd 이벤트가 작동하지 않습니다.

 var lastSel, 
      mydata = [ 
       { id: "1", invdate: "2007-10-01", name: "test", amount: "200.00", tax: "10.00", total: "210.00" } 
      ], 
      grid = $("#list"), 
      getColumnIndex = function (columnName) { 
       var cm = $(this).jqGrid('getGridParam', 'colModel'), i, l = cm.length; 
       for (i = 0; i < l; i++) { 
        if ((cm[i].index || cm[i].name) === columnName) { 
         return i; // return the colModel index 
        } 
       } 
       return -1; 
      }, 
      onclickSubmitLocal = function (options, postdata) { 
       var $this = $(this), grid_p = this.p, 
        idname = grid_p.prmNames.id, 
        grid_id = this.id, 
        id_in_postdata = grid_id + "_id", 
        rowid = postdata[id_in_postdata], 
        addMode = rowid === "_empty", 
        oldValueOfSortColumn, 
        new_id, 
        tr_par_id, 
        colModel = grid_p.colModel, 
        cmName, 
        iCol, 
        cm; 

       // postdata has row id property with another name. we fix it: 
       if (addMode) { 
        // generate new id 
        new_id = $.jgrid.randId(); 
        while ($("#" + new_id).length !== 0) { 
         new_id = $.jgrid.randId(); 
        } 
        postdata[idname] = String(new_id); 
       } else if (typeof postdata[idname] === "undefined") { 
        // set id property only if the property not exist 
        postdata[idname] = rowid; 
       } 
       delete postdata[id_in_postdata]; 

       // prepare postdata for tree grid 
       if (grid_p.treeGrid === true) { 
        if (addMode) { 
         tr_par_id = grid_p.treeGridModel === 'adjacency' ? grid_p.treeReader.parent_id_field : 'parent_id'; 
         postdata[tr_par_id] = grid_p.selrow; 
        } 

        $.each(grid_p.treeReader, function (i) { 
         if (postdata.hasOwnProperty(this)) { 
          delete postdata[this]; 
         } 
        }); 
       } 

       // decode data if there encoded with autoencode 
       if (grid_p.autoencode) { 
        $.each(postdata, function (n, v) { 
         postdata[n] = $.jgrid.htmlDecode(v); // TODO: some columns could be skipped 
        }); 
       } 

       // save old value from the sorted column 
       oldValueOfSortColumn = grid_p.sortname === "" ? undefined : grid.jqGrid('getCell', rowid, grid_p.sortname); 

       // save the data in the grid 
       if (grid_p.treeGrid === true) { 
        if (addMode) { 
         $this.jqGrid("addChildNode", new_id, grid_p.selrow, postdata); 
        } else { 
         $this.jqGrid("setTreeRow", rowid, postdata); 
        } 
       } else { 
        if (addMode) { 
         // we need unformat all date fields before calling of addRowData 
         for (cmName in postdata) { 
          if (postdata.hasOwnProperty(cmName)) { 
           iCol = getColumnIndex.call(this, cmName); 
           if (iCol >= 0) { 
            cm = colModel[iCol]; 
            if (cm && cm.formatter === "date") { 
             postdata[cmName] = $.unformat.date.call(this, postdata[cmName], cm); 
            } 
           } 
          } 
         } 
         $this.jqGrid("addRowData", new_id, postdata, options.addedrow); 
        } else { 
         $this.jqGrid("setRowData", rowid, postdata); 
        } 
       } 

       if ((addMode && options.closeAfterAdd) || (!addMode && options.closeAfterEdit)) { 
        // close the edit/add dialog 
        $.jgrid.hideModal("#editmod" + grid_id, { 
         gb: "#gbox_" + grid_id, 
         jqm: options.jqModal, 
         onClose: options.onClose 
        }); 
       } 

       if (postdata[grid_p.sortname] !== oldValueOfSortColumn) { 
        // if the data are changed in the column by which are currently sorted 
        // we need resort the grid 
        setTimeout(function() { 
         $this.trigger("reloadGrid", [{ current: true }]); 
        }, 100); 
       } 

       // !!! the most important step: skip ajax request to the server 
       options.processing = true; 
       return {}; 
      }, 
      editSettings = { 
       //recreateForm: true, 
       jqModal: false, 
       reloadAfterSubmit: false, 
       closeOnEscape: true, 
       savekey: [true, 13], 
       closeAfterEdit: true, 
       onclickSubmit: onclickSubmitLocal 
      }, 
      addSettings = { 
       recreateForm: true, 
       jqModal: false, 
       reloadAfterSubmit: false, 
       savekey: [true, 13], 
       closeOnEscape: true, 
       closeAfterAdd: false, 
       clearAfterAdd: true, 
       onclickSubmit: onclickSubmitLocal 
      } 
       processing: true 
      } 
      }; 

     grid.jqGrid({ 
      datatype: 'local', 
      data: mydata, 
      colNames: [/*'Inv No', */'Client', 'Amount', 'Tax', 'Total'], 
      colModel: [ 
       //{name: 'id', width: 70, align: 'center', sorttype: 'int', searchoptions: {sopt: ['eq', 'ne']}}, 
       {name: 'name', index: 'name', editable: true, width: 60, editrules: {required: true}}, 
       {name: 'amount', index: 'amount', width: 70, formatter: 'number', editable: true, align: 'right'}, 
       {name: 'tax', index: 'tax', width: 50, formatter: 'number', editable: true, align: 'right'}, 
       {name: 'total', index: 'total', width: 60, formatter: 'number', editable: true, align: 'right'} 
      ], 
      rowNum: 10, 
      rowList: [5, 10, 20], 
      pager: '#pager', 
      gridview: true, 
      rownumbers: true, 
      autoencode: true, 
      ignoreCase: true, 
      sortname: 'invdate', 
      viewrecords: true, 
      sortorder: 'desc', 
      caption: 'How to implement local form editing', 
      height: '100%', 
      editurl: 'clientArray', 
      ondblClickRow: function (rowid, ri, ci) { 
       var $this = $(this), p = this.p; 
       if (p.selrow !== rowid) { 
        // prevent the row from be unselected on double-click 
        // the implementation is for "multiselect:false" which we use, 
        // but one can easy modify the code for "multiselect:true" 
        $this.jqGrid('setSelection', rowid); 
       } 
       $this.jqGrid('editGridRow', rowid, editSettings); 
      }, 
      onSelectRow: function (id) { 
       if (id && id !== lastSel) { 
        // cancel editing of the previous selected row if it was in editing state. 
        // jqGrid hold intern savedRow array inside of jqGrid object, 
        // so it is safe to call restoreRow method with any id parameter 
        // if jqGrid not in editing state 
        if (typeof lastSel !== "undefined") { 
         $(this).jqGrid('restoreRow', lastSel); 
        } 
        lastSel = id; 
       } 
      } 
     }).jqGrid('navGrid', '#pager', {}, editSettings, addSettings, delSettings, 
      {multipleSearch: true, overlay: false, 
       onClose: function (form) { 
        // if we close the search dialog during the datapicker are opened 
        // the datepicker will stay opened. To fix this we have to hide 
        // the div used by datepicker 
        $("div#ui-datepicker-div.ui-datepicker").hide(); 
       }}); 
+0

사람들이 너무 많은 코드를 파고 드는 것을 기대하지 않습니까? jsFiddle 샘플을 게시하거나 코드를 줄이십시오. –

답변

0

당신은 내가 herehere를 게시 한 코드를 사용합니다. 이전 질문에 대한 답변에서 jqGrid는 양식 편집 모드를 사용할 경우 로컬 데이터 편집을 지원하지 않는다고 썼습니다. 내가 게시 한 코드는 로컬 데이터로 양식 편집의 대부분의 기능을 사용할 수있는 해결 방법을 구현하려는 시도였습니다. onclickSubmitLocal 코드는 complete 이벤트 처리기 코드 editGridRow (here 참조)을 기반으로 작성되었습니다. 당신은 코드의 the part를 검사하면 옵션 closeAfterAdd: false, clearAfterAdd: true의 조합 것을 볼 것이다 내부 기능 fillData (the line 참조) 나는 부분을 제거해야 코드를 썼던 것처럼 때문에,

fillData("_empty",$t,frmgr); 

를 호출됩니다 fillData 외부에서 액세스 할 수 없습니다.

afterComplete 콜백을 사용하면 입력 양식의 모든 입력란을 기본값으로 재설정 할 수 있습니다. 수행해야하는 공통 코드는 here이지만 모든 특정 사례에서 입력 된 내용을 채우는 것이 훨씬 쉬울 것입니다.

관련 문제