2012-09-05 1 views
1

데모 : http://www.ok-soft-gmbh.com/jqGrid/LocalFormEditing.htm JavaScript가 jqGrid의 최신 버전으로 업데이트되면 제대로 작동하지 않습니다.로컬 양식 편집 데모 및 jqGrid 4.4.1

<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/redmond/jquery-ui.css" /> 
<link rel="stylesheet" type="text/css" href="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/css/ui.jqgrid.css" /> 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/i18n/grid.locale-en.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/grid.base.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/grid.common.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/grid.formedit.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/grid.inlinedit.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/grid.celledit.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/grid.subgrid.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/grid.treegrid.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/grid.grouping.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/grid.custom.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/jquery.fmatter.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/jquery.searchFilter.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/grid.jqueryui.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/jqDnR.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/src/jqModal.js"></script> 
<!--<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/js/i18n/grid.locale-en.js"></script> 
<script type="text/javascript" src="http://www.ok-soft-gmbh.com/jqGrid/jquery.jqGrid-4.4.1/js/jquery.jqGrid.min.js"></script>--> 

주요 문제는 레코드를 추가 할 때 TR의 ID가 잘못되었습니다. 이 업데이트가 있습니까?

+0

무슨 뜻입니까? –

+0

새 레코드를 추가하십시오 : http://jsfiddle.net/8k8ct/ 두 번째 페이지로 이동하여 첫 번째 열을보십시오. TR의 ID는 "_empty" – FeKuLa

답변

4

the old answer에서 my old demo의 사용에서 가장 큰 문제는

this.processing = true; 

있는 jqGrid 가장 콜백 함수 호출의 테이블 ($("#list")[0])의 DOM 요소에 이제 this 초기화 코드의 라인입니다. 그래서 위의 라인도 구현해야 다른 작은 변화가 있습니다

options.processing = true; 

에 고정되어야한다. 가장 중요한 것은 포맷터 "date"에서 사용되는 모든 필드를 디코딩 할 $.unformat.date을 호출하는 것입니다.

새로운 데모는 jqGrid 4.4.1을 사용하여 here을 찾을 수 있습니다.

전체 코드는이

var lastSel, 
    mydata = [ 
     {id: "1", invdate: "2007-10-01", name: "test", note: "note", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00"}, 
     {id: "2", invdate: "2007-10-02", name: "test2", note: "note2", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00"}, 
     {id: "3", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00"}, 
     {id: "4", invdate: "2007-10-04", name: "test4", note: "note4", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00"}, 
     {id: "5", invdate: "2007-10-31", name: "test5", note: "note5", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00"}, 
     {id: "6", invdate: "2007-09-06", name: "test6", note: "note6", amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00"}, 
     {id: "7", invdate: "2007-10-04", name: "test7", note: "note7", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00"}, 
     {id: "8", invdate: "2007-10-03", name: "test8", note: "note8", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00"}, 
     {id: "9", invdate: "2007-09-01", name: "test9", note: "note9", amount: "400.00", tax: "30.00", closed: false, ship_via: "TN", total: "430.00"}, 
     {id: "10", invdate: "2007-09-08", name: "test10", note: "note10", amount: "500.00", tax: "30.00", closed: true, ship_via: "TN", total: "530.00"}, 
     {id: "11", invdate: "2007-09-08", name: "test11", note: "note11", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00"}, 
     {id: "12", invdate: "2007-09-10", name: "test12", note: "note12", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.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: true, 
     onclickSubmit: onclickSubmitLocal 
    }, 
    delSettings = { 
     // because I use "local" data I don't want to send the changes to the server 
     // so I use "processing:true" setting and delete the row manually in onclickSubmit 
     onclickSubmit: function (options, rowid) { 
      var $this = $(this), grid_id = $.jgrid.jqID(this.id), grid_p = this.p, 
       newPage = grid_p.page; 

      // reset the value of processing option to true to 
      // skip the ajax request to 'clientArray'. 
      options.processing = true; 

      // delete the row 
      if (grid_p.treeGrid) { 
       $this.jqGrid("delTreeNode", rowid); 
      } else { 
       $this.jqGrid("delRowData", rowid); 
      } 
      $.jgrid.hideModal("#delmod" + grid_id, { 
       gb: "#gbox_" + grid_id, 
       jqm: options.jqModal, 
       onClose: options.onClose 
      }); 

      if (grid_p.lastpage > 1) {// on the multipage grid reload the grid 
       if (grid_p.reccount === 0 && newPage === grid_p.lastpage) { 
        // if after deliting there are no rows on the current page 
        // which is the last page of the grid 
        newPage--; // go to the previous page 
       } 
       // reload grid to make the row from the next page visable. 
       $this.trigger("reloadGrid", [{page: newPage}]); 
      } 

      return true; 
     }, 
     processing: true 
    }, 
    initDateEdit = function (elem) { 
     setTimeout(function() { 
      $(elem).datepicker({ 
       dateFormat: 'dd-M-yy', 
       //autoSize: true, 
       showOn: 'button', 
       changeYear: true, 
       changeMonth: true, 
       showButtonPanel: true, 
       showWeek: true 
      }); 
     }, 100); 
    }, 
    initDateSearch = function (elem) { 
     setTimeout(function() { 
      $(elem).datepicker({ 
       dateFormat: 'dd-M-yy', 
       //autoSize: true, 
       //showOn: 'button', // it dosn't work in searching dialog 
       changeYear: true, 
       changeMonth: true, 
       showButtonPanel: true, 
       showWeek: true 
      }); 
     }, 100); 
    }; 

grid.jqGrid({ 
    datatype: 'local', 
    data: mydata, 
    colNames: [/*'Inv No', */'Client', 'Date', 'Amount', 'Tax', 'Total', 'Closed', 'Shipped via', 'Notes'], 
    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: 'invdate', index: 'invdate', width: 80, align: 'center', sorttype: 'date', 
      formatter: 'date', formatoptions: {newformat: 'd-M-Y'}, editable: true, datefmt: 'd-M-Y', 
      editoptions: {dataInit: initDateEdit, size: 14}, 
      searchoptions: {dataInit: initDateSearch}}, 
     {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'}, 
     {name: 'closed', index: 'closed', width: 70, align: 'center', editable: true, formatter: 'checkbox', 
      edittype: 'checkbox', editoptions: {value: 'Yes:No', defaultValue: 'Yes'}, 
      stype: 'select', searchoptions: {sopt: ['eq', 'ne'], value: ':All;true:Yes;false:No'}}, 
     {name: 'ship_via', index: 'ship_via', width: 100, align: 'center', editable: true, formatter: 'select', 
      edittype: 'select', editoptions: {value: 'FE:FedEx;TN:TNT;IN:Intim', defaultValue: 'Intime'}, 
      stype: 'select', searchoptions: {value: ':All;FE:FedEx;TN:TNT;IN:Intim'}}, 
     {name: 'note', index: 'note', width: 60, sortable: false, editable: true, edittype: 'textarea'} 
    ], 
    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(); 
     }}); 

P.S.를 확인할 수 있습니다 TreeGrid를 사용하여 코드를 테스트하지는 않았지만 제대로 작동해야합니다. 아마 "날짜"포맷터가있는 열에 $.unformat.date이라는 동일한 호출을해야 할 것입니다.

+0

완벽하게 작동합니다. 약간의 수정 삭제 : if (grid_p.treeGrid === true) { $ this.jqGrid ("delTreeNode", rowid); } else { $ this.jqGrid ("delRowData", rowid); } – FeKuLa

+0

@FeKuLa : 동의합니다. 감사합니다. 데모와 답변에서 코드를 수정했습니다. 그런데 질문을 끝내라는 대답을 [받아 들일 수] 있습니다 (http://meta.stackexchange.com/a/5235/147495). – Oleg