2015-10-28 72 views
1

我有問題,我的表生成動態的,因爲我不知道該怎麼辦呢?AngularJS添加類​​這是ngRepeat從數據庫

我在HTML表是動態生成的角度與數據庫中的數據,現在我需要爲td元素添加一些樣式。我需要添加的樣式,我從數據庫(rgb中的背景顏色)獲得,但我不知道如何?

下面的代碼是由角鑑於產生:

<table class="table table-bordered table-hover table-striped"> 
     <thead> 
      <tr> 
       <th>Lp.</th> 
       <th>image</th> 
       <th>Article name:</th> 
       <th>Colors in article:</th> 
       <th ng-repeat="y in [] | range:0:number">{{$index+1}}</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr ng-repeat="pokayoke in pokayokes | filter : search track by $index"> 
       <td>{{$index+1}}</td> 
       <td>obrazek</td> 
       <td ng-repeat="column in pokayoke" class="{{ column }}"><p>{{ column }}</p></td> 
      </tr> 
     </tbody> 
</table> 

現在我需要得到的類數據庫,從5日開始的值:

其產生上面的表格
<table class="table table-bordered table-hover table-striped"> 
    <thead> 
     <tr> 
      <th>Lp.</th> 
      <th>image</th> 
      <th>Article name:</th> 
      <th>Colors in article:</th> 
      <th class="ng-binding ng-scope" ng-repeat="y in [] | range:0:number">1</th> 
      <th class="ng-binding ng-scope" ng-repeat="y in [] | range:0:number">2</th> 
      <th class="ng-binding ng-scope" ng-repeat="y in [] | range:0:number">3</th> 
      <th class="ng-binding ng-scope" ng-repeat="y in [] | range:0:number">4</th> 
      <th class="ng-binding ng-scope" ng-repeat="y in [] | range:0:number">5</th> 
      <th class="ng-binding ng-scope" ng-repeat="y in [] | range:0:number">6</th> 
      <th class="ng-binding ng-scope" ng-repeat="y in [] | range:0:number">7</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr class="ng-scope" ng-repeat="pokayoke in pokayokes | filter : search track by $index"> 
      <td class="ng-binding">1</td> 
      <td>obrazek</td> 
      <td ng-repeat="column in pokayoke" class="11_14_1167"><p class="ng-binding">11_14_1167</p></td> 
      <td ng-repeat="column in pokayoke" class="C,M,Y,K,P286,P218"><p class="ng-binding">C,M,Y,K,P286,P218</p></td> 
      <td ng-repeat="column in pokayoke" class="x"><p class="ng-binding">x</p></td> 
      <td ng-repeat="column in pokayoke" class="x"><p class="ng-binding">x</p></td> 
      <td ng-repeat="column in pokayoke" class="C"><p class="ng-binding">C</p></td> 
      <td ng-repeat="column in pokayoke" class="C"><p class="ng-binding">C</p></td> 
      <td ng-repeat="column in pokayoke" class="x"><p class="ng-binding">x</p></td> 
      <td ng-repeat="column in pokayoke" class="x"><p class="ng-binding">x</p></td> 
      <td ng-repeat="column in pokayoke" class="x"><p class="ng-binding">x</p></td> 

     </tr> 
     <tr class="ng-scope" ng-repeat="pokayoke in pokayokes | filter : search track by $index"> 
      <td class="ng-binding">2</td> 
      <td>obrazek</td> 
      <td ng-repeat="column in pokayoke" class="11_14_1171"><p class="ng-binding">11_14_1171</p></td> 
      <td ng-repeat="column in pokayoke" class="C,M,Y,K,P286,P7465"><p class="ng-binding">C,M,Y,K,P286,P7465</p></td> 
      <td ng-repeat="column in pokayoke" class="x"><p class="ng-binding">x</p></td> 
      <td ng-repeat="column in pokayoke" class="x"><p class="ng-binding">x</p></td> 
      <td ng-repeat="column in pokayoke" class="Y"><p class="ng-binding">Y</p></td> 
      <td ng-repeat="column in pokayoke" class="Y"><p class="ng-binding">Y</p></td> 
      <td ng-repeat="column in pokayoke" class="Y"><p class="ng-binding">Y</p></td> 
      <td ng-repeat="column in pokayoke" class="Y"><p class="ng-binding">Y</p></td> 
      <td ng-repeat="column in pokayoke" class="Y"><p class="ng-binding">Y</p></td> 

     </tr> 
    </tbody> 
</table> 

代碼列到最後,並在rgb中分別添加背景樣式。 我不知道整個過程在angular或php網站中的哪個位置。 我會很高興,如果有人寫我應該如何一步看起來像...... :)

編輯15年10月29日:數據庫表的 打印屏幕:

  1. 表從哪裏獲得的數據表:enter image description here

  2. 表在那裏我有類:enter image description here

而且一行的示例代碼,我想怎麼樣到底如何:

<tr class="ng-scope" ng-repeat="pokayoke in pokayokes | filter : search track by $index"> 
      <td class="ng-binding">1</td> 
      <td>obrazek</td> 
      <td ng-repeat="column in pokayoke" class="11_14_1167"><p class="ng-binding">11_14_1167</p></td> 
      <td ng-repeat="column in pokayoke" class="C,M,Y,K,P447,P218"><p class="ng-binding">C,M,Y,K,P447,P218</p></td> 
      <td ng-repeat="column in pokayoke" class="P447" style="background-color: rgb(39,46,32)"><p class="ng-binding">P447</p></td> 
      <td ng-repeat="column in pokayoke" class="P447" style="background-color: rgb(39,46,32)"><p class="ng-binding">P447</p></td> 
      <td ng-repeat="column in pokayoke" class="K" style="background-color: rgb(0,0,0)"><p class="ng-binding">K</p></td> 
      <td ng-repeat="column in pokayoke" class="K" style="background-color: rgb(0,0,0)"><p class="ng-binding">K</p></td> 
      <td ng-repeat="column in pokayoke" class="x" style="background-color: rgb(255,255,255)"><p class="ng-binding">x</p></td> 
      <td ng-repeat="column in pokayoke" class="x" style="background-color: rgb(255,255,255)"><p class="ng-binding">x</p></td> 
      <td ng-repeat="column in pokayoke" class="x" style="background-color: rgb(255,255,255)"><p class="ng-binding">x</p></td> 
</tr> 

該類不是必需的,我只需要添加樣式內聯。

回答

0

我簡化它假設你有保存在DB顏色名,說你有一個簡單的OBJ是這樣的:

$scope.items = [{ 
    "color":"red", 
    "value":12 
    },{ 
    "color":"green", 
    "value":12 
    },{ 
    "color":"blue", 
    "value":12 
    },{ 
    "color":"pink", 
    "value":12 
    }]; 

然後在HTML您可以添加內嵌樣式這樣,

<table> 
     <tr ng-repeat="item in items" style="background:{{item.color}}"><td>{{item.value}}</td></tr> 
</table> 

希望這可以解決您的問題。

Demo

+0

但我已經在TD元素一個ngRepeat環,我可以在這同一個元素不止一個?一個循環爲不同的範圍? –

+0

你能分享你的數據庫的結構和你想達到的最終結果嗎? – Nil

+0

我編輯了問題,並添加了DB的結構和簡單的例子,我想它看起來像。 –