json数组中的下一个索引最小数量应大于上一个索引MaxAmount

时间:2018-07-12 05:26:04

标签: javascript arrays angularjs

在这个Json数组中,我需要增加一个索引中的所有最小数量必须大于先前的索引最大数量,并且另一个条件是maxamount应该大于相同的最小数量索引

// Code goes here

var app = angular.module('Demoapp',[]);
app.controller('ctrl',
function($scope){
$scope.model = [
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 0,
    "maxAmount": 20000,
    "minAmount": 0,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1837,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 15,
    "maxAmount": 500000,
    "minAmount": 20001,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1867,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 17,
    "maxAmount": 200000,
    "minAmount": 100001,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1873,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 20,
    "maxAmount": 999999999,
    "minAmount": 700002,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1879,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  }
];
$scope.volumeChange = function(data){
    angular.forEach($scope.model,function(o,index){
          if(o.minAmount === data.minAmount ){
            if($scope.model[index + 1]){
              $scope.model[index + 1].minAmount = data.maxAmount + 1
             
            }
          }
        })
}

});
<!DOCTYPE html>
<html>

  <head>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-app="Demoapp" ng-controller="ctrl">
      <div ng-repeat="data in model">
        <input class="text-right" type="number" ng-model="data.minAmount" name="minAmount"   min="0" max="999999998" ng-disabled="true" placeholder="from" />
        <input class="text-right" type="number" ng-change="volumeChange(data,volume_range)" ng-model="data.maxAmount" name="maxAmount"  min="{{data.minAmount+1}}" max="999999999" maxlength="9" id="maxAmount" placeholder="to" required>
        <input class="text-right" type="number" name="discount" id="discount" ng-model="data.discount" min="0" max="99.99" step="0.01" placeholder="00.00" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" required>
        
      </div>

</div>
  </body>

</html>

在上面的示例中,我已经基于更改最小量来更改下一个索引值,我的例外输出是在更改单个maxamount时检查所有索引

1 个答案:

答案 0 :(得分:0)

// Code goes here


var app = angular.module('Demoapp',[]);
app.controller('ctrl',
function($scope){
$scope.model = [
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 0,
    "maxAmount": 20000,
    "minAmount": 0,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1837,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 15,
    "maxAmount": 500000,
    "minAmount": 20001,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1867,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 17,
    "maxAmount": 200000,
    "minAmount": 100001,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1873,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  },
  {
    "bundleName": "VD VBS",
    "defaultDiscount": 15,
    "discount": 20,
    "maxAmount": 999999999,
    "minAmount": 700002,
    "price": null,
    "priceListId": 59,
    "priceListProductDetailsId": 1879,
    "productGroupId": 2042,
    "productGroupName": "Distribution Box",
    "productId": null,
    "productShortDescription": null
  }
];
$scope.volumeChange = function(data){
    angular.forEach($scope.model,function(o,index){
          if(o.minAmount === data.minAmount ){
            if($scope.model[index + 1]){
              $scope.model[index + 1].minAmount = data.maxAmount + 1
             
            }
          }
          if($scope.model[index + 1]){
            if($scope.model[index + 1].minAmount < $scope.model[index].maxAmount){
                $scope.model[index + 1].minAmount = $scope.model[index].maxAmount + 1
            }
          if($scope.model[index + 1].maxAmount < $scope.model[index + 1].minAmount){
             $scope.model[index + 1].maxAmount = $scope.model[index + 1].minAmount + 1
          }
        }
        })
}

});
<!DOCTYPE html>
<html>

  <head>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-app="Demoapp" ng-controller="ctrl">
      <div ng-repeat="data in model">
        <input class="text-right" type="number" ng-model="data.minAmount" name="minAmount"   min="0" max="999999998" ng-disabled="true" placeholder="from" />
        <input class="text-right" type="number" ng-change="volumeChange(data,volume_range)" ng-model="data.maxAmount" name="maxAmount"  min="{{data.minAmount+1}}" max="999999999" maxlength="9" id="maxAmount" placeholder="to" required>
        <input class="text-right" type="number" name="discount" id="discount" ng-model="data.discount" min="0" max="99.99" step="0.01" placeholder="00.00" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" required>
        
      </div>

</div>
  </body>

</html>