2017-09-24 115 views
0

​​我有一個動態創建的ngTable,設置每列的行顏色。如何更改列標題的顏色?如何以編程方式更改ngTable標題顏色?

HTML:

<table ng-table-dynamic="tableParams with cols" class="table table-bordered table-hover"> 
    <tr ng-repeat="row in data"> 
     <td ng-repeat="col in cols" ng-style="{ 'color': col.color }">{{row[col.nm]}}</td> 
    </tr> 
</table> 

的Javascript:

var app = angular.module('app', ['ngTable']); 

app.controller('myCtl', function($scope,NgTableParams) { 

     $scope.cols = [ 
     {nm:'uid', title:'User ID', color: 'blue'}, 
     {nm:'ugr', title: 'Group ID', color: 'red'} 
     ]; 


     $scope.data = [ 
     { uid: 'aaa',ugr: '222'}, 
     { uid: 'bbb', ugr: '111'} 
     ]; 

     $scope.tableParams = new NgTableParams({dataset: $scope.data}); 

}); 

回答

1

您需要包括一個THEAD。這裏是一個更新Plunker

<table ng-table-dynamic="tableParams with cols" class="table table-bordered table-hover"> 
    <thead> 
     <tr> 
      <th ng-repeat="col in cols" ng-style="{ 'color': col.color }">{{col.title}}</th> 
     </tr> 
    </thead> 
    <tr ng-repeat="row in data"> 
     <td ng-repeat="col in cols" ng-style="{ 'color': col.color }">{{row[col.nm]}}</td> 
    </tr> 
</table> 
+0

你的解決方案有效,是首選的解決方案,因爲它可以讓我以編程方式更改顏色無類。然而,它打破了每頁的行數,看看這個plunk,表應該顯示每行3行,而不是4. http://plnkr.co/edit/98kvWKgzE8fwgsEjkQce?p=preview – ps0604

1

您可以使用每個對象的class財產在你cols陣列:

$scope.cols = [ 
    {nm:'uid', title:'User ID', class: 'text-blue' }, 
    {nm:'ugr', title: 'Group ID', class: 'text-red'} 
]; 

然後設置相應的CSS類在樣式表:

.text-blue{ 
    color: #0000ff; 
} 

.text-red{ 
    color: #ff0000; 
} 

Demo Plunk

+1

好吧我不知道它實際上支持類屬性,因此我的解決方案,我會保持它的情況下有人需要在該標題的未來修改,但這應該是upvoted。 – pegla

+0

我需要以編程方式更改顏色,顏色不應該在類中,因爲它們將存儲在數據庫中 – ps0604

0

正確的方式做到這一點是馬修·考利的答案,但如果你想要做的表頭額外的修改是非常有用的知道,你可以更改模板標題:後加

http://plnkr.co/edit/662FYVbJyz2wxqXV5nNk?p=preview

<table template-header="table-header.html" ng-table-dynamic="tableParams with cols" class="table table-bordered table-hover"> 

文件中包含該項目表了header.html:

<tr> 
    <th title="{{$column.headerTitle(this)}}" 
     ng-repeat="$column in $columns" 
     ng-class="{ 
        'sortable': $column.sortable(this), 
        'sort-asc': params.sorting()[$column.sortable(this)]=='asc', 
        'sort-desc': params.sorting()[$column.sortable(this)]=='desc', 
        }" 
     ng-click="sortBy($column, $event)" 
     ng-if="$column.show(this)" 
     ng-init="template = $column.headerTemplateURL(this)" 
     class="header {{$column.class(this)}} {{$column.headerClass}}"> 
     <div ng-if="!template" class="ng-table-header" ng-class="{'sort-indicator': params.settings().sortingIndicator == 'div'}"> 
      <span ng-bind="$column.title(this)" ng-class="{'sort-indicator': params.settings().sortingIndicator == 'span'}"></span> 
     </div> 
     <div ng-if="template" ng-include="template"></div> 
    </th> 
</tr> 
在你的代碼

則:

$scope.cols = [ 
     {nm:'uid', title:'User ID', headerClass: 'blue'}, 
     {nm:'ugr', title: 'Group ID', headerClass: 'red'} 
     ]; 

也不要忘了CSS類:

.red { 
    color: red; 
} 
.blue { 
    color: blue; 
} 
相關問題