2016-01-13 88 views
0

我有一個按鈕來向表中添加新行,我的問題是ng模型的值。它的目標是從下拉列表中選擇一個選項,在其他字段中顯示來自該項目的所有數據,以及創建新行時,所有ng模型具有相同的值。如果您選擇2次相同的設備並且添加一個值,它會在兩者中更新,並且只需要在其中一箇中更新。表格內容在每一行中都是獨立的

下面的代碼與工作的例子當前狀態的例子http://jsfiddle.net/z06sonnt/5/

<div ng-app> 
<div ng-controller="Ctrl" id="midi-ctrl"> 
<button ng-click="addRow()">Add Row</button> 
<table> 
    <tr> 
    <th></th> 
    <th>Type</th> 
    <th>Product</th> 
    <th>Price</th> 
    <th>Quantity</th> 
    <th>Notes</th> 
    <th>Cost</th> 
    </tr> 
    <tr ng-repeat="row in rows"> 
    <td><strong>{{row.id}}</strong> 
    </td> 
    <td align="center">{{row.selectedItem.type}}</td> 
    <td align="center"> 
     <select ng-model="row.selectedItem" ng-options="item.product for item in products"> 
     <option value="">Select Product</option> 
     </select> 
    </td> 
    <td>{{row.selectedItem.price}} </td> 
    <td align="center"> 
     <input style="width: 75px" ng-model="row.selectedItem.quantity" ng-change="calcPrice(row.selectedItem)" type='number'> 
    </td> 
    <td align="center"> 
     <input style="width: 75px" ng-model="row.selectedItem.notes"> 
    </td> 
    <td align="center"> 
     <input style="width: 75px" ng-model="row.selectedItem.cost">$ 
    </td> 
    </tr> 
</table> 
<b>rows</b>{{rows}} 
<br> 
<b>Products</b>{{products}} 

Controller.js

function Ctrl($scope) { 
$scope.rows = [{ 
id: '1', 
selectedItem: [] 
}, { 
id: '2', 
selectedItem: [] 
}]; 
$scope.counter = 3; 

$scope.calcPrice = function(item) { 
item.cost = item.price * item.quantity; 
}; 

$scope.addRow = function() { 
$scope.newRow = {}; 
$scope.newRow['id'] = $scope.counter; 
$scope.newRow['selectedItem'] = []; 
$scope.rows.push($scope.newRow); 
$scope.counter++; 
}; 

$scope.products = [{ 
type: 'mobile', 
product: 'Device A', 
price: 70 
}, { 
type: 'games', 
product: 'Device B', 
price: 80 
}, { 
type: 'PC', 
product: 'Device C', 
price: 100 
}, ]; 
} 
+0

什麼叫動態 – gaurav5430

+0

意思,如果你提供了這個代碼的plunkr或codepen例如這將是真正的幫助。 –

+0

@ gaurav5430了'selectedItem'領域屬於每一行就像他們加入這裏 – changez

回答

0

你可以讓你選擇的項目中的對象你的rowContent。這樣

<div class="col-lg-12" > 
<button ng-click="addRow()">Add Row</button> 
<table class="table table-bordered "> 
    <tr> 
     <th>ID</th> 
     <th>Client</th> 
     <th>Quantity</th> 
     <th>Notes</th> 
     <th></th> 
     <th>Software Cost</th> 
     <th>Labor</th> 
     <th>Workflow</th> 
    </tr> 
    <tr ng-repeat="row in rows"> 
     <td align="center">{{row.id }}{{row.selectedItem.clientType}}</td> 
     <td align="center"><select ng-model="row.selectedItem" 
            ng-options="item.software for item in products"> 
      <option value="">Select Client</option> 
     </select></td> 
     <td align="center"><input ng-model="row.selectedItem.quantity" ng-change="calcLabor(row.selectedItem)"></td> 
     <td align="center"><input ng-model="row.selectedItem.locations" ng-change="calcLabor(row.selectedItem)"></td> 
     <td align="center"><input ng-model="row.selectedItem.notes"></td> 
     <td align="center">{{(row.selectedItem.price)*(row.selectedItem.quantity)|currency}}</td> 
     <td align="center"><input ng-model="row.selectedItem.labor"> 
      <a href="" data-toggle="modal" 
       data-target="#image-modal"> 
       <button ng-click="imageShowModal(row.selectedItem)">Edit Formula</button> 
      </a> 
     </td> 
     <td align="center"><input ng-model="row.selectedItem.workflow"></td> 
     <td align="center"></td> 
    </tr> 
</table> 
</div> 

控制器東西

app.controller("appController", ["$scope", "$filter", function ($scope, $filter) { 
    $scope.rows=[{ id:'1', selectedItem:[] }, { id:'2', selectedItem:[] }]; 
    $scope.counter=3; 
    $scope.imageShowModal = function (item) { 
    $scope.bedModal = item; 
}; 

$scope.addRow = function() { 
    $scope.newRow = {}; 
    $scope.newRow['id'] = $scope.counter; 
    $scope.newRow['selectedItem'] = []; 
    $scope.rows.push($scope.newRow); 
    $scope.counter++; 
}; 
+0

我試過這種方式,但錯誤的是: 類型錯誤:無法分配給只讀屬性'selectedItem'的1 在fn.assign(eval在 changez

+0

我認爲你需要在你的控制器的行內聲明每個selectedItem,就像$ scope.row = [{id:'1',selectedItem: []}] – rlwheeler

+0

我修改了上面的答案,將對象添加到控制器 – rlwheeler

相關問題