一个表行中有两个ng-repeat(tr)

时间:2019-04-18 11:58:10

标签: angularjs

我想在一个表行中使用ng-repeat重复两个数据。我如何在一个表格行中使用它?

<table>
    <thead>
        <tr>
            <th rowspan=2>Line</th>
            <th>Debit Account</th>
            <th>Credit Account</th>
            <th>Ref</th>
            <th>Amount</th>
        </tr>
        <tr>
            <th>Debit Account Name</th>
            <th>Credit Account Name</th>
            <th>Date</th>
            <th colspan=2>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr  ng-repeat="dbAccount in formData.dbAccounts track by $index" ng-repeat-start="crAccount in formData.crAccounts track by $index">
            <td rowspan=2>0000{{$index + 1}}</td>
            <td>
                <input type="text" ng-model="dbAccount.select"/>
            </td>
            <td>
                <input type="text" ng-model="crAccount.select"/>
            </td>
            <td>
                <input type="text" ng-model="referenceNumber"/>
            </td>
            <td>
                <input type="text" ng-model="dbAccount.debitAmount"/>
            </td>
        </tr>
        <tr>
            <td>
                <input readonly type="text" ng-model="dbAccountname"/>
            </td>
            <td>
                <input readonly type="text" ng-model="crAccountname"/>
            </td>
            <td>
                <input disabled sort type="text" datepicker-pop="dd MMMM yyyy" is-open="opened" ng-model="transDate"/>
            </td>
            <td colspan=2>
                <input type="text" ng-model="comments" ng-keydown="$event.keyCode === 13 && addNewBatchRow($index)"/>
            </td>
        </tr>
    </tbody>
</table>

在控制器中,没有json数据,但是下面的代码。 ng-repeat重复表行以供从前端输入。我们非常感谢您的协助。

scope.formData.crAccounts = [{}];
scope.formData.dbAccounts = [{}];
scope.numberOfAcceptableRows = 5;

这将在输入Enter键时添加新行。

scope.addNewBatchRow = function (index) {
    if(scope.numberOfCreditsDebits <= 1){
    scope.formData.crAccounts.push({});
    scope.formData.dbAccounts.push({});
    scope.numberOfCreditsDebits = scope.numberOfCreditsDebits + 1;
    } else if (scope.numberOfCreditsDebits >= scope.numberOfAcceptableRows){
    scope.error = "Journal entry is limmited to " + scope.numberOfAcceptableRows + " rows.";
    }
};

2 个答案:

答案 0 :(得分:0)

您可能希望将formData.dbAccountsformData.crAccounts合并为一个数组,然后使用ng-repeat对其进行迭代:

var app = angular.module('app', []);
app.controller('TestCtrl', function($scope) {
  var x = ['x_one', 'x_two'];
  var y = ['y_one', 'y_two'];
  $scope.xy = x.map(function(item, index) {
    return { x: item, y: y[index] };
  })
}); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="TestCtrl">
    <table>
      <tr ng-repeat="item in xy">
        <td>{{ item.x }}</td>
        <td>{{ item.y }}</td>
      </tr>
    </table>
  </div>
</div>

答案 1 :(得分:0)

也许您可以使用$index遍历数据。

var app = angular.module('app', []);
app.controller('TestCtrl', function($scope) {
  $scope.formData = {
    dbAccounts: ['Mike', 'Bob'],
    crAccounts: [231, 239]
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="TestCtrl">
    <table>
      <tr ng-repeat="dbAccount in formData.dbAccounts">
        <td>{{ dbAccount }}</td>
        <td>{{ formData.crAccounts[$index] }}</td>
      </tr>
    </table>
  </div>
</div>