2011-03-08 81 views
0

你好我採取的jqGrid與後端返回動態數據,如這裏討論我下面一個類似的解決方案填充數據,我嘗試了無數不同的方法,但無法享受。下面是代碼:的jqGrid與dynmaic數據頭部疼痛

 $.ajax({ 
    type: "GET", 
    url: "http://cw-epjdp-d01.tm-gnet.com:10040/wps/PA_Resource_Manager_1/JSONMovementServlet", 
    dataType: "json", 
    success: function(result){ 
      var colD = result; 
      //console.log(colD); 
      var colN = result.MOVEMENTS.columnNames; 
      var colM = result.MOVEMENTS.colModelList; 
      console.log(colN); 
     $("#movementTable").jqGrid({ 
      datatype: 'local', 
      data: colD.MOVEMENTS.rows, 
      colNames: colN, 
      colModel :colM, 
      height: "auto", 
      loadComplete: function(data){ 
       console.log("data loaded..."); 
      }, 
      loadError: function(xhr,status,error){ 
       alert('error'); 
      } 
     }); 
    }, 
    error: function(x, e){ 
     alert(x.readyState + " "+ x.status +" "+ e.msg); 
    } 
}); 

下面是JSON的樣品返回:

{ 
"MOVEMENTS": { 
    "currpage": "1", 
    "totalpages": "3", 
    "totalrecords": "8", 
    "rows": [ 
     { 
      "id": "uid\u003dAshokPandey", 
      "cells": [ 
       { 
        "NAME": "uid\u003dAshokPandey", 
        "DAY1": "Canary Wharf", 
        "DAY2": "Holiday", 
        "DAY3": "Holiday", 
        "DAY4": "Holiday", 
        "DAY5": "Holiday", 
        "DAY6": "Holiday", 
        "DAY7": "Holiday", 
        "DAY8": "Holiday", 
        "DAY9": "Holiday", 
        "DAY10": "Holiday", 
        "DAY11": "Holiday", 
        "DAY12": "Holiday", 
        "DAY13": "Holiday", 
        "DAY14": "Holiday" 
       } 
      ] 
     }, 
     { 
      "id": "uid\u003dGarethHarris", 
      "cells": [ 
       { 
        "NAME": "uid\u003dGarethHarris", 
        "DAY1": "Canary Wharf", 
        "DAY2": "Canary Wharf", 
        "DAY3": "Canary Wharf", 
        "DAY4": "Canary Wharf", 
        "DAY5": "Canary Wharf", 
        "DAY6": "Canary Wharf", 
        "DAY7": "Canary Wharf", 
        "DAY8": "Canary Wharf", 
        "DAY9": "Canary Wharf", 
        "DAY10": "Canary Wharf", 
        "DAY11": "Canary Wharf", 
        "DAY12": "Canary Wharf", 
        "DAY13": "Canary Wharf", 
        "DAY14": "Canary Wharf" 
       } 
      ] 
     }, 
     { 
      "id": "uid\u003dKeithPriddle", 
      "cells": [ 
       { 
        "NAME": "uid\u003dKeithPriddle", 
        "DAY1": "Canary Wharf", 
        "DAY2": "Canary Wharf", 
        "DAY3": "Canary Wharf", 
        "DAY4": "Canary Wharf", 
        "DAY5": "Canary Wharf", 
        "DAY6": "Canary Wharf", 
        "DAY7": "Canary Wharf", 
        "DAY8": "Canary Wharf", 
        "DAY9": "Canary Wharf", 
        "DAY10": "Canary Wharf", 
        "DAY11": "Canary Wharf", 
        "DAY12": "Canary Wharf", 
        "DAY13": "Canary Wharf", 
        "DAY14": "Canary Wharf" 
       } 
      ] 
     }, 
     { 
      "id": "uid\u003dJonathanPopoola", 
      "cells": [ 
       { 
        "NAME": "uid\u003dJonathanPopoola", 
        "DAY1": "Canary Wharf", 
        "DAY2": "Sickness", 
        "DAY3": "Sickness", 
        "DAY4": "Sickness", 
        "DAY5": "Sickness", 
        "DAY6": "Sickness", 
        "DAY7": "Sickness", 
        "DAY8": "Sickness", 
        "DAY9": "Sickness", 
        "DAY10": "Sickness", 
        "DAY11": "Sickness", 
        "DAY12": "Sickness", 
        "DAY13": "Sickness", 
        "DAY14": "Sickness" 
       } 
      ] 
     }, 
     { 
      "id": "uid\u003dNaveedZaib", 
      "cells": [ 
       { 
        "NAME": "uid\u003dNaveedZaib", 
        "DAY1": "Canary Wharf", 
        "DAY2": "Sickness", 
        "DAY3": "Sickness", 
        "DAY4": "Sickness", 
        "DAY5": "Sickness", 
        "DAY6": "Sickness", 
        "DAY7": "Sickness", 
        "DAY8": "Sickness", 
        "DAY9": "Sickness", 
        "DAY10": "Sickness", 
        "DAY11": "Sickness", 
        "DAY12": "Sickness", 
        "DAY13": "Sickness", 
        "DAY14": "Sickness" 
       } 
      ] 
     }, 
     { 
      "id": "uid\u003dPooyanFarahani", 
      "cells": [ 
       { 
        "NAME": "uid\u003dPooyanFarahani", 
        "DAY1": "Canary Wharf", 
        "DAY2": "Canary Wharf", 
        "DAY3": "Canary Wharf", 
        "DAY4": "Canary Wharf", 
        "DAY5": "Canary Wharf", 
        "DAY6": "Canary Wharf", 
        "DAY7": "Canary Wharf", 
        "DAY8": "Canary Wharf", 
        "DAY9": "Canary Wharf", 
        "DAY10": "Canary Wharf", 
        "DAY11": "Canary Wharf", 
        "DAY12": "Canary Wharf", 
        "DAY13": "Canary Wharf", 
        "DAY14": "Canary Wharf" 
       } 
      ] 
     }, 
     { 
      "id": "uid\u003dWendyBuchta", 
      "cells": [ 
       { 
        "NAME": "uid\u003dWendyBuchta", 
        "DAY1": "Canary Wharf", 
        "DAY2": "AWOL", 
        "DAY3": "AWOL", 
        "DAY4": "AWOL", 
        "DAY5": "AWOL", 
        "DAY6": "AWOL", 
        "DAY7": "AWOL", 
        "DAY8": "AWOL", 
        "DAY9": "AWOL", 
        "DAY10": "AWOL", 
        "DAY11": "AWOL", 
        "DAY12": "AWOL", 
        "DAY13": "AWOL", 
        "DAY14": "AWOL" 
       } 
      ] 
     }, 
     { 
      "id": "uid\u003dEdwardRios", 
      "cells": [ 
       { 
        "NAME": "uid\u003dEdwardRios", 
        "DAY1": "Canary Wharf", 
        "DAY2": "Canary Wharf", 
        "DAY3": "Canary Wharf", 
        "DAY4": "Canary Wharf", 
        "DAY5": "Canary Wharf", 
        "DAY6": "Canary Wharf", 
        "DAY7": "Canary Wharf", 
        "DAY8": "Canary Wharf", 
        "DAY9": "Canary Wharf", 
        "DAY10": "Canary Wharf", 
        "DAY11": "Canary Wharf", 
        "DAY12": "Canary Wharf", 
        "DAY13": "Canary Wharf", 
        "DAY14": "Canary Wharf" 
       } 
      ] 
     } 
    ], 
    "colModelList": [ 
     { 
      "INDEX": "NAME", 
      "LABEL": "NAME", 
      "WIDTH": "90", 
      "NAME": "NAME", 
      "JSONMAP": "CELLS.0.NAME" 
     }, 
     { 
      "INDEX": "Mon Jan 31", 
      "LABEL": "Mon Jan 31", 
      "WIDTH": "90", 
      "NAME": "Mon Jan 31", 
      "JSONMAP": "CELLS.0.Mon Jan 31" 
     }, 
     { 
      "INDEX": "Tue Feb 01", 
      "LABEL": "Tue Feb 01", 
      "WIDTH": "90", 
      "NAME": "Tue Feb 01", 
      "JSONMAP": "CELLS.0.Tue Feb 01" 
     }, 
     { 
      "INDEX": "Wed Feb 02", 
      "LABEL": "Wed Feb 02", 
      "WIDTH": "90", 
      "NAME": "Wed Feb 02", 
      "JSONMAP": "CELLS.0.Wed Feb 02" 
     }, 
     { 
      "INDEX": "Thu Feb 03", 
      "LABEL": "Thu Feb 03", 
      "WIDTH": "90", 
      "NAME": "Thu Feb 03", 
      "JSONMAP": "CELLS.0.Thu Feb 03" 
     }, 
     { 
      "INDEX": "Fri Feb 04", 
      "LABEL": "Fri Feb 04", 
      "WIDTH": "90", 
      "NAME": "Fri Feb 04", 
      "JSONMAP": "CELLS.0.Fri Feb 04" 
     }, 
     { 
      "INDEX": "Sat Feb 05", 
      "LABEL": "Sat Feb 05", 
      "WIDTH": "90", 
      "NAME": "Sat Feb 05", 
      "JSONMAP": "CELLS.0.Sat Feb 05" 
     }, 
     { 
      "INDEX": "Sun Feb 06", 
      "LABEL": "Sun Feb 06", 
      "WIDTH": "90", 
      "NAME": "Sun Feb 06", 
      "JSONMAP": "CELLS.0.Sun Feb 06" 
     }, 
     { 
      "INDEX": "Mon Feb 07", 
      "LABEL": "Mon Feb 07", 
      "WIDTH": "90", 
      "NAME": "Mon Feb 07", 
      "JSONMAP": "CELLS.0.Mon Feb 07" 
     }, 
     { 
      "INDEX": "Tue Feb 08", 
      "LABEL": "Tue Feb 08", 
      "WIDTH": "90", 
      "NAME": "Tue Feb 08", 
      "JSONMAP": "CELLS.0.Tue Feb 08" 
     }, 
     { 
      "INDEX": "Wed Feb 09", 
      "LABEL": "Wed Feb 09", 
      "WIDTH": "90", 
      "NAME": "Wed Feb 09", 
      "JSONMAP": "CELLS.0.Wed Feb 09" 
     }, 
     { 
      "INDEX": "Thu Feb 10", 
      "LABEL": "Thu Feb 10", 
      "WIDTH": "90", 
      "NAME": "Thu Feb 10", 
      "JSONMAP": "CELLS.0.Thu Feb 10" 
     }, 
     { 
      "INDEX": "Fri Feb 11", 
      "LABEL": "Fri Feb 11", 
      "WIDTH": "90", 
      "NAME": "Fri Feb 11", 
      "JSONMAP": "CELLS.0.Fri Feb 11" 
     }, 
     { 
      "INDEX": "Sat Feb 12", 
      "LABEL": "Sat Feb 12", 
      "WIDTH": "90", 
      "NAME": "Sat Feb 12", 
      "JSONMAP": "CELLS.0.Sat Feb 12" 
     }, 
     { 
      "INDEX": "Sun Feb 13", 
      "LABEL": "Sun Feb 13", 
      "WIDTH": "90", 
      "NAME": "Sun Feb 13", 
      "JSONMAP": "CELLS.0.Sun Feb 13" 
     } 
    ], 
    "columnNames": [ 
     "NAME", 
     "Mon Jan 31", 
     "Tue Feb 01", 
     "Wed Feb 02", 
     "Thu Feb 03", 
     "Fri Feb 04", 
     "Sat Feb 05", 
     "Sun Feb 06", 
     "Mon Feb 07", 
     "Tue Feb 08", 
     "Wed Feb 09", 
     "Thu Feb 10", 
     "Fri Feb 11", 
     "Sat Feb 12", 
     "Sun Feb 13" 
    ] 
} 

}

請注意,JSON是有效的與http://www.jsonlint.com/驗證我沒有選擇,只能使用id這是提供。

提前感謝

回答

0

使用在前面的答案,我說你,你應該使用jqGrid的參數jsonReader。裏面帶有「=」字符的id的用法也很糟糕。例如,如果您在數據庫"uid=AndrewBryant"中使用了id,那麼您可以將JSON數據中僅包含"AndrewBryant"作爲HTML標識。如果你說你「沒有選擇」,我不同意。

在當前的例子中,你在服務器端做出這麼多錯誤,並使用無jsonReaderlocalReader,這是很難評論那裏。例如,您在數據中使用了cells,在jsonmap列屬性中使用了「CELLS」,您以錯誤的方式將其寫爲JSONMAP。在JavaScript中,字符的情況非常重要。此外,您使用"NAME": "Mon Jan 31"而不是「名稱」和一個標識符作爲值。標識符不能有空格。您當然沒有JSON數據中的「Mon Jan 31」屬性的數據。

在末端使用data參數(data: colD.MOVEMENTS.rows)這意味着的localReader代替jsonReader使用(見here有關localReader更多信息)。

不好意思,但是你現在的代碼和數據太髒了,包含了太多的錯誤,所有的東西看起來都是垃圾,無法工作。