2012-03-25 62 views
0

我使用自定義的格式顯示其編輯cell.if我選擇單元格,選擇任何其他細胞的細胞數據選擇的是細胞正常工作,電池的數據是越來越消失等單元格變得不可編輯。如果我使用unformatter,它也不起作用。爲可編輯單元格自定義格式是不是在jqGrid的

我的代碼是:

jQuery("#tree").jqGrid({ 
    url:'json/jsonSamplePots.json', 
    datatype: "json", 
    mtype:'GET', 
    colNames: ["id", "no.", "name"], 
    colModel: [ 
     {name:'id',width: 30, editable:false, align:"right",sortable:false, hidden: true, key: true}, 
     {name:'no',width:80, editable:false, align:"left", sortable:true, sorttype:"int"}, 
     {name:'name', width:150, editable:true, sortable:true, sorttype:"text",formatter:resourceFormatter}, 
], 
    rowNum:10, 
    rowList:[10,20,30], 
    treeGridModel:'adjacency', 
    treeGrid: true, 
    cellEdit: true, 
    ExpandColumn:'name', 
    cellsubmit : 'clientArray'}); 

resourceFormatter=function(cellvalue, options, rowObject) 
{ 
var strResources=''; 
if(null != rowObject.name) 
{ 
    $.each(rowObject.name,function(i,Assignment) 
    { 
     if(Assignment) 
     { 
      for(i=0;i<Assignment.length;i++) 
      { 
       if(i!=0) 
       { 
        strResources=strResources+","; 
       } 

       strResources=strResources+Assignment[i].employeeName+'['+Assignment[i].assignPercent+']'; 
      } 
     } 

    }); 
} 
return strResources;} 

我的JSON是::

{ 
"list": [ 
    { 
     "id": 16731, 
     "no": "1", 
     "name": { 
      "resources": [ 
       { 
        "employeeID": 103, 
        "employeeName": "Gowri", 
        "assignPercent": 100 
       }, 
       { 
        "employeeID": 108, 
        "employeeName": "Paul", 
        "assignPercent": 50 
       }, 
       { 
        "employeeID": 111, 
        "employeeName": "Sarfaraz", 
        "assignPercent": 50.5 
       } 
      ] 
     } 
    } 
]} 
+0

JSON數據似乎是錯誤的。 'list'屬性必須被引用:''list「'。該部分'「名」:「資源」:'也是錯的,如果你使用的命名屬性'resources'你可以做這個對象裏面只有那麼它應該像'「名」:{「資源」:'或' 「名」:[{ 「資源」:'。你可以發佈固定的JSON數據,然後我會回答你的主要問題?我建議你驗證JSON數據[這裏](http://jsonlint.com/)。 – Oleg 2012-03-25 14:42:44

+0

@ Oleg:謝謝你的回覆。我修改了Json數據,併發布了正確的JSON。 – lakshmi 2012-03-26 04:30:30

回答

2

在你的情況下,選擇custom formatter看起來我錯了。問題在於,自定義格式化程序不僅在初始網格加載期間被調用,而且可以在以後調用。所以,在我看來的jsonmap使用越好:

{name: 'name', width: 250, editable: true, 
    jsonmap: function (obj) { 
     var prop, name = obj.name, assignment, resource, values = [], i, n; 
     for (prop in name) { 
      if (name.hasOwnProperty(prop)) { 
       assignment = name[prop]; 
       if ($.isArray(assignment)) { 
        for (i = 0, n = assignment.length; i < n; i++) { 
         resource = assignment[i]; 
         values.push(resource.employeeName + '[' + 
          resource.assignPercent + ']'); 
        } 
       } 
      } 
     } 
     return values.join(', '); 
    }} 

你必須定義jsonReader太:

jsonReader: { 
    repeatitems: false, 
    root: "list" 
} 

此外,它填補的TreeGrid特定的屬性是非常重要的。你可以填寫beforeProcessing回調內的部分屬性。在下面的例子中我填寫所有的TreeGrid特殊性能beforeProcessing

beforeProcessing: function (data) { 
    var i, list = data.list, n, item; 
    if ($.isArray(list)) { 
     for (i = 0, n = list.length; i < n; i++) { 
      item = list[i]; 
      if (typeof item.level === "undefined") { item.level = 0; } 
      if (typeof item.parent === "undefined") { item.parent = null; } 
      if (typeof item.isLeaf === "undefined") { item.isLeaf = false; } 
      if (typeof item.expanded === "undefined") { item.expanded = false; } 
      if (typeof item.loaded === "undefined") { item.loaded = true; } 
     } 
    } 
} 

修改後的演示中,你發現here

enter image description here

修訂:我在演示改變了當地cell editing到當地inline editing因爲單元格編輯不支持使用的TreeGrid工作。

+0

:謝謝你的解決方案。它的做工精細現在:) – lakshmi 2012-03-26 07:43:58

+0

@lakshmi:歡迎您!順便說一句,你有投票權高達約30問題或解答**每天**(見[這裏](http://meta.stackexchange.com/a/5213/147495))。投票的主要目標是*幫助其他*尋找常見問題的解決方案。搜索引擎集中使用投票。所以我建議你使用權利來幫助其他訪問者的計算器。 – Oleg 2012-03-26 07:48:35

0

您正在使用的JSON有幾個錯誤。嘗試下面。

{"list":{"id":16731,"no":"1","name":"resources","employeeID":116,"employeeName":"lakshmi","assignPercent":50.5},"employeeID":118,"employeeName":"abc","assignPercent":50.5} 
相關問題