表格中的可扩展中间行与AngularJs

时间:2016-05-04 15:21:35

标签: javascript angularjs

我正在创建一个付款门户网站,用户可以为paymentPlanpaymentMethod创建numberOfIntallmentspaymentPlan显示在ng-repeat的表格中。 numberOfIntallments最多可以为36,我不希望用户查看有关所有installments的详细信息。

我想做什么:

如果numberOfIntallments< 7然后显示所有installments 如果numberOfIntallments> = 7显示前2个installments和最后2个,则在表格中间创建一个按钮showAll installments

我该如何解决这个问题?

当前代码:

<tr ng-repeat="model in paymentPlan.installments">
    <td>{{model.dueDate | date: 'shortDate'}}</td>
    <td>{{model.principal | currency:undefined:0}}</td>
    <td ng-show="numberOfInstallments > 1">{{model.contractInterest | currency:undefined:0}}</td>
    <td ng-show="numberOfInstallments > 1">{{model.lendingFee | currency:undefined:0}}</td>
    <td ng-show="paymentMethod == 0">{{model.noticeAndPaymentFee | currency:undefined:0}}</td>
    <td>{{model.installmentFee | currency:undefined:0}}</td>
    <td>{{model.total | currency:undefined:0}}</td>
</tr>

1 个答案:

答案 0 :(得分:1)

您需要在控制器中进行一些计算,并将结果放在您的范围内。然后,您可以调整ng-repeat以获得所需的结果。

<强>控制器:

$scope.ShowMiddleRows = false;

if ($scope.numberOfInstallments >= 7) {
    // Add the first two installments into PrefixRows[] array
    for (i=0; i < 2; i++) {
        $scope.PrefixRows.push($scope.paymentPlan.installments[i]);
    }
    // Add the lasttwo installments into SuffixRows[] array
    for (i=$scope.paymentPlan.installments-2; i < scope.paymentPlan.installments; i++) {
        $scope.SuffixRows.push($scope.paymentPlan.installments[i]);
    }
    // Add the remaining middle rows into MiddleRows[] array
    for (i=2; i < $scope.paymentPlan.installments-2; i++) {
        $scope.MiddleRows.push($scope.paymentPlan.installments[i]);
    }
}

<强> HTML:

<!-- this <tbody> will only show if numberOfInstallments >= 7 -->
<tbody ng-if="numberOfInstallments >= 7">

    <!-- 2 PREFIX ROWS -->
    <tr ng-repeat="model in PrefixRows">
        <td> ... </td>
    </tr>

    <!-- MIDDLE ROWS or a button -->
    <!-- ShowMiddleRows is a scope variable which is true or false depending on button click. Setting it to true will show the middle rows -->
    <tr ng-show="!ShowMiddleRows">
        <td colspan="10"><button ng-click="ShowMiddleRows = !ShowMiddleRows">Show All Installments</button></td>
    </tr>
    <tr ng-repeat="model in MiddleRows" ng-show="ShowMiddleRows">
        <td> ... </td>
    </tr>
    <!-- 2 SUFFIX ROWS -->
    <tr ng-repeat="model in SuffixRows">
        <td> ... </td>
    </tr>
</tbody>

<!-- this <tbody> will only show if numberOfInstallments < 7 -->
<tbody ng-if="numberOfInstallments < 7">
    <tr ng-repeat="model in paymentPlan.installments">
        <td> ... </td>
    </tr>
</tbody>