简体   繁体   English

如何访问Angular Material表的特定行中的模板变量引用? (角度7)

[英]How to access a template variable reference in a specific row of an Angular Material table? (Angular 7)

I'm currently trying to implement a more complex version of the following behavior, but as I will not have access to my project until Monday, I'll simplify for the sake of it, using an altered example from Angular Material's tutorials. 我目前正在尝试实现以下行为的更复杂的版本,但是由于我要到周一才能访问我的项目,因此,我将使用Angular Material教程中的一个更改示例进行简化。

An Angular Material table is filled with data from a string array. Angular Material表中填充了来自字符串数组的数据。 Data is presented in an input field on the first column. 数据显示在第一列的输入字段中。 On the second column of every row is a set of buttons that, when pressed, change the contents of their line's input. 每行第二列上有一组按钮,按下这些按钮可更改其行输入的内容。

例

I've tried to identify the input element through the use of a template reference variable, but it always turns up as undefined. 我试图通过使用模板引用变量来标识输入元素,但是它始终显示为未定义。 So I assume Angular Material's table is doing something behind my back that changes the scope of said variable. 因此,我假设Angular Material的表在我背后做了一些事情,从而改变了所述变量的范围。

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

    <ng-container matColumnDef="inputColumn">
        <th mat-header-cell *matHeaderCellDef> No. </th>
        <td mat-cell *matCellDef="let element">
            <input #inputRef placeholder="{{element.name}}">
        </td>
    </ng-container>

    <ng-container matColumnDef="buttonColumn">
        <th mat-header-cell *matHeaderCellDef> Name </th>
        <td mat-cell *matCellDef="let element">
            <button (click)="inputRef.value='Changed Element'">Change!</button>
        </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

On my actual project, data will come from an API, and there will also be more buttons that will either enable the input, save or discard changes made to its content and so forth. 在我的实际项目中,数据将来自API,并且还将有更多按钮可以启用输入,保存或放弃对其内容所做的更改等。 But if I manage to get this example to work, then the remainder should be pretty straight forward. 但是,如果我设法使该示例起作用,则其余部分应该很简单。

Any help would be greatly appreciated. 任何帮助将不胜感激。

You can solve this by using ViewChildren to access all the input elements in your table and identifying them via the id attribute for example. 您可以通过使用ViewChildren访问表中的所有输入元素并通过id属性标识它们来解决此问题。

Check out this stackblitz where I threw together a quick solution. 查看堆叠闪电战,在那里我提出了一个快速解决方案。

Basically all I did in the template was to add the matInput directive to your input elements and uniquely identify each one of the via the id attribute (just assigning the inputColumn variable of the current row element ): 基本上,我在模板中所做的就是将matInput指令添加到您的input元素,并通过id属性唯一地标识每个via(只需分配当前行elementinputColumn变量):

<mat-form-field>
  <input matInput id="{{element.inputColumn}}" placeholder="{{element.inputColumn}}">
</mat-form-field>

Added a function call on your button where we pass the current rows element : 在您的按钮上添加了一个函数调用,我们在其中传递了当前的row element

<button mat-raised-button (click)="changeValue(element)">{{element.buttonColumn}}</button>

In the component we retrieve all the input elements with the matInput directive: 在组件中,我们使用matInput指令检索所有input元素:

@ViewChildren(MatInput) matInputs: QueryList<MatInput>;

And then in the function that gets called by the buttons we search for the correct matInput via the id that corresponds to the element 's inputColumn value: 然后,在得到由我们搜索正确的按钮调用的函数matInput通过id对应于elementinputColumn值:

changeValue(element: TableData) {
  const input = this.matInputs.find(matInput => matInput.id === element.inputColumn);
  input.value = 'Changed!';
}

I am sure there are other ways to solve this, not sure how this behaves if you have a lot of buttons/rows. 我确定还有其他方法可以解决此问题,如果您有很多按钮/行,则不确定如何运行。

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

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