代码之家  ›  专栏  ›  技术社区  ›  Abhi

如何检查数值是否大于ng更改时的数值

  •  2
  • Abhi  · 技术社区  · 6 年前

    screenshot

    闪电战:

    https://stackblitz.com/edit/angularjs-vc9lg5

    我有一个号码 Total expense 和一个数据表。

    有一个专栏 Amt 金额

    ng-change deductedTillNow balanceLeftInExpense .

    您可以在chrome扩展中看到 $scope 变量 现在扣除 平衡 在右边。当我试图通过输入从第一行扣除剩余的500时 50 变成450。

    , 0 ,即 450 500大于450。如何允许输入500?

     //calculate amount till now
    $scope.deductedTillNow = 0;
    for (let j = 0; j < $scope.amountLedgerListArray.length; j++) {
       if($scope.amountLedgerListArray[j].adjustAmount){
        $scope.deductedTillNow = $scope.amountLedgerListArray[j].adjustAmount + $scope.deductedTillNow;
       }
    }
    
    //calculate balance left in expense
    
    $scope.balanceLeftInExpense = $scope.totalExpenseAmount - $scope.deductedTillNow;
    

    平衡 -验证:

    if(item.adjustAmount > $scope.balanceLeftInExpense){
      item.adjustAmount = item.adjustAmount.toString();
      item.adjustAmount = item.adjustAmount.substring(0, item.adjustAmount.length - 1);
      item.adjustAmount = parseInt(item.adjustAmount);
    }
    

    html:

        <table class="table table-bordered table-striped">
       <thead>
          <tr>
             <th>Payment Ref no</th>
             <th>Date</th>
             <th>Amt</th>
             <th >Adjust amount</th>
          </tr>
       </thead>
       <tbody>
          <tr ng-repeat="item in amountLedgerListArray">
             <td>
                {{item.payment_ref_no}}
             </td>
             <td>
                {{item.payment_date}}
             </td>
             <td>
                {{item.amount}}
             </td>
             <td>
                <input type="number" name="sgst" class="form-control"
                   ng-disabled="item.isDisabled" autocomplete="off"
                   ng-change="calculateBalanceAndRestrict(item)"
                   ng-class="{'borderRedError' : expense.$invalid}"
                   ng-model="item.adjustAmount" onkeypress="limitKeyPress(this, 10, event)" />
             </td>
          </tr>
       </tbody>
    </table>
    

    编辑 “金额”列中的数据是已支付的预付款。现在有一个费用(本例中为1000。请参见屏幕截图) Total expense:1000 ).我可以从表中的任何一行中扣除 金额

    这里我从第三行扣除了500英镑(现在的余额是500英镑)。我可以从最上面的两行中扣除剩下的500。第二行的100(因为它只有100)和第一行的剩余500,或者第一行的剩余500。

    我这样验证:在更改输入框时,我检查输入的金额是否大于输入的所有金额之和( 现在扣除 ).当我现在开始在第一行输入时,费用中的余额会被计算出来。当我输入50时,左边的余额是450(1000-500(第三行)+50(第一行))。

    当我在50之后为第一行剩余的500键入0时,左边的余额是450。因此,我的验证不允许这样做(输入的500大于450)。我需要允许输入500。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Nitin Bhapkar    6 年前

    将您的if条件更改为以下条件:

    if(($scope.totalExpenseAmount <= $scope.deductedTillNow) || (($scope.totalExpenseAmount - $scope.deductedTillNow) < item.adjustAmount) ){
      item.adjustAmount = item.adjustAmount.toString();
      item.adjustAmount = item.adjustAmount.substring(0, item.adjustAmount.length - 1);
      item.adjustAmount = parseInt(item.adjustAmount);
    }
    
        2
  •  0
  •   Just code    6 年前

    代码中的主要问题是错误地检查了余额left

     if(item.adjustAmount > item.amount || (item.amount <= 0)){
          item.adjustAmount = null;
          return
        }
    

    在扣除这样的金额后,应检查剩余余额

      $scope.balanceLeftInExpense = $scope.totalExpenseAmount - $scope.deductedTillNow;
    
      if ($scope.balanceLeftInExpense < 0) {
        item.adjustAmount = item.adjustAmount.toString();
        item.adjustAmount = item.adjustAmount.substring(0, item.adjustAmount.length - 1);
        item.adjustAmount = parseInt(item.adjustAmount);
      }
    

    帐户禁用代码似乎也错了,您需要检查每一行的数据

      //if amount entered is equal to advance paid of that row, enable others
      for (let i = 0; i < $scope.amountLedgerListArray.length; i++) {
        if (Number($scope.amountLedgerListArray[i].amount) < $scope.amountLedgerListArray[i].adjustAmount) {
          $scope.amountLedgerListArray[i].isDisabled = false;
        }
        else {         
            //disable those that dont have a value in it
            if ($scope.amountLedgerListArray[i].advance_id != item.advance_id && !$scope.amountLedgerListArray[i].adjustAmount && Number($scope.amountLedgerListArray[i].amount) < $scope.amountLedgerListArray[i].adjustAmount) {
              $scope.amountLedgerListArray[i].isDisabled = true;           
    
          }
        }
      }
    

    Demo