基于数组中值的ngChange更新禁用ngOptions [英] Disable ngOptions based on ngChange update of value in array

查看:92
本文介绍了基于数组中值的ngChange更新禁用ngOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在选定选项后禁用它们.这取决于与每个"cusomter_no"相关联的"perf_no",如以下代码所示. ngChange在仍在更新模型的同时更新关联对象中已禁用"的值.防抖的ngModelOptions对我不起作用. 在ngChange完成后是否可以通过回调来更新该对象的"disabled"值?或者也许是实现这一目标的更好的方法?预先感谢.

I'm attempting to disabled options in a select after they've already been selected. This is dependent on the 'perf_no' associated with each 'cusomter_no' as seen in the following code. The ngChange updates the value of 'disabled' in the associated object while it's still updating the model. ngModelOptions of debounce does not work for me. Is there a way to have a callback after the ngChange is finished to update the value of 'disabled' for that object? Or perhaps a much better way to achieve this? Thanks in advance.

<div class="session-registrant-field field-dropdown">
      <label for="">{{nameLbl}}</label>
      <select
      ng-model="affiliateSelected[cartPerf.perf_no][$index]"
      ng-change="changedAffiliate(affiliateSelected, cartPerf.perf_no, $index, affiliateSelected[cartPerf.perf_no][$index].customer_no)" 
      ng-options="affiliate.fname + ' ' + affiliate.lname disable when affiliate.disabled for affiliate in affiliates | filter:cartPerf.perf_no | orderBy:affiliate.customer_no:true track by affiliate.customer_no"
      required>
        <option value="" disabled="">-- Select person --</option>
      </select>
    </div>

控制器代码段

$scope.affiliates = [
    {"id":"1","perf_no":"154","perf_dt":"2017-09-18T10:00:00","customer_no":"-10003","fname":"","lname":"Add New Child","validation_pass_age":"Y","validation_pass_prereq":"Y","ask_birthdate":"Y","ask_current_grade":"Y","ask_next_grade":"Y","ask_email":"N","bdatereq":"Y","age_validation_ind":"Y","min_age":"10","max_age":"25","rel_type_id":"10003"},
  {"id":"2","perf_no":"154","perf_dt":"2017-09-18T10:00:00","customer_no":"-10002","fname":"","lname":"Add New Adult","validation_pass_age":"Y","validation_pass_prereq":"Y","ask_birthdate":"Y","ask_current_grade":"Y","ask_next_grade":"Y","ask_email":"N","bdatereq":"Y","age_validation_ind":"Y","min_age":"10","max_age":"25","rel_type_id":"10002"},
  {"id":"3","perf_no":"154","perf_dt":"2017-09-18T10:00:00","customer_no":"2762","fname":"Ryan","lname":"Jones","age":"22","validation_pass_age":"Y","validation_pass_prereq":"Y","ask_birthdate":"Y","ask_current_grade":"N","ask_next_grade":"N","ask_email":"Y","birthdate":"07\/13\/1995","bdatereq":"Y","eaddress":"","age_validation_ind":"Y","min_age":"10","max_age":"25","rel_type_id":"10003"},
  {"id":"4","perf_no":"154","perf_dt":"2017-09-18T10:00:00","customer_no":"2700","fname":"John","lname":"Doe","age":"","validation_pass_age":"Y","validation_pass_prereq":"Y","ask_birthdate":"Y","ask_current_grade":"N","ask_next_grade":"N","ask_email":"Y","birthdate":"02\/22\/1988","bdatereq":"Y","eaddress":"","age_validation_ind":"Y","min_age":"10","max_age":"25","rel_type_id":"10003"},
  {"id":"5","perf_no":"154","perf_dt":"2017-09-18T10:00:00","customer_no":"2701","fname":"Rick","lname":"Deckard","age":"","validation_pass_age":"Y","validation_pass_prereq":"Y","ask_birthdate":"Y","ask_current_grade":"N","ask_next_grade":"N","ask_email":"Y","birthdate":"02\/22\/1988","bdatereq":"Y","eaddress":"","age_validation_ind":"Y","min_age":"10","max_age":"25","rel_type_id":"10002"},
  {"id":"1","perf_no":"93","perf_dt":"2017-09-18T10:00:00","customer_no":"-10003","fname":"","lname":"Add New Child","validation_pass_age":"Y","validation_pass_prereq":"Y","ask_birthdate":"Y","ask_current_grade":"Y","ask_next_grade":"Y","ask_email":"N","bdatereq":"Y","age_validation_ind":"Y","min_age":"10","max_age":"25","rel_type_id":"10003"},
  {"id":"2","perf_no":"93","perf_dt":"2017-09-18T10:00:00","customer_no":"-10002","fname":"","lname":"Add New Adult","validation_pass_age":"Y","validation_pass_prereq":"Y","ask_birthdate":"Y","ask_current_grade":"Y","ask_next_grade":"Y","ask_email":"N","bdatereq":"Y","age_validation_ind":"Y","min_age":"10","max_age":"25","rel_type_id":"10002"},
  {"id":"3","perf_no":"93","perf_dt":"2017-09-18T10:00:00","customer_no":"2762","fname":"Ryan","lname":"Jones","age":"22","validation_pass_age":"Y","validation_pass_prereq":"Y","ask_birthdate":"Y","ask_current_grade":"N","ask_next_grade":"N","ask_email":"Y","birthdate":"07\/13\/1995","bdatereq":"Y","eaddress":"","age_validation_ind":"Y","min_age":"10","max_age":"25","rel_type_id":"10003"},
  {"id":"4","perf_no":"93","perf_dt":"2017-09-18T10:00:00","customer_no":"2700","fname":"John","lname":"Doe","age":"","validation_pass_age":"Y","validation_pass_prereq":"Y","ask_birthdate":"Y","ask_current_grade":"N","ask_next_grade":"N","ask_email":"Y","birthdate":"02\/22\/1988","bdatereq":"Y","eaddress":"","age_validation_ind":"Y","min_age":"10","max_age":"25","rel_type_id":"10003"}
]

$scope.changedAffiliate = function(selectedItem, cartPerfNo, index, affiliateCustomer_no) {
  $.each($scope.affiliates, function(i) {
    if(this.perf_no === cartPerfNo && this.customer_no === affiliateCustomer_no){
      this['disabled'] = true;
    }
  });
}
$scope.getDisableCondition = function(affiliate, cartPerfNo, index){
  $.each($scope.affiliates, function(i) {
    if(this.disabled === true){
      console.log(this);
      return affiliate.customer_no === this.customer_no;
    }
    if(this.disabled === false || this.disabled === undefined){
      return false;
    }
    //console.log(this);

  });
}


此处的完整柱塞


Full Plunker here

推荐答案

嗯,也许不是很漂亮,但是可以完成工作( plnkr ):

Hmm, maybe it's not pretty, but doing its job (plnkr):

    $scope.passMe;

$scope.changedAffiliate = function(selectedItem, cartPerfNo, index, affiliateCustomer_no) {
  if ($scope.passMe) {
    $scope.affiliates.forEach((aff)=>{
      if (aff.perf_no ==  $scope.passMe[0] && aff.customer_no ==  $scope.passMe[2]) {
        selectedItem[$scope.passMe[0]][ $scope.passMe[1]] = aff
      }
    })
    $scope.passMe = null;
  }
  $scope.passMe = [cartPerfNo,index,affiliateCustomer_no]
  $.each($scope.affiliates, function(i) {
    if(this.perf_no === cartPerfNo && this.customer_no === affiliateCustomer_no){
      this['disabled'] = true;
    }
  });

}

基于这样的事实,即changedAffiliate在被禁用后将再运行1次.

It's based on fact that changedAffiliate will run 1 additional time after being disabled.

这篇关于基于数组中值的ngChange更新禁用ngOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆