xtf-select
组件说明
xtf-select 是统一下拉选择组件,适用于表单、筛选和远程数据选择。支持单选、多选、搜索、树结构、分组、虚拟滚动、Datacom 与远程分页。
基础用法
1. 单选
vue
<template>
<xtf-select v-model="value" label="类目" :options="options" clearable @change="changed" />
</template>
<script>
export default {
data() {
return {
value: '',
options: [
{ label: '数码', value: 'digital' },
{ label: '家居', value: 'home' }
]
}
},
methods: {
changed(v) {
console.log(v)
}
}
}
</script>2. 多选与搜索
vue
<template>
<xtf-select v-model="values" :options="options" multiple searchable :max="2" @confirm="confirm" />
</template>
<script>
export default {
data() {
return {
values: [],
options: [
{ label: '商城', value: 'mall' },
{ label: '门店', value: 'store' }
]
}
},
methods: {
confirm(p) {
console.log(p.options)
}
}
}
</script>3. 自定义选项插槽
vue
<template>
<xtf-select v-model="value" :options="options">
<template #option="{ option, selected }">
<view>
<text>{{ option.label }}</text>
<text>{{ selected ? ' 已选' : option.desc }}</text></view>
</template>
</xtf-select>
</template>
<script>
export default {
data() {
return { value: '', options: [{ label: '标准服务', value: 'normal', desc: '48 小时响应' }] }
}
}
</script>4. 树选择
vue
<template>
<xtf-select
v-model="values"
:options="options"
tree
multiple
show-path
default-expand-all
:tree-check-strictly="false"
/>
</template>
<script>
export default {
data() {
return {
values: [],
options: [{ label: '技术部', value: 'tech', children: [{ label: '前端组', value: 'web' }] }]
}
}
}
</script>全部属性
| 属性 | 类型 | 默认值 | 作用描述 | 适用范围 |
|---|---|---|---|---|
value / modelValue | String | Number | Boolean | Array | Object | ''/undefined | 当前选择值 | 双向绑定 |
label / placeholder / title | String | '' | 字段文案 | 展示 |
options / localdata | Array | []/undefined | 普通或本地数据 | 数据源 |
collection / field / where / orderby / action / pageSize / getcount / loadtime | String | Number | Boolean | Object | - | Datacom 查询配置 | uniCloud Datacom |
rememberSelection / storageKey | Boolean/String | true/'xtf-select-last-selected-value' | 缓存集合选择 | Datacom |
valueKey / labelKey / descKey | String | 'value'/'label'/'desc' | 字段映射 | 自定义数据 |
size / disabled / readonly / clearable / clear / required | String | Boolean | ''/false/false/false/undefined/false | 字段状态 | 表单 |
multiple / searchable / max / showToolbar / separator | Boolean | String | Number | false/false/0/true/' / ' | 多选和搜索 | 多选 |
selectedOption / returnObject | Object | Array/Boolean | null/false | 预缓存选项、返回对象 | 远程回显 |
remote / remoteMethod / remoteImmediate / searchLoading / searchDebounce | Boolean/Function/Boolean/Boolean/String | Number | false/null/false/false/0 | (payload)=>Promise<Array|{options,total}> 远程搜索 | 远程 |
filterMethod | Function | null | (option,keyword)=>Boolean | 本地搜索 |
searchPlaceholder / confirmText / cancelText / emptyText / emptyTips / message | String | '' | 文案 | 展示 |
format | String | '' | {field} 模板格式 | 数据回显 |
defItem / maxVisibleTags | String | Number | 0/2 | 默认第几项、最多标签数 | 展示 |
infiniteScroll / remotePage / remotePageSize / remoteTotal / loadingMore / finished | Boolean | String | Number | false/1/20/0/false/false | 外部远程分页状态 | 分页 |
groupKey | String | 'children' | 分组子项字段 | 分组 |
mode / placement / autoPlacement | String/Boolean | 'popup'/'bottom-start'/false | popup/center/dropdown 与下拉位置 | 面板 |
maxDropdownHeight / maxVisibleOptions / optionHeight | String | Number | 560/6/88 | 面板尺寸 | 布局 |
showHandle / showCancel / showIndicatorIcon / arrowIcon / clearIcon | Boolean | String | true/true/true/'expand_more'/'ri-close-line' | 面板和图标 | 展示 |
triggerCustomClass / panelCustomClass / dropdownCustomClass / customClass | String | '' | 类名 | 样式 |
triggerCustomStyle / panelCustomStyle / dropdownCustomStyle / customStyle | String | Object | '' | 样式 | 样式 |
virtualScroll / virtualItemHeight / virtualThreshold | Boolean/String | Number/String | Number | false/88/100 | 虚拟列表 | 大数据 |
tree / childrenKey / defaultExpandAll / showPath / treeCheckStrictly | Boolean | String | false/'children'/false/false/true | 树选择 | 树数据 |
loadChildren | Function | null | (option)=>Promise<Array> | 懒加载树 |
disabledValues / disableBranchSelect / pathSeparator | Array/Boolean/String | []/false/' / ' | 禁用值、禁选分支、路径分隔 | 树数据 |
options 每项支持的字段
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
value,label,desc,disabled,disable | Any/String/String/Boolean/Boolean | - | 值、标题、描述、禁用 |
children,lazy | Array/Boolean | []/false | 分组或树子项、懒加载标识 |
事件
| 事件名称 | 回调参数 | 说明 |
|---|---|---|
input / update:modelValue / update:value / change | (value) | 提交选择值时依次触发。 |
confirm | ({ value, option?, options }) | 单选含 option,多选含 options。 |
clear / open / close | 无 | 清空、打开面板、关闭面板时触发。 |
reach-max | (draft: Array) | 多选草稿达到 max 时触发。 |
search | (keyword: String) | 搜索防抖结束后触发。 |
load-more | 无 | 开启 infiniteScroll 且滚动到底时触发。 |
remote-page-change | ({ page, pageSize, keyword, total, hasMore, reason, internal, options }) | 内置远程请求完成或外部远程分页请求时触发。 |
error | (error) | Datacom、远程请求、懒加载或缓存操作失败时触发。 |
事件使用示例
vue
<template>
<xtf-select
v-model="value"
:options="options"
searchable
@search="search"
@change="changed"
@clear="cleared"
/>
</template>
<script>
export default {
data() {
return { value: '', options: [{ label: 'A', value: 'a' }] }
},
methods: {
search(k) {
console.log('search', k)
},
changed(v) {
console.log('change', v)
},
cleared() {
console.log('clear')
}
}
}
</script>方法
1. query() - 手动触发 Datacom 查询
仅在 collection 数据源下可用,返回 Promise<Array>。
js
this.$refs.select.query().then((rows) => console.log(rows))插槽
| 插槽名 | 作用域参数 | 说明 |
|---|---|---|
tag | option、index、label、close | 替换多选已选标签;调用 close() 移除该选项。 |
toolbar | keyword、selected-options、draft-value | 位于多选且非树形面板的工具栏,内置全选和反选之后。 |
option | option、index、selected、keyword、label、desc | 替换每个选项的主体内容。 |
footer | keyword、selected-options、draft-value | 面板底部区域;仅提供插槽时渲染。 |
option 插槽示例
vue
<template>
<xtf-select v-model="value" :options="options" @change="changed">
<template #option="{ option, selected, desc }">
<view class="option-row">
<text>{{ option.label }}</text>
<text>{{ selected ? '已选' : desc }}</text>
</view>
</template>
</xtf-select>
</template>
<script>
export default {
data() {
return { value: '', options: [{ label: '标准服务', value: 'normal', desc: '48 小时响应' }] }
},
methods: {
changed(value) {
console.log('change', value)
}
}
}
</script>主题说明
- 触发器使用
--xtf-input-bg、--xtf-input-border和--xtf-color-primary。 - 面板使用
--xtf-color-surface,选中项使用--xtf-color-primary-soft。 - 在
xtf-config-provider或主题配置中覆盖变量即可全局定制。