2015-11-01 176 views
4

我正在從Django的這個JSON和我想的角度UI的表格顯示這一點,但我得到的錯誤:如何顯示從JSON來UI網格數據角度UI網

Error: colDef.name or colDef.field property is required 
[email protected]://127.0.0.1:8000/static/buddy/js/ui-grid.js:3771:1 
buildColumns/<@http://127.0.0.1:8000/static/buddy/js/ui-grid.js:3630:7 
[email protected]://127.0.0.1:8000/static/buddy/js/ui-grid.js:3629:5 
[email protected]://127.0.0.1:8000/static/buddy/js/ui-grid.js:2749:27 
[email protected]://127.0.0.1:8000/static/buddy/js/angular.js:15693:13 
$RootScopeProvider/this.$get</[email protected]://127.0.0.1:8000/static/buddy/js/angular.js:15826:23 
$RootScopeProvider/this.$get</[email protected]://127.0.0.1:8000/static/buddy/js/angular.js:16097:13 
[email protected]://127.0.0.1:8000/static/buddy/js/angular.js:10546:36 
[email protected]://127.0.0.1:8000/static/buddy/js/angular.js:10744:7 
[email protected]://127.0.0.1:8000/static/buddy/js/angular.js:10685:1 

我要只顯示來自「域」

的JSON是屬性:

[{"fields": {"joiningtime": null, "boozprofileId": 1, "userId": 1, "likeStatus": true}, "model": "buddy.guestentry", "pk": 1}, {"fields": {"joiningtime": null, "boozprofileId": 1, "userId": 1, "likeStatus": true}, "model": "buddy.guestentry", "pk": 2}, {"fields": {"joiningtime": "2015-10-18T15:53:58.243Z", "boozprofileId": 12, "userId": 3, "likeStatus": true}, "model": "buddy.guestentry", "pk": 3}, {"fields": {"joiningtime": "2015-10-18T15:54:24.055Z", "boozprofileId": 8, "userId": 3, "likeStatus": true}, "model": "buddy.guestentry", "pk": 4}, {"fields": {"joiningtime": null, "boozprofileId": 3, "userId": 1, "likeStatus": true}, "model": "buddy.guestentry", "pk": 5}, {"fields": {"joiningtime": null, "boozprofileId": 3, "userId": 1, "likeStatus": true}, "model": "buddy.guestentry", "pk": 6}, {"fields": {"joiningtime": null, "boozprofileId": 3, "userId": 1, "likeStatus": true}, "model": "buddy.guestentry", "pk": 7}, {"fields": {"joiningtime": null, "boozprofileId": 3, "userId": 1, "likeStatus": true}, "model": "buddy.guestentry", "pk": 8}] 

回答

1

您收到的錯誤表明您沒有定義列定義的UI電網,或者不能讓他們正確定義。簡單地指嵌套fields屬性爲fields.<attributeName>

//the JSON from above 
$scope.gridOptions.data = [{"fields": {"joiningtime": null, "boozprofileId": ....}]; 

$scope.gridOptions.columnDefs = [ 
    {name: 'fields.joiningtime' }, 
    {name: 'fields.boozprofileId' }, 
    {name: 'fields.userId' }, 
    {name: 'fields.likeStatus' } 
]; 

演示 - >http://plnkr.co/edit/KXvES4G64RVwneFbZzV2?p=preview


記住要瞄準正確的控制器。你有兩個IndexCtrlajax

<div ng-controller="ajax"> 
    <div ui-grid="gridOptions" ui-grid-cellNav class="grid"></div> 
</div> 
+0

現在我得到錯誤intialize它:需要colDef.name或colDef.field財產 – focode

+0

@focode - 你這樣做之前, - 「_colDef.name或colDef.field屬性是必需的」 - 如果你顯示你的代碼,它會容易得多,也許你對標記有問題。你是否從plnkr複製了這個計劃?它應該馬上工作,就像'$ http'一樣。 – davidkonrad

+0

我不能在這裏複製過去,這裏是它的github鏈接...控制器名稱是ajax:https://github.com/focode/buddy2/blob/master/src/static/buddy/js/ main.js – focode

1

萬一大衛的回答並沒有幫助你:

嘗試從後JSON received地方卸下

$scope.gridOptions.data = [{"fields": {"joiningtime": null, "boozprofileId": ....}]; 

$scope.gridOptions.columnDefs = [ 
    {name: 'fields.joiningtime' }, 
    {name: 'fields.boozprofileId' }, 
    {name: 'fields.userId' }, 
    {name: 'fields.likeStatus' } 
]; 

,並把它放在控制器的起點功能。

我的意思是,在一開始,然後只更新data當您收到JSON response

$scope.gridOptions.data = response.data