2017-06-16 104 views
0

enter image description here具有一個字段名稱的網格用戶和要求是它應該在ui-grid標題名稱中顯示Users(用戶數)。我怎麼能得到這個。 這是我的JS文件代碼如何將動態字段添加到ui-grid標題名稱字段

var userCount= response.usercount; 

     columnDefs: [{ 
        name: 'Users', // Along with Users i want count also 
        width: '25%',       
        cellTemplate: 'beacon-template' 
       }, 

我怎麼能得到這個?

+0

你能舉一個更詳細的例子嗎? –

+0

我有用戶,名字,Latname,地址具有標題名稱欄的ui網格。在用戶標題列中,我需要用戶(用戶數)和用戶的標題名稱中的用戶數。我有這個動態的計數值。我怎樣才能顯示標題名稱用戶 –

+0

我添加了一個圖像,它會幫助你更好地理解。 –

回答

0

您可以隨時更改標題文本。聽起來像你可以把手錶的計數值,並做類似如下:

$scope.$watch('yourCountValue', function(newVal){ 

    $scope.gridOptions.columnDefs[0].displayName = 'Users(' + newVal + ')'; 

    $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN); 
}) 

代碼可能會有所不同,在你的情況。例如,您可以觀看$ scope.data更改,並使用$ scope.data.length來連接displayName。您可能有選擇標題和索引將是1而不是0,等等。

更新回答:你忘記注入ui.grid和uiGridConstants?

var app = angular.module('myApp', ['ui.grid']); 

app.controller('appCtrl', ['$scope', 'uiGridConstants', function($scope, uiGridConstants) { 

    $scope.gridOptions = { 

    columnDefs: [ 
     {field: 'field1', displayName: 'User 1'}, 
     {field: 'field2', displayName: 'User 2'}, 
    ], 

    data: [ 
     { field1: "user 1", field2: "bar"}, 
     { field1: "user 2", field2: "bar"}, 
     { field1: "user 3", field2: "bar"} 
    ], 

    onRegisterApi: function(gridApi) { 
     $scope.gridApi = gridApi; 
    } 
    }; 
    //put the watch here 
    /... 
}]); 
+0

什麼是$ scope.gridApi.core m獲取錯誤核心是未定義的。 –

+0

你能告訴我更多你的代碼嗎?聽起來好像你沒有注入ui-grid模塊或者沒有註冊gridApi –