2016-05-18 70 views
0

我有一個來自api調用的json數據,就像下面的procution_volumes數組一樣。

"production_volumes = [{period:'2014Q4', volume:'200', comment:'nothing'},{period:'2014Q2', volume:'300', comment:'something'},{period:'2014Q1', volume:'500', comment:'search'}]" 

我想這樣以表格形式顯示用戶可以更新數據。表代碼如下

<table> 
     <tbody> 
     <tr> 
      <th><strong>Year</strong></th> 
      <th><strong>Quarter</strong></th> 
      <th><strong>Volume</strong></th> 
      <th><strong>Comment</strong></th> 
      <th class="text-right"><strong>Delete</strong></th> 
     </tr> 
     <tr ng-repeat="production in production_volumes"> 
      <td class="vertical-align-top"> 
      <select style="min-width: 140px;" class="select_field form-control"> 
       <option ng-repeat="period_year in period_years" value="{{period_year}}" ng-model="production.period.split('Q')[0]" ng-selected="production.period.indexOf(production.period)!=-1">{{period.year}}</option> 
      </select> 
      </td> 
      <td class="vertical-align-top"> 
      <select style="min-width: 140px;" class="select_field form-control"> 
       <option ng-repeat="period_quater in period_quaters" value="{{period_quater}}" ng-model="production.period.split('Q')" ng-selected="production.period.indexOf('Q'+period_year)!=-1">{{production.period.split("Q")[1]}}</option> 
      </select> 
      </td> 
      <td class="vertical-align-top"> 
      <input type="text" class="input_field form-control" ng-maxlength="50" ng-pattern="" ng-model="production.volume"> 
      </td> 
      <td class="vertical-align-top"> 
      <input type="text" class="input_field form-control" ng-maxlength="50" ng-pattern="" ng-model="production.comment"> 
      </td> 
      <td class="text-center" style="vertical-align: middle;"> 
      <input type="checkbox" class=""> 
      </td> 
     </tr> 

     <tr ng-repeat="n in [].constructor(12-project_details.production_volumes.length) track by $index"> 
      <td class="vertical-align-top"> 
      <select style="min-width: 140px;" class="select_field form-control" ng-model=""> 
       <option ng-repeat="period_year in period_years" value="{{period_year}}">{{period_year}}</option> 
      </select> 
      </td> 
      <td class="vertical-align-top"> 
      <select style="min-width: 140px;" class="select_field form-control" ng-model=""> 
       <option ng-repeat="period_quater in period_quaters" value="{{period_quater}}">{{period_quater}}</option> 
      </select> 
      </td> 
      <td class="vertical-align-top"> 
      <input type="text" class="input_field form-control" ng-maxlength="50"> 
      </td> 
      <td class="vertical-align-top"> 
      <input type="text" class="input_field form-control" ng-maxlength="50"> 
      </td> 
      <td class="text-center" style="vertical-align: middle;"> 
      <input type="checkbox" class=""> 
      </td> 
     </tr> 

     </tbody> 
    </table> 

在同一個控制器js文件,我無法綁定$ scope.production.volume和$ scope.production.comment。它給我錯誤的$ scope.production沒有定義。當我使用不同的ng模型而不是production.volume時,那麼數據不會顯示在視圖中。什麼是理想的解決方案,我可以顯示來自json的數據並將任何更新的數據發送回json。

小提琴鏈接,供您參考http://jsfiddle.net/reachsampathreddy/1gxgsrqm/

回答

0

你在NG-重複,體積和註釋屬性是通過NG-重複創建的生產對象的成員。綁定時不需要嘗試使用索引。正在使用:

<td class="vertical-align-top"> 
    <input type="text" class="input_field form-control" ng-maxlength="50" ng-model="production.volume"> 
</td> 
<td class="vertical-align-top"> 
    <input type="text" class="input_field form-control" ng-maxlength="50" ng-model="production.comment"> 
</td> 

工作得很好。

+0

這樣我就可以顯示數組的數據,但無法綁定到範圍,以便在用戶提交時發回數據。 –

+0

爲什麼不呢? Angular會跟蹤它來自哪裏並在模型中更新它?我添加了一個提交按鈕,並在其中放置了一個調試器行,並檢查了範圍並更新了值。 –

0

請注意,您已經在ng中聲明瞭生產 - 重複它不再與js文件相關。如果你想訪問更新的音量和評論,只需使用ng-change()。

例如:

<input type="text" class="input_field form-control" ng-maxlength="50" ng-model="production.volume" ng-change="getTheChangedValue(production.volume)"> 
 
</td>


在js文件創建getTheChangedValue()函數,安慰你的變化值。

如果你想發佈數據庫,你不需要在你的js文件中手動修改它,Angular會監視你在視圖中改變了什麼,它會在模型​​中自動更新。

+0

在表格末尾有一個提交按鈕,我錯過了這裏。在提交我已經在控制器中創建了一個函數,我試圖構造數組與現有和更新字段數據變量併發送到api調用。但問題是什麼時候聲明var data = {「volume」= $ scope.production.volume,「comment」= $ scope.production.comment} ..我得到錯誤的$ scope.production沒有被定義爲它的動態生成使用ng-repeat。我希望你能理解我的問題。 –

+0

你能否提供完整的代碼片段?如果你想要,你可以使用angular.copy()將原始數組複製到temp變量中。做一件事,像original_production_volumes這樣的原始數組副本,任何如何修改的值將在production_volumes中,所以你有兩個,你可以使用你想要的。 – SivaTeja

相關問題