How to auto focus grid cell for new data row in edit.

1 Answer 1495 Views
Grid
NETe2
Top achievements
Rank 1
NETe2 asked on 24 Feb 2022, 06:11 AM | edited on 24 Feb 2022, 06:12 AM

Hello 
I'm trying to do focus grid cell textbox after new data row is adding.
In KendoReact data grid sample for editField, there is no auto focus the first cell Name after clicking  Add new 
https://www.telerik.com/kendo-react-ui/components/grid/editing/

For another use case we would like to use is to auto focus the first cell Product Name after clicking  Edit 
https://www.telerik.com/kendo-react-ui/components/grid/editing/editing-custom/

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 24 Feb 2022, 07:49 AM

Hi,

For achieving the desired behavior you can handle the "cellRender" of the Grid and conditionally focus the editor for the field that you want to focus as shown in the following example:

  const cellRender = (td, props) => {
    if (props.field != 'ProductName') {
      return td;
    }
    return (
      <td
        {...td.props}
        ref={(td) => {
          const input = td && td.querySelector('input');
          const activeElement = document.activeElement;
          const isButton = activeElement.className.indexOf('k-button') >= 0;
          if (
            !input ||
            !activeElement ||
            input === activeElement ||
            (!activeElement.contains(input) && !isButton)
          ) {
            return;
          } else {
            setTimeout(() => {
              input.select();
            });
          }
        }}
      ></td>
    );
  };

Note that the above uses the "input.select()", because the editor is input element.

Hope this helps.

 

Regards,
Konstantin Dikov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

NETe2
Top achievements
Rank 1
commented on 25 Feb 2022, 08:04 AM

Appreciate your sample and quick response.
Thank you.

Tags
Grid
Asked by
NETe2
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or