IE 11的Angular Ag Grid单击问题

时间:2019-05-07 12:10:31

标签: angular internet-explorer-11 ag-grid ag-grid-angular

我在Angular 5中使用了Ag Grid企业版17.0。我启用了单击编辑选项。一次单击即可在Chrome中完美运行,但在IE 11中却无法正常运行。我必须多次单击才能使该单元格处于编辑模式。谁能帮我解决问题?

请找到以下我用于自定义输入文本的代码。我已经手动添加了click和dblclick事件,以使其在IE 11中正常工作。

this.gridOptions.suppressClickEdit = false;
this.gridOptions.singleClickEdit = true;

自定义输入类型

import { AfterViewInit, Component, ViewChild, ViewContainerRef ,ElementRef } from '@angular/core';

import { ICellEditorAngularComp } from 'ag-grid-angular/main';

@Component({
  selector: 'text-cell',
  template: `<input #input style="width: 100%; height: 100%;" (keydown)="onKeyDown($event)" [(ngModel)]="value" (dblclick) = "$event.target.select()" (click)="handleClick()">
              <button (click)="clear()" style="position:absolute;top:5px;right:2px;cursor:pointer;color:grey;background-color:white;border:none;">
                <span>&#10060;</span>
              </button>`
})
export class TextEditorComponent implements ICellEditorAngularComp, AfterViewInit {
  private params: any;
  public value: number;
  private cancelBeforeStart = false;

  @ViewChild('input', { read: ViewContainerRef }) public input;


  constructor(private elementRef : ElementRef){

  }

  agInit(params: any): void {
    this.params = params;
    this.value = this.params.value;
    // only start edit if key pressed
    this.cancelBeforeStart = false;
  }

  getValue(): any {
    return this.value;
  }

  isCancelBeforeStart(): boolean {
    //return this.cancelBeforeStart;
    return this.params ? this.params.cancelEdit ? this.params.cancelEdit(this.params.node.data) : false : false;
  }

  clear(){
    this.value = null;
    return this.value;
  }

  onKeyDown(event: any): void {
    if (!this.isKeyPressedAlphaNumericOrBackSpace(event)) {
      if (event.preventDefault) {
        event.preventDefault();
      }
    }
  }

  // dont use afterGuiAttached for post gui events - hook into ngAfterViewInit instead for this
  ngAfterViewInit() {
    this.input.element.nativeElement.focus();
  }

  private getCharCodeFromEvent(event: any): any {
    event = event || window.event;
    return (typeof event.which === 'undefined') ? event.keyCode : event.which;
  }

  private isCharAlphaNumeric(charStr: any): boolean {
    return true;

  }

  private isKeyPressedAlphaNumericOrBackSpace(event: any): boolean {
    const charCode = this.getCharCodeFromEvent(event);
    const charStr = event.key ? event.key : String.fromCharCode(charCode);
    return this.isCharAlphaNumeric(charStr) || charStr === 'Backspace' || charStr === 'Delete' || charStr === '.' || charStr === '-';
  }

  handleClick(){    
    setTimeout(()=>{
      this.elementRef.nativeElement.focus();
    },100);    
  }
}

2 个答案:

答案 0 :(得分:0)

尝试将ag-Grid版本升级到the 20.2.0 version,我已经使用此版本的代码创建了一个示例,看来一切正常。

答案 1 :(得分:0)

这是我的解决方案。通过修改onKeyDown和ngAfterViewInit方法,我设法使一些工作正常进行。

@Component({
  selector: 'text-cell',
  template: `<input #input  id="textinput" style="width: 100%; height: 100%;" (keydown)="onKeyDown($event)" [(ngModel)]="value" (dblclick) = "$event.target.select()">
              <button (click)="clear()">
                <span>&#10060;</span>
              </button>`
})
export class TextEditorComponent implements ICellEditorAngularComp, AfterViewInit {

  private params: any;
  public value: number;
  private cancelBeforeStart: boolean = false;
  private iCaretPos : number =  0;

  @ViewChild('input', {read: ViewContainerRef}) public input;

  agInit(params: any): void {
      this.params = params;
      this.value = this.params.value;

      // only start edit if key pressed is a number, not a letter
      this.cancelBeforeStart = false;
  }
  clear() {
    this.value =  null;
    return this.value;
  }


  getValue(): any {
      return this.value;
  }

  isCancelBeforeStart(): boolean {
    return this.params ? this.params.cancelEdit ? this.params.cancelEdit(this.params.node.data) : false : false;
  }

  onKeyDown(event): void {
      var key = event.which || event.keyCode;

      if(key === 37 || key === 39){   
          //Left or right       
        let inputDocument = document.getElementById('textinput');
        const element : HTMLInputElement = <HTMLInputElement>inputDocument;
        event.stopPropagation();                                                  
        let selectionStart = 0;          
        if(key === 39 && this.iCaretPos < element.innerHTML.length+1){               
              selectionStart = this.iCaretPos;
              this.iCaretPos = this.iCaretPos +1;
        }else if(key === 37 && this.iCaretPos !== 0){               
              selectionStart = this.iCaretPos;
              this.iCaretPos = this.iCaretPos -1;
        }  
        let selectionEnd = this.iCaretPos;          
          if (element.setSelectionRange) {                                          
              element.focus();
              element.setSelectionRange(selectionStart, selectionEnd);
          }        
      }
      if (!this.isKeyPressedText(event)) {
          if (event.preventDefault) event.preventDefault();
      }

  }

  // dont use afterGuiAttached for post gui events - hook into ngAfterViewInit instead for this
  ngAfterViewInit() {
      window.setTimeout(() => {
          this.input.element.nativeElement.focus();
          if(document.getElementById('textinput') != null){
            document.getElementById('textinput').innerHTML = this.input.element.nativeElement.value;
          }

      })
  }

  private getCharCodeFromEvent(event): any {
      event = event || window.event;
      return (typeof event.which == "undefined") ? event.keyCode : event.which;
  }

  private isCharText(charStr): boolean {
      return true;
  }

  private isKeyPressedText(event): boolean {
    const charCode = this.getCharCodeFromEvent(event);
    const charStr = event.key ? event.key : String.fromCharCode(charCode);
    return this.isCharText(charStr) || charStr === 'Backspace' || charStr === 'Delete' || charStr === '.' || charStr === '-';
  }

}