DataGrid editor how to select all text of focused cell when opening editor

Hi

When user double click on a cell (or press enter while in it) of a DataGrid with buffered editor, the cell is getting focus, like below.

image

What I want to do is select all text of the field when this happens, like below. Which means first input will erase whole field without additional action from user.

image

I was thinking about getting the focused editor Field and call TextField.selectAll().

I know how to get editor fields through DataGrid.EditorOpenEvent.getFields(), but I don’t know how to get the one being focused.

Advise welcome.

cuba 6.10.9

Best Regards
Michael

Hi,

To meet your requirement, you can unwrap CUBA component to expose it as a Vaadin component and add the focus listener:

import com.company.sales.entity.Product;
import com.haulmont.cuba.gui.components.DataGrid;
import com.haulmont.cuba.gui.components.Field;
import com.haulmont.cuba.gui.components.TextInputField.TextSelectionSupported;
import com.haulmont.cuba.gui.screen.*;
import com.vaadin.event.FieldEvents.FocusNotifier;

import java.util.Map;


@UiController("sales_Product.browse")
@UiDescriptor("product-browse.xml")
@LookupComponent("productsTable")
@LoadDataBeforeShow
public class ProductBrowse extends StandardLookup<Product> {

    @Subscribe("productsDataGrid")
    private void onProductsDataGridEditorOpen(DataGrid.EditorOpenEvent event) {
        Map<String, Field> fields = event.getFields();
        for (Field field : fields.values()) {
            if (field instanceof TextSelectionSupported) {
                field.unwrap(FocusNotifier.class)
                        .addFocusListener(focusEvent ->
                                ((TextSelectionSupported) field).selectAll());
            }
        }
    }
}

Regards,
Gleb

Works great, thanks @gorelov

Best Regards
Michael