2011-04-12 49 views
2

我傾注了每個問題在本網站上詢問有關填充jqGrid摘要頁腳沒有用戶數據是服務器請求。和jqGrid維基資源,只是找不到答案。這可能嗎?jqGrid摘要頁腳可以填充沒有userdata服務器請求?

我以許多典型的方式使用jqGrid作爲我的管理門戶的一部分,但有一個特定的用途,我希望外觀&覺得jqGrid最初是一個空UI容器,用於某些用戶交互,一種不會在提交上發佈的表單(感謝下面Oleg的偉大腳本),但只是添加行並使用新添加行中的值更新摘要頁腳。在grid選項中,我有'summary':colModel中的「sum」,「grouping:true」,footerrow:true,userDataOnFooter:true,altRows:true,但除了由本地數據字符串中的userdata提供的初始值總結頁腳值從不改變。似乎沒有人想要觸及這個主題。是否因爲jqGrid的主要特性是數據庫驅動功能?我在它的數據庫驅動的立場中使用了大約15個jqGrid實例(其中許多現在位於生產服務中),但爲了一致性(我所有的UI都在jqTabs內)需要使用它,最初作爲客戶端UI,沒有服務器請求(所有的東西都會在以後保存到db),但是我只是拉着我的頭髮試圖在客戶端和客戶端上編程操作彙總頁腳。任何人都可以建議如何增加一個新的行添加新行的值,其中的值將與任何現有行彙總,哪些不涉及任何發佈到服務器,只是在網格內更新?

提供的代碼很長,主要基於用戶Oleg的解決方案,用於從模式表單添加一行而不發佈到服務器。我已經將本地數組數據更改爲JSON字符串,只是爲了更好地理解符號,因爲我習慣於使用xml。 jsonstring使用一個默認行初始化網格供用戶編輯。我遺漏了jsonReader,因爲網格不會與它一起渲染。

簡而言之,我想要做的是在將新行添加到網格時(不會發布到此時發生的服務器)或編輯或刪除,更新摘要頁腳。當達到某一組值時,用戶會看到一個顯示的按鈕,將行數據保存到數據庫。

var lastSel, mydata = { "total": 1, "page": 1, "records": 1, "rows": [{ "id": acmid, "cell": ["0.00", "0.00", "0.00", "0.00"]}], "userdata":{ "mf": 0.00, "af":0.00,"pf":0.00,"cf":0.00 }} 

grid = $("#ta_form_d"), 
    onclickSubmitLocal = function (options, postdata) { 

     var grid_p = grid[0].p, 
      idname = grid_p.prmNames.id, 
      grid_id = grid[0].id, 
      id_in_postdata = grid_id + "_id", 
      rowid = postdata[id_in_postdata], 
      addMode = rowid === "_empty", 
      oldValueOfSortColumn; 

     // postdata has row id property with another name. we fix it: 
     if (addMode) { 
      // generate new id 
      var new_id = grid_p.records + 1; 
      while ($("#" + new_id).length !== 0) { 
       new_id++; 
      } 
      postdata[idname] = String(new_id); 
      //alert(postdata[idname]); 
     } 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) { 
       var 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); 
     //alert(oldValueOfSortColumn); 
     // save the data in the grid 
     if (grid_p.treeGrid === true) { 
      if (addMode) { 
       grid.jqGrid("addChildNode", rowid, grid_p.selrow, postdata); 
      } else { 
       grid.jqGrid("setTreeRow", rowid, postdata); 
      } 
     } else { 

      if (addMode) { 
       grid.jqGrid("addRowData", rowid, postdata, options.addedrow); 


      } else { 
       grid.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() { 
       grid.trigger("reloadGrid", [{ current: true}]); 
      }, 100); 
     } 

     // !!! the most important step: skip ajax request to the server 
     this.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 grid_id = grid[0].id, 
       grid_p = grid[0].p, 
       newPage = grid[0].p.page; 

      // delete the row 
      grid.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. 
       grid.trigger("reloadGrid", [{ page: newPage}]); 
      } 

      return true; 
     }, 
     processing: true 
    };   //, 
    /*initDateEdit = 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 
      }); 
      //$(elem).focus(); 
     }, 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 
      }); 
      //$(elem).focus(); 
     }, 100); 
    };*/ 

    //jQuery("#ta_form_d").jqGrid({ 
    grid.jqGrid({ 
     // url:'/admin/tg_cma/ta_allocations.asp?acmid=' + acmid + '&mid=' + merchantid + '&approval_code=' + approval_code, 
     //datatype: "local", 
     //data: mydata, 
     datatype: 'jsonstring', 
     datastr: mydata, 

     colNames: ['ID', 'Monthly Fees', 'ATM Fees', 'POS Fees', 'Card to Card Fees'], 
     colModel: [ 
     { name: 'id', index: 'id', width: 90, align: "center", editable: true, editoptions: { size: 25 }, formoptions: { rowpos: 1, colpos: 1, label: "EzyAccount ID", elmprefix: "(*) " }, editrules: { required: true} }, 
     { name: 'mf', index: 'mf', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 2, colpos: 1, label: "Monthly Fee", elmprefix: "(*) " }, editrules: { required: true} }, 
     { name: 'af', index: 'af', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 3, colpos: 1, label: "ATM Fee", elmprefix: "(*) " }, editrules: { required: true} }, 
     { name: 'pf', index: 'pf', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 4, colpos: 1, label: "POS Fee", elmprefix: "(*) " }, editrules: { required: true} }, 
     { name: 'cf', index: 'cf', width: 130, align: "right", formatter: 'number', editable: true, summaryType: 'sum', editoptions: { size: 25 }, formoptions: { rowpos: 5, colpos: 1, label: "Card to Card Fee", elmprefix: "(*) " }, editrules: { required: true} } 
    ], 
     rowNum: 5, 
     rowList: [5, 10, 20], 
     pager: '#pta_form_d', 
     toolbar: [true, "top"], 
     width: 500, 
     height: 100, 
     editurl: 'clientArray', 
     sortname: 'id', 
     viewrecords: true, 
     sortorder: "asc", 
     multiselect: false, 
     cellEdit: false, 
     caption: "Allocations", 
     grouping: true, 
     /*groupingView: { 
      groupField: ['id', 'mf', 'af', 'pf', 'cf'], 
      groupColumnShow: [true], 
      groupText: ['<b>{0}</b>'], 
      groupCollapse: false, 
      groupOrder: ['asc'], 
      groupSummary: [true], 
      groupDataSorted: true 
     },*/ 
     footerrow: true, 
     userDataOnFooter: true, 
     altRows: true, 
     ondblClickRow: function (rowid, ri, ci) { 
      var p = grid[0].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" 
       grid.jqGrid('setSelection', rowid); 
      } 
      grid.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") { 
        grid.jqGrid('restoreRow', lastSel); 
       } 
       lastSel = id; 
      } 
     }, 
     afterEditCell: function (rowid, cellname, value, iRow, iCol) { 
      alert(iCol); 
     }, 
     gridComplete: function() { 

     }, 
     loadComplete: function (data) { 


     } 

    }) 
.jqGrid('navGrid', '#pta_form_d', {}, 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(); 
       } 
      }); 

$("#t_ta_form_d").append("<input type='button' class='add' value='Add New Allocation' style='height:20px; color:green; font-size:11px;' />"); 
$("input.add", "#t_ta_form_d").click(function() { 
    jQuery("#ta_form_d").jqGrid('editGridRow', "new", { 
     //recreateForm:true, 
     jqModal: false, 
     reloadAfterSubmit: false, 
     savekey: [true, 13], 
     closeOnEscape: true, 
     closeAfterAdd: true, 
     onclickSubmit: onclickSubmitLocal 


    }) 
}) 
+0

參考關於用戶數據的Stackoverflow用戶@Oleg(http://stackoverflow.com/questions/3128837/json-and-jqgrid-what-is-user-data)這是說:_A標準的JSON閱讀器是定義如此,它從所發送數據的根讀取數據userdata屬性並保存。這個數據可以通過以下方式訪問:_' var myUserData = jQuery(「grid_id」)。getGridParam('userData');'Question然後'userdata'可以在jsonReader初始讀取之後被修改和檢索(在本例中使用json ),如果是這樣,那麼本地修改的'userdata'如何顯示在摘要頁腳? – 2011-04-12 07:36:15

回答

9

在「本地」的數據類型的情況下,可以在頁腳使用footerData方法來設置(或獲取)的數據。此外,方法getCol可以用來計算列中元素的總和。

查看the answer瞭解更多信息。我希望它能解決你的問題。

+0

目前,這已經解決了問題,並提供了我一直試圖實現的行爲。非常感謝您對此的及時關注,您在這方面的知識令人印象深刻。哦,謝謝你挽救我的婚姻。 – 2011-04-12 09:30:11

+0

@Jerry Of Perth:不客氣! – Oleg 2011-04-12 09:35:16