Junaid Zubair
Junaid Zubair

Reputation: 55

Why input element is not getting focus?

I'm using ag-grid community edition in angular project. Requirement was to enable inline editing on tab, it was achieved, but there is another requirement which is, when navigating through tab and reached to last column and hit tab key again it should create an empty row and focus to 1st element of new row as well. I'm able to push new data through ag-grid function setRowData(data).

I tried startEditingCell() and setFocusedCell() available in grid api. They do focus on element but cursor in input element does not appear.

import {
    Component, OnInit
} from '@angular/core';
import {GridOptions, StartEditingCellParams} from "ag-grid-community";

@Component({
  selector: 'app-angular-grid',
  templateUrl: './angular-grid.component.html',
  styleUrls: ['./angular-grid.component.css']
})
export class AngularGridComponent implements OnInit {

  gridOptions:GridOptions;
  constructor() {
  }

  ngOnInit() {
    this.initializeGridOption();
  }


  initializeGridOption() {
    this.gridOptions = {
      onGridReady: (params)=> {
        params.api.sizeColumnsToFit();
      },
      rowData: this.rowData,
      columnDefs: this.columnDefs,
      rowSelection: 'multiple',
      onCellEditingStopped: function (params) {
        let rowIndex = params.rowIndex;
        let rowDataLength = this.rowData.length;
        let colId = params.column.getColId();
        if (rowDataLength === rowIndex + 1 && colId === 'price') {
          let data = {id: this.rowData.length + 1, make: '', model: '', price: ''};
          this.rowData.push(data);

          params.api.setRowData(this.rowData);
          setTimeout(() => {
            params.api.startEditingCell({rowIndex: rowDataLength, colKey: 'make'});
            params.api.setFocusedCell(rowDataLength, 'make');

          });
        }
      }
    };
  }

  columnDefs = [
    {
      headerName: '',
      width: 40
    },
    {headerName: 'Make', field: 'make', editable: true},
    {headerName: 'Model', field: 'model', editable: true},
    {
      headerName: 'Price',
      field: 'price',
      editable: true
    }
  ];

  rowData = [
    {id: 1, make: 'Toyota', model: 'Celica', price: 35000},
    {id: 2, make: 'Ford', model: 'Mondeo', price: 32000},
    {id: 3, make: 'Porsche', model: 'Boxter', price: 72000}
  ];
}

Upvotes: 0

Views: 3557

Answers (1)

Paritosh
Paritosh

Reputation: 11560

You don't need to explicitly set the cell focus by setFocusedCell.

Check this sample plunk I've created: Cell Editing

Reach till the last cell and hit tab and observe that the input is already focused.

setTimeout(() => {
  params.api.startEditingCell({rowIndex: rowDataLength, colKey: 'make'});
  //params.api.setFocusedCell(rowDataLength, 'make');
});

Upvotes: 1

Related Questions