【问题标题】:Angular material , to select mat-list-option and show the respective data of the selected mat-list-optionAngular material , 选择 mat-list-option 并显示所选 mat-list-option 的相应数据
【发布时间】:2020-06-29 21:59:27
【问题描述】:

当用户将鼠标悬停在表格的第一列上时,会出现一个工具提示,底部有一个按钮。

点击按钮后,角垫对话框打开,其中第一列数据位于垫对话框的左侧。

我需要完成两件事

1) 在 mat-dialog 的左侧,mat-list-option 应默认选中,具体取决于用户悬停在哪一行并单击其工具提示按钮。

2) mat-dialog 的右侧需要填充与左侧选择的 mat-list-option 相关的“conditionals”、“offset”、“alert”属性数据。

例如:在表格中,用户已将鼠标悬停在 1 分钟数据上(在警报列下)并在工具提示打开后单击按钮,在 mat-dialog 中应选择 1 分钟数据行并打开右侧应加载其各自的“条件”、“偏移”、“警报”数据,因此应根据所选的 mat-list-option 加载其各自的“条件”数据。

我已经强制一次只能选择一个 mat-list-option

alert-dialog.component.html

<div [ngStyle]="{'width':'50%','border':'1px solid yellow','margin-right':'15px','height':'100%'}">
        <h3>LEFT</h3>
        <div class="alert-select">
            <mat-selection-list #preDefAlertList>
                <mat-list-option *ngFor="let preDef of data.data" [value]="preDef">
                  {{preDef.Alert}}
                </mat-list-option>
            </mat-selection-list>
        </div>  
</div>

<div [ngStyle]="{'width':'100%','border':'1px solid red'}">
        <h3>Edit JSON</h3>
        <div class="makeEditable" contenteditable="true">
            {{preDef.conditionals | json}}
        </div>
 </div>

tooltip-overview-example.component.ts 有表格,一旦用户点击工具提示中的按钮,数据就会被传递到 alert-dialog.component.ts。

Stackblitz 链接 https://stackblitz.com/edit/angular-mat-tooltip-rzstlk?file=app%2Ftooltip-overview-example.ts

【问题讨论】:

  • 基本上我必须在 mat-dialog 的左侧显示预先选择的 mat-list-option,在 mat-dialog 的右侧显示相同的 json 数据(在工具提示中显示)但是当用户选择一些不同的 mat-list-option 时,它们各自的“条件”、“偏移”、“警报” json 数据

标签: javascript angular angular7 angular-material2


【解决方案1】:

首先,在左侧列表中,您必须设置将其绑定到您的模型的mat-selection-list 的选定值:[(ngModel)]="incomingSelectedAlert"

然后在mat-list-options 中使用索引作为列表项的值:let i = index" [value]="i" 并突出显示所选项目设置其类:[ngClass]="i==incomingSelectedAlert ? 'selected-option' : ''"

这样整个mat-selection-list 看起来像这样:

<mat-selection-list #preDefAlertList [(ngModel)]="incomingSelectedAlert">
  <mat-list-option *ngFor="let preDef of data.data; let i = index" [value]="i" 
    [ngClass]="i==incomingSelectedAlert ? 'selected-option' : ''">
      {{preDef.Alert}}
    </mat-list-option>
</mat-selection-list>

在 CSS 文件中添加选定的选项样式:

.selected-option {
  background-color: yellow;
}

返回模板文件,只需更改右侧 JSON 数据的来源,使其始终显示所选项目的条件:

<div class="makeEditable" contenteditable="true">
  {{data.data[incomingSelectedAlert].conditionals | json}}
</div>

如您所见,您几乎拥有它。我在这里分叉并更改了您的 Stackblitz:https://stackblitz.com/edit/angular-mat-tooltip-pjq4ve

更新

根据您的 cmets,我制作了另一个 Stackblitz:https://stackblitz.com/edit/angular-mat-tooltip-fhh3gr

mat-list-options 值现在来自项目 id 属性:&lt;mat-list-option *ngFor="let preDef of data.data" [value]="preDef.id"

为了获得条件,它调用一个方法:

<div class="makeEditable" contenteditable="true">
    {{getSelectedConditionals() | json}}
</div>

在模型中:

getSelectedConditionals() {
   return this.data.data.find(x => x.id == this.incomingSelectedAlert).conditionals;
}

【讨论】:

  • 谢谢,但它不适用于分页,在我提供的堆栈闪电战中,分页也不起作用,如果你能建议如何用分页处理索引。
  • 我尝试过传递 (click)="onClick($event,(this.paginator.pageIndex == 0 ? i : i + this.paginator.pageIndex * this.paginator.pageSize))按钮单击,但在我的应用程序中它不起作用
  • 是的,事实上你不应该传递索引,因为它在数组之间变化。相反,您应该有一些独特的属性,例如,“_id”(然而,它似乎不是唯一的)属性并将其传递给对话框。这里已经很晚了,但如果您到那时还没有找到解决方案,我可以在明天尝试帮助您。
猜你喜欢
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多