【问题标题】:How to add Angular 10 Component to an Ag-Grid cell如何将 Angular 10 组件添加到 Ag-Grid 单元格
【发布时间】:2021-02-03 18:53:41
【问题描述】:

我有一个 Angular10 组件(切换开关),我需要将它包含在我的 ag-grid(单元格)的特定列中。

目前我有一个标准的 html 复选框,如下所示:

colDefs: ColDef[] = [
    { field: 'Name',             headerName: 'Name'},
    { field: 'Somethingelse',    headerName: 'Something else'},
    { field: 'Checkbox',         headerName: 'A Checkbox',      //<--this one
        editable:true,    
        checkboxSelection: false,   
        headerCheckboxSelection: false,   
        filter: false,    
        sortable: false,    
        cellRenderer: function (params: { value: boolean; }) {. //<--draws this
          var input = document.createElement("input");
          input.type = "checkbox";
          input.checked = params.value;
          return input;            //<--want to draw my component here instead
    } }
  ];

我想使用的组件而不是复选框已经存在于我的项目中:

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'my-toggle-switch',
  templateUrl: './toggle-switch.component.html',
  styleUrls: ['./toggle-switch.component.scss']
})
export class ToggleSwitchComponent implements OnInit {
  selected: boolean = false;
  constructor() { }
  ngOnInit(): void {}
}

如何包含 Angular10 组件来代替复选框?

【问题讨论】:

    标签: angular ag-grid angular10


    【解决方案1】:

    首先在 AG Grid 组件文件中导入您的组件。

    import { ToggleSwitchComponent } from './ToggleSwitchComponent '; // whatever is the path
    

    coldef 中排名第二,只需将您的渲染器分配给cellRenderer

      { 
            field: 'Checkbox',
            headerName: 'A Checkbox',
            editable:true,    
            checkboxSelection: false,   
            headerCheckboxSelection: false,   
            filter: false,    
            sortable: false,    
            cellRenderer: 'ToggleSwitchComponent'
        } }
    

    第三次在frameworkComponents下注册你的组件

     this.frameworkComponents = {
          ToggleSwitchComponent: ToggleSwitchComponent,
    ...
    ...
    }
    

    这就是渲染自定义组件所需的全部内容。

    【讨论】:

      【解决方案2】:
      Few custom classes you need to add as per your need. Hope you can customize it by yourself. If you need all files let me  know I will send you generic files.  
      
      <table class="table table-bordered table-hover">
              <thead>
                <tr>
                  <th width="2%"></th>
                  <th width="20%">Rule Name</th>
                  <th width="10%">Rule ID</th>
                </tr>
              </thead>
              <tbody>
                <tr *ngFor="let item of gridItems">
                  <td width="2%">
                   <generic-checkbox [chkbxoptid]="'ruleId'"
                    [data]="item.ruleInfo"
                    ></generic-checkbox>
                  </td>
                  <td width="20%">{{item.ruleInfo.ruleName}}</td>
                  <td width="10%">{{item.ruleInfo.ruleId}}</td>
                </tr>
              </tbody>
            </table>
          
          
          genericcomponent.ts
          import { Component, Input, OnInit, OnDestroy } from '@angular/core';
          
          import { TVUIConstants } from '../utils/tvconstants';
          import { Subscription } from 'rxjs';
          import { Rule } from '../models/rule-models';
          import { BaseServices } from '../services/base.service';
          import { UIDGenerator } from '../services/uid-generator.service';
          @Component({
            selector: 'generic-checkbox',
            template: `<input type="checkbox" [(ngModel)]=
            "isChecked"  *ngIf="hideCheckBox"
            value="{{isChecked}}" (click)='checkClickHandler()'>`
          
          })
          
          export class ChkBoxComponent implements OnInit, OnDestroy {
              @Input() chkbxoptid:string;
              @Input() multiselectable:boolean = true;
              @Input() set data(value:any){
                if(value === undefined ||
                  (this.chkbxoptid
                    && value
                    && value[this.chkbxoptid] === undefined)){
                  this._hideCheckBox = false;
                }else{
                  this._data = value;
                  this._hideCheckBox = true
                }
              }
              @Input() rowindex:number;
              private isChecked:boolean;
              private _subscription: Subscription;
              private _hideCheckBox:boolean;
              private _data:any;
              private _chkbxunqid:string;
          
              constructor(private baseService:BaseServices,
                          private uidGenerator:UIDGenerator) {
                this._subscription = this.baseService.dataEvent.dataObserver.subscribe(data => {
                  if(data.subevnttype === TVUIConstants.CLEAR_CHKBX_REF
                    && data.cid !== undefined
                    && data.cid !== this._chkbxunqid){
                    this.isChecked = data.value;
                    if(this._data)
                      this._data.isChecked = data.value;
                  }else if(data.subevnttype === TVUIConstants.REFRESH_RULES){
                    if(this.isChecked && this.isChecked === true){
                      this.isChecked = false;
                    }
          
                  }
                })
               }
          
              ngOnInit() {
                if(!this.multiselectable){
                 // this._chkbxunqid = this.uidGenerator.getUnqUID("chkbx_");
                }
                if(this._data && this._data.isChecked === true)
                           this.isChecked = this._data.isChecked;
              }
          
              private checkClickHandler():void{
                this.isChecked = !this.isChecked;
                this._data.isChecked = this.isChecked;
          
                if(!this.multiselectable && this.isChecked){
                 // this.baseService.dataEvent.publishEvt({subevnttype: TVUIConstants.CLEAR_CHKBX_REF, cid: this._chkbxunqid, value: false})
                }
              }
          
              get hideCheckBox(){
                return this._hideCheckBox;
              }
          
              ngOnDestroy() {
                // prevent memory leak when component destroyed
                this._subscription.unsubscribe();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2020-11-16
        • 2021-08-20
        • 2019-05-13
        • 2020-10-30
        • 2019-06-01
        • 1970-01-01
        • 2016-12-10
        • 2021-11-01
        • 2021-03-01
        相关资源
        最近更新 更多