skip to Main Content

The following allows me to have the first available cell focused by default:

  useEffect(() => {
    if (
      editMode &&
      gridRef.current?.api &&
      ppsColumns &&
      ppsColumns.length > 0 &&
      lastSelected === undefined
    ) {
      const value = ppsColumns[0].field;

      gridRef.current.api.setFocusedCell(0, value);
    }
  }, [editMode, gridRef.current]);

however, I would like to trigger a double click event instead of setting a focus state which enables the user to edit the cell immediately. Is there some sort of api that allows to do that?

2

Answers


  1. You could modify your useEffect to start editing the first cell when your conditions are met:

    useEffect(() => {
      if (
        editMode &&
        gridRef.current?.api &&
        ppsColumns &&
        ppsColumns.length > 0 &&
        lastSelected === undefined
      ) {
        const firstColumnField = ppsColumns[0].field;
    
        // Focusing the cell is still necessary before starting to edit
        gridRef.current.api.setFocusedCell(0, firstColumnField);
    
        // Now, start editing the focused cell
        gridRef.current.api.startEditingCell({
          rowIndex: 0,
          colKey: firstColumnField,
          // Optional parameters for editing, like keyPress and charPress, can be added here
        });
      }
    }, [editMode, gridRef.current, ppsColumns, lastSelected]);
    
    Login or Signup to reply.
  2. I use setTimeout to work around timing issues, try adjust the delay duration (100 ms in the example)

     useEffect(() => {
      const startEdit = () => {
        if (
          editMode &&
          gridRef.current?.api &&
          ppsColumns &&
          ppsColumns.length > 0 &&
          lastSelected === undefined
        ) {
          const firstColumnField = ppsColumns[0].field;
    
          gridRef.current.api.setFocusedCell(0, firstColumnField);
    
          setTimeout(() => {
            gridRef.current.api.startEditingCell({
              rowIndex: 0,
              colKey: firstColumnField,
            });
          }, 100); // delay in ms, adjust as necessary
        }
      };
    
      startEdit();
    }, [editMode, gridRef.current, ppsColumns, lastSelected]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search