简体   繁体   English

Angular 2+从事件(ag网格)访问类变量

[英]Angular 2+ accessing class variables from event (ag grid)

I'm trying to create a button that would edit a class' boolean to true upon being clicked. 我正在尝试创建一个按钮,当单击该按钮时会将一个类的布尔值编辑为true。 The code looks like this: 代码如下:

export class componentName{

    public editingMode = false;
    private colDefinitions = [
    {
        headername: "Edit",
        field: "edit",
        cellRenderer: this.editCellRendererFunc
    }];


    editCellRendererFunc(params){
        element.addEventListener('click', () => {
            // Need to modify editingMode from here.
        }
    }

    constructor(private _svc:GridHTTP){
            this.gridOptions = <GridOptions>{};
            this.gridOptions = {
                enableFilter: true,
                columnDefs: this.colDefinitions
            }
    }

I understand that I can't access the pointer anymore. 我知道我无法再访问指针。 Is there any method of having this variable be accessible so I can use an *ngIf in the template.html in order to hide/show DOM elements whenever that button is clicked? 是否有任何方法可以访问此变量,所以无论何时单击该按钮,我都可以在template.html中使用* ngIf来隐藏/显示DOM元素?

usually what i do if a want to have a EDITABLE TABLE (or smilar) is like this: 通常,如果要拥有可编辑表(或笑脸),我该怎么做:

1 - create a edit : boolean field in your model ... so for example: 1-在模型中创建一个edit:boolean字段...例如:

export class Use{

public id: number;
public name:string;
 // ... other prop
public edit: boolean //put this .. maybe you can have in a entitybase and then extends ALL your models from tit
}

2 - in your HTML: 2-在您的HTML中:

<table>


 <th>ID</th>
 <th>NAME</th>
 <th> OTHER </th>

 <tbody>
    <tr *ngFor="let row of rows" (click)="editable(row)">
    <td *ngIf="!row.edit" [innerText]="row.id"></td>
    <td *ngIf="row.edit"><input type="number" step="1" [(ngModel)]="row.id" /> </td>
    <td *ngIf="!row.edit" [innerText]="row.name"></td>
    <td *ngIf="row.edit"><input type="text"[(ngModel)]="row.name" /> </td>
    <td *ngIf=""><input type="number" step="1" [(ngModel)]="row.id" /> </td>
    </tr>
    </tbody>
</table>

3- in your ts file (component) 3-在您的ts文件中(组件)

editable(item : User){

//first make all edit=false
this.rows.foreach(xx=>xx.edit=false);

//then set edit=true the clicked row
row.edit=true;

}

..matìybe then you have to make a directive to make all rows.edit=false when YOU CLICK OUTSIDE the table (you can find some example on stackoverflow) ..matìybe,那么当您点击表的外部时,您必须做出指令以使所有行都变为.edit = false(可以在stackoverflow上找到一些示例)

Hope it helps you!! 希望对您有帮助!!

Ag grid allows for custom parameters apparently, doing as follows allows access to the class scope: Ag网格显然允许自定义参数,执行以下操作可以访问类范围:

            // under columnDefs
            {
                headerName: "Edit",
                field: "edit",
                cellRenderer: this.editCellRendererFunc,
                cellRendererParams: {
                    _self: this;
                }
            }

    // Later in the code
     editCellRendererFunc(params) {
          params._self.changeEditingMode(true);    
     }

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

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