【问题标题】:Angular-6 based on the select multi-select dropdown show and hide not working properly基于选择多选下拉显示和隐藏的Angular-6无法正常工作
【发布时间】:2021-08-31 05:55:10
【问题描述】:

也许有人会问这个问题,但这并不能解决我对multi-select 的问题。

在我的angular 项目中,密钥的drop-down 包含databasedesktopaccount。基于键的drop-down,值multi-selectdrop-downinputbox将发生变化。

https://stackblitz.com/edit/angular-ivy-kioexw

我的问题:当我将第一行单击为database 时,它会显示multi-select,但如果我选择desktop,则在同一行中database multi-select 不会隐藏。请检查下图。

app.component.ts

import { Component, VERSION } from '@angular/core';
declare var $: any;
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
rowArray: Array<any> = [];
  newRowArray: any = {};
  dbValue = ["mysql", "oracle", "mongo"];
  desktopValue = [
    { id: "1", name: "dell" },
    { id: "2", name: "lenovo" },
    { id: "3", name: "hp" }
  ];
  ngOnInit(): void {
    this.newRowArray = {
      title1: "",
      title2: "",
      dropdownDataDb: [],
      dropdownDataDesktop: [],
      isDropDown: true
    };
    this.rowArray.push(this.newRowArray);
    console.log( $('.multiple-select').multiselect())
  }
  addRow(index) {
    
    this.newRowArray = {
      title1: "",
      title2: "",
      dropdownDataDb: [],
      dropdownDataDesktop: [],
      isDropDown: true,
      isText: false
    };
    this.rowArray.push(this.newRowArray);
    console.log(this.rowArray);
    return true;
  }

  deleteRow(index) {
    if (this.rowArray.length == 1) {
      return false;
    } else {
      this.rowArray.splice(index, 1);
      return true;
    }
  }

  //multiselect code
     multiSelectJquery(){
        setTimeout(()=>{            
             $('.multiple-select').multiselect({
                includeSelectAllOption: false,
                enableFiltering: true,
                includeFilterClearBtn: false,
                enableCaseInsensitiveFiltering: true               
            });
        },1);
    } 
     //multiselect code 

  changed(value: any, index: any) {
    this.multiSelectJquery();
    if (value == 1) {
      this.rowArray[index].isDropDown = true;
      this.rowArray[index].isText = false;
      this.rowArray[index].dropdownDataDb = this.dbValue;
    }

    if (value == 2) {
      this.rowArray[index].isDropDown = true;
      this.rowArray[index].isText = false;
      this.rowArray[index].dropdownDataDesktop = this.desktopValue;
    }

    if (value == 3) {
      this.rowArray[index].isDropDown = false;
      this.rowArray[index].isText = true;
    }
  }
}

app.component.html

<div class="container" style="margin-top: 5%">
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>Action</th>
                <th>key</th>
                <th>value</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor="let dynamic of rowArray; let i = index;">
                <td (click)="deleteRow(i)">
                    <i class="fa fa-trash fa-2x"></i>
                </td>
                <td>
                    <select [(ngModel)]="rowArray[i].title1" class="form-control" #sel (change)="changed(sel.value, i)">
            <option [value]='1'>Database</option>
            <option [value]='2'>Desktop</option>
            <option [value]='3'>Account</option>
          </select>
                </td>
                <td>
                    <!-- show db data -->
                    <select *ngIf="rowArray[i].title1 == 1 && dynamic?.isDropDown" [(ngModel)]="rowArray[i].title2" class="form-control multiple-select"  multiple="multiple">
            <option *ngFor="let data of rowArray[i].dropdownDataDb;">{{data}}</option>
          </select>
                    <!-- show desktop data -->
                    <select *ngIf="rowArray[i].title1 == 2 && dynamic?.isDropDown" [(ngModel)]="rowArray[i].title2" class="form-control">
            <option *ngFor="let data of rowArray[i].dropdownDataDesktop;">{{data?.name ? data?.name : data}}</option>
          </select>

                    <!-- show account data -->
                    <input *ngIf="rowArray[i].title1 == 3 && dynamic?.isText" type="text" [(ngModel)]="rowArray[i].title2" class="form-control">
                </td>

            </tr>
            <tr>
                <td (click)="addRow(0)">
                    <i class="fa fa-plus fa-2x"></i>
                </td>
            </tr>
        </tbody>
    </table>
</div>

请帮我解决这个问题。提前致谢。

【问题讨论】:

    标签: javascript angular angular6 bootstrap-multiselect


    【解决方案1】:

    您可以将代码 &lt;!-- show db data --&gt;&lt;select *ngIf="rowArray[i].title1 == 1 &amp;&amp; dynamic?.isDropDown" [(ngModel)]="rowArray[i].title2" class="form-control multiple-select" multiple="multiple"&gt; &lt;option *ngFor="let data of rowArray[i].dropdownDataDb;"&gt;{{data}}&lt;/option&gt; &lt;/select&gt; 替换为:

    <!-- show db data -->
                    <span *ngIf="rowArray[i].title1 == 1 && dynamic?.isDropDown">
                        <select  [(ngModel)]="rowArray[i].title2" class="form-control multiple-select"  multiple>
            <option *ngFor="let data of rowArray[i].dropdownDataDb;">{{data}}</option>
          </select>
                    </span>
    

    【讨论】:

    • 感谢您的回答,我会尽快检查并更新您!
    猜你喜欢
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2013-09-16
    • 2011-10-10
    相关资源
    最近更新 更多