直接跳到内容

SelectionList / Selection 多选列表

SelectionList 是支持多选的列表组件,Selection 是其子选项组件。用户可以通过空格键切换每项的选中状态,适用于多项选择的表单场景。

底层:Textual SelectionList

基本用法

vue
<template>
  <VBox>
    <SelectionList ref="list_ref" @selection_list_selected_changed="on_change">
      <Selection prompt="Python" value="py" />
      <Selection prompt="JavaScript" value="js" :initial_state="True" />
      <Selection prompt="Rust" value="rs" />
    </SelectionList>
    <Label :label="f'已选: {selected_str.value}'" />
  </VBox>
</template>

<script lang="py">
from vuepy import ref

list_ref = ref(None)
selected_str = ref("")

def on_change(event):
    if list_ref.value:
        selected_str.value = str(list_ref.value.unwrap().selected)
</script>

SelectionList Props

SelectionList 无特殊 Props,通过子 Selection 组件声明选项。

Selection Props

属性类型默认值说明
promptstr必填,选项显示文字
valueanyprompt 相同选项的绑定值,选中后通过 .selected 返回此值
initial_stateboolFalse初始是否处于选中状态

v-model

  • SelectionListv-model,通过 ref 访问 .selected 获取选中值列表
  • Selectionv-model 默认绑定属性:prompt

事件

事件说明
selection_list_selected_changed选中项列表发生变化时触发(对应 Textual SelectionList.SelectedChanged

通过 ref 访问

通过 ref 获取 SelectionList 实例后,可访问:

属性 / 方法说明
.selected当前所有选中项的 value 列表
.clear_selections()清除所有选中状态
.select_all()选中所有选项
vue
<template>
  <VBox>
    <SelectionList ref="lang_list">
      <Selection prompt="Go" value="go" />
      <Selection prompt="Python" value="py" :initial_state="True" />
      <Selection prompt="Java" value="java" />
    </SelectionList>
    <HBox>
      <Button label="全选" @click="select_all()" />
      <Button label="清空" @click="clear_all()" />
    </HBox>
  </VBox>
</template>

<script lang="py">
from vuepy import ref

lang_list = ref(None)

def select_all():
    if lang_list.value:
        lang_list.value.unwrap().select_all()

def clear_all():
    if lang_list.value:
        lang_list.value.unwrap().clear_selections()
</script>

通用属性

所有 Textual-vuepy 组件均支持以下属性:

属性类型说明
idstr组件 CSS ID
stylestrTCSS 内联样式
classstrTCSS 类名(空格分隔)
border_titlestr边框标题
border_subtitlestr边框副标题
SelectionList / Selection 多选列表已经加载完毕