This is a migrated thread and some comments may be shown as answers.

Set focus to editable cell using inline edit row

7 Answers 2182 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chau
Top achievements
Rank 1
Chau asked on 24 Jan 2018, 05:47 PM

I have a kendo-grid with 3 columns editable out of the list of 25 columns displayed.

The grid has a cellClick event which sends in the column and columnIndex:

(cellClick)="editClick($event)"

public editClick({ sender, column, rowIndex, columnIndex, dataItem }): void {

    this.editHandler({
                dataItem: dataItem,
                rowIndex: rowIndex,
                sender: sender,
                isNew: false
            });

}
public editHandler({ sender, rowIndex, dataItem }: EditEvent): void {
        if (this.formGroup && !this.formGroup.valid) {
            return;
        }

this.formGroup = formGroup(dataItem);
        this.editedRowIndex = rowIndex;

        console.log('editrowindex =' + rowIndex);
        console.log('edited CDM_Service_Code =' + dataItem.CDM_Service_Code);

        sender.editRow(rowIndex, this.formGroup);

When I call sender.editRow

7 Answers, 1 is accepted

Sort by
0
Chau
Top achievements
Rank 1
answered on 24 Jan 2018, 05:51 PM

When I call sender.editRow, the row is rendered editable for the 3 columns specified in the formGroup.

Is there a way to set the focus to one of those editable cells?

Thanks for any suggestion.

0
Chau
Top achievements
Rank 1
answered on 24 Jan 2018, 05:52 PM

When I call sender.editRow, the row is rendered editable with the 3 columns specified in the formGroup.

Is there a way to set focus to one of those 3 editable cells?

Thanks for any suggestion.

0
Dimiter Topalov
Telerik team
answered on 25 Jan 2018, 12:44 PM
Hello Chau,

You can use the edit event handler to focus the desired input programmatically in a setTimeout callback (to ensure that the edit row's inputs are rendered so you can focus the one you need). A similar approach is demonstrated in the following how to article from our documenattion:

https://www.telerik.com/kendo-angular-ui-develop/components/grid/how-to/focus-clicked-cell-in-row-editing/

It shows how to focus the editor, corresponding to the clicked cell, but you can adapt it slightly by providing the desired column index in the following method:

public editHandler({ sender, colIndex, rowIndex, dataItem }: EditEvent): void {
        if (this.formGroup && !this.formGroup.valid) {
            return;
        }
 
        this.saveRow();
        this.formGroup = formGroup(dataItem);
        this.editedRowIndex = rowIndex;
        sender.editRow(rowIndex, this.formGroup);
        setTimeout(() => {
           // set a custom colIndex and pass it to the method below
          document.querySelector(`.k-grid-edit-row > td:nth-child(${colIndex + 1}) input`).focus();
        });
    }

I hope this helps.

Regards,
Dimiter Topalov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Chau
Top achievements
Rank 1
answered on 25 Jan 2018, 03:52 PM

Thanks Dimiter for your quick reply!

I am getting 2 Build errors:

1) for adding the param colIndex to: public editHandler({ sender, colIndex, rowIndex, dataItem }: EditEvent): void { ... 

===> Build:Type 'EditEvent' has no property 'colIndex' and no string index signature.

2) for calling the focus in this line of code: setTimeout(() => {
            console.log(colIndex);
            document.querySelector(`.k-grid-edit-row > td:nth-child(${colIndex + 1}) input`).focus();
        });

===> Property 'focus' does not exist on type 'Element'

I am using these versions of progress library, are these versions outdated?

 "@progress/kendo-angular-buttons": "^2.0.0",
    "@progress/kendo-angular-dateinputs": "^1.3.0",
    "@progress/kendo-angular-dialog": "^1.3.0",
    "@progress/kendo-angular-dropdowns": "^1.2.2",
    "@progress/kendo-angular-excel-export": "^1.0.4",
    "@progress/kendo-angular-grid": "^1.5.0",
    "@progress/kendo-angular-inputs": "^1.3.1",
    "@progress/kendo-angular-intl": "^1.2.2",
    "@progress/kendo-angular-l10n": "^1.0.4",
    "@progress/kendo-data-query": "^1.0.7",
    "@progress/kendo-drawing": "^1.4.0",
    "@progress/kendo-theme-default": "^2.43.1",

 

1
Dimiter Topalov
Telerik team
answered on 29 Jan 2018, 10:03 AM
Hello Chau,

The described issues are TypeScript-related errors that can be addressed by type casting or removing certain types, e.g.:

1) The EditEvent indeed does not have a colIndex property:

https://www.telerik.com/kendo-angular-ui/components/grid/api/EditEvent/

As the discussed scenario involves custom logic, necessitating the use of colIndex in the editHandler, the most straight-forward approach would be to declare the parameter of type "any":

 editHandler({ sender, colIndex, rowIndex, dataItem }: any): void { ...


... or create a custom type/interface for this purpose only.

2) The type, returned from this method call:

document.querySelector(`.k-grid-edit-row > td:nth-child(${colIndex + 1}) input`)

... is "Element" (as opposed to HTMLElement, as the compiler cannot be sure what type of DOM element will be selected). HTMLElement is a subclass of Element that adds additional methods like "focus()":

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement

You can cast the result of the querySelector() method to HTMLElement type as follows:

(<HTMLElement>document.querySelector('.k...')).focus()


... or:

(document.querySelector('.k...') as HTMLElement).focus()

Regards,
Dimiter Topalov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Nikolaos
Top achievements
Rank 1
answered on 18 Dec 2019, 08:05 AM

Hi Dimiter,

I tried to implement the above solution in order to focus the clicked cell in an edited row.

My problem is that document.querySelector is too slow, takes more than 2 seconds to locate the clicked cell.

Is there another way to focus the clicked cell in edited row?

I am using grid.editRow.

Is there a way to know when editRow is finished?

 

Thanks

0
Svet
Telerik team
answered on 19 Dec 2019, 03:26 PM

Hi Nikolaos,

I opened a new private ticket for this case In order to keep a clean forum history. Let's keep any future communication there.

Regards,
Svetlin
Progress Telerik

Get quickly onboarded and successful with your Telerik and Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
General Discussions
Asked by
Chau
Top achievements
Rank 1
Answers by
Chau
Top achievements
Rank 1
Dimiter Topalov
Telerik team
Nikolaos
Top achievements
Rank 1
Svet
Telerik team
Share this question
or