Skip to content
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

Keep order of selection if multiple items were selected in input_selectize #1751

Open
DvGils opened this issue Oct 31, 2024 · 0 comments
Open

Comments

@DvGils
Copy link

DvGils commented Oct 31, 2024

Hi!

I noticed Shiny will not keep the order of items that you set in the selected argument. For my use case this ordering matters. The following code will set the order to "A", "C", "D" instead of "C", "A", "D"

from shiny import App, ui, render, reactive

app_ui = ui.page_fluid(
    ui.h3("Order of Selected Items in ui.input_selectize()"),
    ui.input_selectize(
        "my_selectize",
        "Choose items:",
        choices=["A", "B", "C", "D"],
        selected=["C", "A", "D"],
        multiple=True,
    ),
)


def server(input, output, session): ...


app = App(app_ui, server)

Pretty sure the issue lies in:

#_input_selectize.py
def _render_choices(
    x: _SelectChoices, selected: Optional[str | list[str]] = None
) -> TagList:
    result = TagList()

    if x is None:
        return result

    for k, v in x.items():
        if isinstance(v, Mapping):
            result.append(
                tags.optgroup(
                    *(_render_choices(cast(_SelectChoices, v), selected)), label=k
                )
            )
        else:
            is_selected = False
            if isinstance(selected, list):
                is_selected = k in selected
            else:
                is_selected = k == selected

            result.append(tags.option(v, value=k, selected=is_selected))

    return result

Is there a chance this can be fixed?

An additional note; it is strange to me that input.my_selectize() will return a tuple, but the selected will not accept a tuple, only a list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant