简体   繁体   English

是否可以在angular4中删除或禁用或隐藏select属性的下拉选项

[英]Is it possible to delete or disable or hide a drop-down option of select attribute in angular4

I have written my code in angular4 and i want to hide or disable a repetitive option that appears in the dropdown. 我已经用angular4编写了代码,我想隐藏或禁用下拉菜单中出现的重复选项。 Can you please let me know the possible way to hide it . 你能告诉我隐藏它的可能方法吗?

I tried implementing codes from - 我尝试从执行代码-

How to Remove duplicate dropdown option elements with same value 如何删除具有相同值的重复的下拉选项元素

在此处输入图片说明

assign.component.ts assign.component.ts

  import * as $ from 'jquery';
  import { JQuery } from 'jquery';


   export class AssignComponent implements OnInit {

   seen = {};

  getRolesList() {
   var url = config.url;
   var port = config.port;
    this.http.post("http:....
   .map(result => this.rolesList = result.json())
   .subscribe((res: Response) => {


 JQuery('.updateMappingUserRole').children().each(function() {
  var txt = JQuery(this).attr('value');
  if (this.seen[txt]) {
      JQuery(this).remove();
  } else {
      this.seen[txt] = true;
  }
  });

  }

assign.component.ts assign.component.ts

    <div class="col-md-6">
          <div class="form-group">
            <label for="role"> Role: </label>
            <select class="form-control" name="updateMappingUserRole"

    [formControl]=
     "updateMappingRoleForm.controls['updateMappingUserRole']"
            [(ngModel)]="updateMappingUserRole" 
      (change)="getRoleID(updateMappingUserRole)" id="role">
            <option  > {{updateMappingUserRole}}</option>
              <option  *ngFor="let i of rolesList">{{i.ROLE_CD}} 
              </option>

              </select>
          </div>
        </div>
      </div>
.map(result => this.removeDuplicates(result.json(), this.rolesList));

removeDuplicates(json: any[], destination: any[]) {
  destination = json.reduce((p, n) => {
    // If role already in array, don't push
    if (!p.includes(n)) { p.push(n); }
    return p;
  }, []);
}

This function will transform your array returned by the HTTP call with the reduce function. 此函数将使用reduce函数转换HTTP调用返回的数组。

EDIT : How reduce works ( documentation ) 编辑:如何reduce工作( 文档

For newcomers in Javascript, or people not knowing the reduce function : 对于Java语言的新手,或不了解reduce函数的人们:

reduce is a function that will iterate over the array and transform it. reduce是将遍历数组并对其进行转换的函数。 Its signature is 它的签名是

reduce(callback(previousElement, nextElement, currentIndex, arr), startingValue);

Let's use an example : transform an array of numbers into their square values. 让我们使用一个示例:将数字数组转换为其平方值。

const initial = [1, 2, 3, 4];
const transformed = initial.reduce((p, n) => {
  p.push(n * n);
  return p;
}, []); // Will give [1, 4, 9, 16]

Now let's break it down : 现在让我们分解一下:

On the first iteration, we are on the first item of the array : 1. 在第一次迭代中,我们在数组的第一项上:1。

The initial value given to the reduce function is an empty array. 提供给reduce函数的初始值为一个空数组。
In the callback, this will give 在回调中,这将给

p = [], n = 1

So, we push the square value of 1 into the array, then return the array (mandatory). 因此,我们将平方值1推入数组,然后返回数组(强制性)。

The next iteration comes : the values of the callback are 下一次迭代是:回调的值是

p = [1], n = 2

We do the same process, and on the third and fourth iterations, we will have this 我们执行相同的过程,在第三和第四次迭代中,我们将拥有

3 : p = [1, 4], n = 3
4 : p = [1, 4, 9], n = 4

Once the function is finished ( nextElement has no more value to take), it returns the last value of previousElement , which is the transformed array. 一旦函数结束( nextElement没有更多的值取),则返回的最后一个值previousElement ,这是变换数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM