Skip to content

GridPro: Extend the built-in select editor with options of any type #7556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dnDeveloper opened this issue Jun 3, 2025 · 0 comments
Open
Labels
enhancement New feature or request vaadin-grid-pro

Comments

@dnDeveloper
Copy link

Describe your motivation

For select editor I want to use options of any type and do not want to handle String options in ItemUpdater. Instead I want to use a label generator for the options.

Describe the solution you'd like

I use the approach used for enums in EditColumnConfigurator and generalize it.

    /**
     * Configures the column to have a select editor with the given item updater and options.
     *
     * @param itemUpdater          the callback function that is called when item is changed. It receives two arguments:
     *                             item and newValue.
     * @param optionLabelGenerator used to get the string representation for each option
     * @param options              options provided for the select editor
     * @param <O>                  the type of options
     * @return the configured column
     */
    public <O> Column<T> select(
      ItemUpdater<T, O> itemUpdater,
      Function<O, String> optionLabelGenerator,
      Collection<O> options)
    {
        Map<String, O> optionValues = new HashMap<>();
        List<String> optionLabels = new ArrayList<>();
        for (O option : options)
        {
            String optionLabel = optionLabelGenerator.apply(option);
            if (optionValues.containsKey(optionLabel))
            {
                throw new IllegalArgumentException("Options "
                                                   + optionValues.get(optionLabel) + " and " + option
                                                   + " both have the same string representation: "
                                                   + optionLabel);
            }
            optionValues.put(optionLabel, option);
            optionLabels.add(optionLabel);
        }

        ItemUpdater<T, String> wrapper = (item, newOptionLabel) ->
          itemUpdater.accept(item, optionValues.get(newOptionLabel));

        return select(wrapper, optionLabels);
    }

Describe alternatives you've considered

#7409 is a solution only for enum type, but with a bigger interface, and is included in this solution.

Additional context

No response

@yuriy-fix yuriy-fix added enhancement New feature or request vaadin-grid-pro labels Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request vaadin-grid-pro
Projects
None yet
Development

No branches or pull requests

2 participants