Single Select

单选组件要求用户从列表中选择一个项目。它类似于简单的下拉框实现。以下列表显示了一个示例:

A single select component asks a user to choose one item from a list. It is similar to a simple dropbox implementation. The following listing shows an example:

Unresolved include directive in modules/ROOT/pages/components/ui/singleselect.adoc - include::../../../../../src/test/java/org/springframework/shell/docs/UiComponentSnippets.java[]

以下屏幕截图显示了单选组件的典型输出:

The following screencast shows typical output for a single select component:

{"version": 2, "width": 85, "height": 8, "timestamp": 1645645867, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
[2.193321, "o", "java -jar spring-shell-sample-commands.jar"]
[3.11524, "o", "\r\n"]
[4.904714, "o", "\u001b[?1h\u001b=\u001b[?2004h\u001b[33mmy-shell:>\u001b[0m"]
[5.372714, "o", "\u001b[31mc\u001b[0m"]
[5.484943, "o", "\u001b[31mo\u001b[0m"]
[5.70511, "o", "\u001b[31mm\u001b[0m"]
[5.913092, "o", "\u001b[31mp\u001b[0m"]
[5.992808, "o", "\u001b[31mo\u001b[0m"]
[6.159092, "o", "\u001b[31mn\u001b[0m"]
[6.281094, "o", "\u001b[31me\u001b[0m"]
[6.353925, "o", "\u001b[31mn\u001b[0m"]
[6.49373, "o", "\u001b[31mt\u001b[0m"]
[6.677988, "o", "\u001b[31m \u001b[0m"]
[6.825018, "o", "\u001b[31ms\u001b[0m"]
[6.95602, "o", "\u001b[31mi\u001b[0m"]
[7.121719, "o", "\u001b[31mn\u001b[0m"]
[7.309007, "o", "\u001b[13D\u001b[1mcomponent single\u001b[0m \u001b[K"]
[7.972392, "o", "\r\r\n\u001b[?1l\u001b>\u001b[?1000l\u001b[?2004l"]
[8.019885, "o", "\u001b[?1h\u001b=\u001b[?25l"]
[8.080156, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mtestSimple\u001b[0m [Use arrows to move], type to filter\u001b[54C \u001b[96;1m>\u001b[0m\u001b[96;1m key1\u001b[0m\u001b[97C   key2\u001b[2A\r"]
[9.181662, "o", "\r\r\n  key1\r\r\n\u001b[96;1m> key2\u001b[0m\u001b[2A\r"]
[9.701431, "o", "\r\r\n\u001b[96;1m> key1\u001b[0m\r\r\n  key2\u001b[2A\r"]
[10.380557, "o", "\r\r\n  key1\r\r\n\u001b[96;1m> key2\u001b[0m\u001b[2A\r"]
[12.157001, "o", "\u001b[35Cfiltering 'k'\u001b[K\r"]
[14.605973, "o", "\u001b[47C\u001b[1@e\r\r\n\u001b[96;1m> key1\u001b[0m\r\r\n  key2\u001b[2A\r"]
[14.92268, "o", "\u001b[48C\u001b[1@y\r"]
[15.320008, "o", "\u001b[49C\u001b[1@1\r\r\n\u001b[103C \u001b[K\r\r\r\n\u001b[K\u001b[2A"]
[16.215448, "o", "\u001b[49C\u001b[P\r\r\n\u001b[103C   key2\u001b[2A\r"]
[16.559377, "o", "\u001b[48C\u001b[P\r"]
[16.784486, "o", "\u001b[47C\u001b[P\r"]
[17.004921, "o", "\u001b[35Ctype to filter\u001b[K\r"]
[17.790542, "o", "\r\r\n  key1\r\r\n\u001b[96;1m> key2\u001b[0m\u001b[2A\r"]
[18.340142, "o", "\u001b[?1l\u001b>\u001b[?12;25h\u001b[K\r\r\n\u001b[K\r\r\n\u001b[K\u001b[2A"]
[18.347511, "o", "\u001b[32;1m?\u001b[0m \u001b[97;1mtestSimple\u001b[0m \u001b[34mvalue2\u001b[0m\r\n"]
[18.34852, "o", "Got value value2\r\n"]
[18.35067, "o", "\u001b[?1h\u001b=\u001b[?2004h\u001b[33mmy-shell:>\u001b[0m"]
[20.35067, "o", "\u001b[?1h\u001b=\u001b[?2004h\u001b[33mmy-shell:>\u001b[0m"]

context 对象是 SingleItemSelectorContext。下表描述了其上下文变量:

The context object is SingleItemSelectorContext. The following table describes its context variables:

Table 1. SingleItemSelectorContext Template Variables
Key Description

value

The returned value when the component exists.

rows

The visible items, where rows contains maps of name and selected items.

model

The parent context variables (see SelectorComponentContext Template Variables).

你可以通过定义一项来预先选中它以供显示。如果你知道默认设置,这样很有用且可以让用户只需按 Enter 即可做出选择。以下列表设置一个默认值:

You can pre-select an item by defining it to get exposed. This is useful if you know the default and lets the user merely press Enter to make a choice. The following listing sets a default:

Unresolved include directive in modules/ROOT/pages/components/ui/singleselect.adoc - include::../../../../../src/test/java/org/springframework/shell/docs/UiComponentSnippets.java[]