【问题标题】:Get last clicked element in multiple select Angular在多选Angular中获取最后点击的元素
【发布时间】:2019-12-04 14:12:16
【问题描述】:

我试图在多选中获取最后单击的选项,无论该选项是选中还是未选中。 问题是选择不是通过模板,而是通过 TS 动态安装的。

我尝试在创建选项时添加 vanilla JS 事件侦听器,但不起作用。实际上我可以得到所有选定的元素,但是我失去了未选定的选项,我不能完全得到新的选定元素。

我的 HTML

<tr *ngFor="let communityLine of communityLines">
      <td>{{communityLine.name}}</td>
      <td>{{communityLine.instrument.name}}</td>
      <td>{{communityLine.param.name}}</td>
      <td>{{communityLine.contextSource.name}}</td>
      <td>{{communityLine.sampleType.name}}</td>
      <td>{{communityLine.value}}</td>
      <td>

        <select multiple [id] ="communityLine.apiKey" (change)="eventGetChange(communityLine, $event)" [(ngModel)]="nodeKey">
        </select>
      </td>
    </tr>

我的 TS 功能

private eventGetChange(commLineKey, event) {
    console.log(this.nodeKey);
    console.log(commLineKey);
    console.log(event.target.value)

我挂载选择的 TS 方法有点复杂,因为我需要显示所有节点(存储在 this.allNodes var 中)但选择其他数组中的节点(nodesInRelation var)。

private mountSelect(nodesInRelation: Node[], lineApiKey: String): void {
    let select = <HTMLSelectElement>document.getElementById(lineApiKey);
    let copy = this.allNodes;
    for (let node of nodesInRelation) {
      copy.forEach((item, index) => {
        if (item.name === node.name) copy.splice(index, 1);
      });
    }
    for (let node of nodesInRelation) {
      let newoption = new Option(node.name, node.apiKey, null, true);
      select.add(newoption);
    }

    for (let node of copy) {
      let newoption = new Option(node.name, node.apiKey, null, false);
      select.add(newoption);
    }
    M.updateTextFields();
    M.AutoInit();
  }

在 eventGetChange 函数的第一个 console.log 中,我得到所有当前选定的值,在第二个中,我得到键并且没问题,在第三个中,我只得到框中的第一个选定元素。

我只想要最后一次点击、选择或取消选择。

谢谢。

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    您似乎无缘无故地放弃了 Angular 并选择了直接 DOM 操作。 Angular 完全能够以编程方式填充选项列表。它可能看起来像

          <td>
            <select multiple [id] ="communityLine.apiKey" [(ngModel)]="nodeKey">
                <option *ngFor="let option of optionList; let i = index"
                   [value]="option" (click)="eventGetChange(option)">{{ option }}
                </option>
            </select>
          </td>
    
    optionList: any[];
    
    private mountSelect(nodesInRelation: Node[], lineApiKey: String): void {
    // populate optionList here
    }
    
    private eventGetChange(commLineKey) {
    // the clicked option is available here
    }
    

    【讨论】:

    • 但是在我的 Node 对象中我没有 selected 属性...也许我需要做一些深刻的改变。谢谢你
    • 并且optionList在每个communityLine循环中都会不同
    【解决方案2】:

    如果 this.nodeKey 像你说的那样存储一个包含所有当前选定值的数组,你可以简单地这样做:

    this.nodeKey[this.nodeKey.length - 1]
    

    它会给你 nodeKey 数组中的 las 值

    【讨论】:

    • 这不起作用,因为我没有得到最后一个未选择的元素,如果用户选择了在下一个选择之前的元素,我会得到下一个选择。
    • 我的意思是,如果您有一个未选中所有选项的选择,然后单击第三个,然后单击第二个,您将获得第三个
    • 如果 nodeKey 包含所有选择的值,并且您在更改事件上对其进行评估,它应该获取当前选择值,如果没有,那么您错误地定义了该变量的工作
    • 无论如何@mbojko 会更好地解决这个问题
    • 是的,节点键有所有选中的值,但是我丢失了最后一个未选中的值。
    猜你喜欢
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2022-01-07
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    相关资源
    最近更新 更多