multiSelectListBox 多选列表

multiSelectListBox 支持用户通过复选框在一个可滚动的列表中选择多个数据。

XML 元素

multiSelectListBox

Java 类

JmixMultiSelectListBox

基本用法

使用 itemsContainer 或 itemsEnum 创建选项列表:

XML
<vbox width="AUTO">
    <h4 text="Single-select ListBox"/>
    <listBox id="listBox"/>
</vbox>
<vbox width="AUTO">
    <h4 text="Multi-select ListBox"/>
    <multiSelectListBox id="multiSelectListBox"/>
</vbox>
Java
@ViewComponent
protected JmixListBox<String> listBox;
@ViewComponent
protected JmixMultiSelectListBox<String> multiSelectListBox;

@Subscribe
protected void onInit(InitEvent event) {
    List<String> items = List.of("CSS", "HTML", "Java", "JavaScript", "JSON", "Kotlin", "XML");
    listBox.setItems(items);
    multiSelectListBox.setItems(items);

    listBox.setValue("Java");
    multiSelectListBox.select("Java", "Kotlin");
}

选项状态 Provider

itemEnabledProvider 支持按特定条件动态控制单个选项是否启用。禁用的选项显示灰色,无法被用户选中。所有选项默认启用。

@Install(to = "multiSelectListBox", subject = "itemEnabledProvider")
private boolean multiSelectListBoxItemEnabledProvider(final User user) {
    if (user != null) {
        return user.getActive();
    }
    return true;
}

自定义选项标签

itemLabelGenerator 支持自定义选项在 multiSelectListBox 组件中的显示标签。可以控制用户看到的文本,能以更友好的方式或上下文更相关的方式展示选项。

@Install(to = "multiSelectListBox", subject = "itemLabelGenerator")
private String multiSelectListBoxItemLabelGenerator(final User item) {
    return metadataTools.format(item.getDisplayName()).toUpperCase();
}

选项渲染

可以自定义选项的渲染方式。渲染器在每个选项应用,返回一个表示该选项的组件。

可以使用 setRenderer() 方法或 @Supply 注解实现。

@Supply(to = "mSelectListBox", subject = "renderer")
private ComponentRenderer<Span, User> mSelectListBoxRenderer() {
    return new ComponentRenderer<>(user -> {
        Span span = uiComponents.create(Span.class);
        span.setText(user.getDisplayName());
        span.setClassName("font-bold");
        return span;
    });
}

或者,还可以使用嵌套的 fragmentRenderer 元素来渲染选项。更多信息请参考 Fragment 渲染器 部分。

XML 属性

下面是 multiSelectListBox 的特殊属性:

名称 描述 默认值

itemsContainer

设置选项数据容器。

—

itemsEnum

设置选项枚举。

—

下面是 multiSelectListBox 支持的其他通用属性:

事件和处理器

下面是 multiSelectListBox 的特殊处理方法:

名称 描述

itemEnabledProvider

itemEnabledProvider 应用于 multiSelectListBox 的每个选项,确定选项启用(true)还是禁用(false)。参阅 选项状态 Provider。

itemLabelGenerator

支持自定义 multiSelectListBox 中每个选项显示的标签。参阅 自定义选项标签。

renderer

为 multiSelectListBox 设置选项渲染器。参阅 选项渲染。

下面是 multiSelectListBox 支持的其他通用事件:

XML 内部元素

一个 multiSelectListBox 可以包含 fragmentRenderer 和 tooltip 作为其内部元素。

参考