Skip to content

Select 选择器

当选项过多时,使用下拉菜单展示并选择内容。

基础用法

适用广泛的基础单选

type为'select'

[
  {
    type: "select",
    name: "select",
    text: "选项",
    placeholder: "请选择",
    options: [
      {
        text: "选项1",
        value: 1
      },
      {
        text: "选项2",
        value: 2
      }
    ]
  }
]
显示配置

有禁用选项

在 options 选项配置中,设定 disabled 值为 true,即可禁用该选项

[
  {
    type: "select",
    name: "select",
    text: "选项",
    placeholder: "请选择",
    options: [
      {
        text: "选项1",
        value: 1
      },
      {
        text: "选项2",
        value: 2,
        disabled: true
      }
    ]
  }
]
显示配置

禁用状态

选择器不可用状态

为 el-select 设置 disabled 属性,则整个选择器不可用

[
  {
    type: "select",
    name: "select",
    text: "选项",
    placeholder: "请选择",
    disabled: true,
    options: [
      {
        text: "选项1",
        value: 1
      },
      {
        text: "选项2",
        value: 2
      }
    ]
  }
]
显示配置

基础多选

适用性较广的基础多选,用 Tag 展示已选项

[
  {
    type: "select",
    name: "select",
    text: "选项",
    placeholder: "请选择",
    multiple: true,
    options: [
      {
        text: "选项1",
        value: 1
      },
      {
        text: "选项2",
        value: 2
      },
      {
        text: "选项3",
        value: 3
      }
    ]
  }
]
显示配置

分组

备选项进行分组展示

配置group为true

[
  {
    type: "select",
    name: "select",
    text: "选项",
    placeholder: "请选择",
    group: true,
    options: [
      {
        label: "group1",
        options: [
          {
            text: "选项1",
            value: 1
          },
          {
            text: "选项2",
            value: 2
          },
          {
            text: "选项3",
            value: 3
          }
        ],
        disabled: true
      },
      {
        label: "group2",
        options: [
          {
            text: "选项4",
            value: 4
          },
          {
            text: "选项5",
            value: 5
          },
          {
            text: "选项6",
            value: 6
          }
        ]
      }
    ]
  }
]
显示配置

创建条目

可以创建并选中选项中不存在的条目

[
  {
    type: "select",
    name: "select",
    text: "选项",
    placeholder: "请选择",
    allowCreate: true,
    options: [
      {
        text: "选项1",
        value: 1
      },
      {
        text: "选项2",
        value: 2
      }
    ]
  }
]
显示配置

远程选项

通过接口请求获取选项列表

配置remote为true,然后配置option,而不是options

[
  {
    type: "select",
    name: "select",
    text: "选项",
    placeholder: "请选择",
    remote: true,
    option: {
      url: "select/remote",
      root: "data",
      method: "post",
      mode: "cors",
      headers: {
        "Content-Type": "application/json"
      },
      body: {
        query: ""
      },
      json: true,
      text: (option) => `${option.name}`,
      value: (option) => `${option.id}`
    }
  }
]
显示配置

同时在 src/main.ts 中需要自定义实现请求

typescript
app.use(MagicForm, {
  request: async (options: any) => {
    // 自定义请求实现
  },
});

TIP

如果 Select 的绑定值为对象类型,请务必指定 valueKey 作为它的唯一性标识。

Select Attributes

参数说明类型可选值默认值
name绑定值string
placeholder输入框占位文本string
text表单标签string
disabled是否禁用boolean / FilterFunctionfalse
multiple是否多选booleanfalse
valueKey作为 value 唯一标识的键名,绑定值为对象类型时必填stringvalue
allowCreate是否允许用户创建新条目booleanfalse
remote是否为远程搜索booleanfalse
group是否选择分组booleanfalse
onChange值变化时触发的函数OnChangeHandler-
options选项Array-
option选项Object-
查看 FilterFunction / OnChangeHandler 及关联类型定义
ts
export type FilterFunction<T = boolean> = (
  mForm: FormState | undefined,
  data: {
    model: FormValue;
    values: FormValue;
    parent?: FormValue;
    formValue: FormValue;
    prop: string;
    config: any;
    index?: number;
    getFormValue: (prop: string) => any;
  },
) => T;
ts
export type OnChangeHandler = (mForm: FormState | undefined, value: any, data: OnChangeHandlerData) => any;
ts
export interface OnChangeHandlerData {
  model: FormValue;
  values?: Readonly<FormValue> | null;
  parent?: FormValue;
  formValue?: FormValue;
  config: Readonly<any>;
  prop: string;
  changeRecords: ChangeRecord[];
  setModel: (prop: string, value: any) => void;
  setFormValue: (prop: string, value: any) => void;
}
ts
export interface ChangeRecord {
  propPath?: string;
  value: any;
}
ts
export type FormValue = Record<string | number, any>;

配置类型

查看 SelectConfig 配置类型定义
ts
export interface SelectConfig extends FormItem, Input {
  type: 'select';
  clearable?: boolean;
  multiple?: boolean;
  valueKey?: string;
  allowCreate?: boolean;
  filterable?: boolean;
  group?: boolean;
  options?: SelectConfigOption[] | SelectConfigGroupOption[] | SelectOptionFunction;
  remote?: true;
  option?: {
    url: string | ((mForm: FormState | undefined, data: { model: any; formValue: any }) => string);
    initUrl?: string | ((mForm: FormState | undefined, data: { model: any; formValue: any }) => string);
    method?: 'jsonp' | string;
    cache?: boolean;
    timeout?: number;
    mode?: string;
    headers?: {
      [key: string]: string;
    };
    json?: false | boolean;
    body?: Record<string, any> | RemoteSelectOptionBodyFunction;
    initBody?: Record<string, any> | RemoteSelectOptionBodyFunction;
    jsonpCallback?: 'callback' | string;
    afterRequest?: RemoteSelectOptionAfterRequestFunction;
    afterInitRequest?: RemoteSelectOptionAfterRequestFunction;
    beforeRequest?: RemoteSelectOptionBeforeRequestFunction;
    beforeInitRequest?: RemoteSelectOptionBeforeRequestFunction;
    root?: string;
    totalKey?: string;
    initRoot?: string;
    item?: RemoteSelectOptionItemFunction;
    value?: string | SelectOptionValueFunction;
    text?: string | SelectOptionTextFunction;
  };
}
ts
export interface FormItem {
  /** vnode的key值,默认是遍历数组时的index */
  __key?: string | number;
  /** 表单域标签的的宽度,例如 '50px'。支持 auto。 */
  labelWidth?: string | number;
  /** label 标签的title属性 */
  labelTitle?: string;
  className?: string;
  /** 字段名 */
  name?: string | number;
  /** 额外的提示信息,和 help 类似,当提示文案同时出现时,可以使用这个。 */
  extra?: string | FilterFunction<string>;
  /** 配置提示信息 */
  tooltip?: ToolTipConfigType | FilterFunction<ToolTipConfigType>;
  /** 是否置灰 */
  disabled?: boolean | FilterFunction;
  /** 使用表单中的值作为key,例如配置了text,则使用model.text作为key */
  key?: string;
  /** 是否显示 */
  display?: boolean | 'expand' | FilterFunction<boolean | 'expand'>;
  /** 值发生改变时调用的方法 */
  onChange?: OnChangeHandler;
  /** label 标签的文本 */
  text?: string | FilterFunction<string>;
  /** 右侧感叹号 */
  tip?: string;

  filter?: 'number' | OnChangeHandler;
  /** 是否去除首尾空格 */
  trim?: boolean;
  /** 默认值 */
  defaultValue?: any | DefaultValueFunction;
  /** 表单验证规则 */
  rules?: Rule[];
  extensible?: boolean;
  dynamicKey?: string;
  /** 是否需要显示`展开更多配置` */
  expand?: boolean;
  style?: Record<string, any>;
  fieldStyle?: Record<string, any>;
  labelPosition?: 'top' | 'left' | 'right';
}
ts
export interface Input {
  /** 输入框没有内容时显示的文案 */
  placeholder?: string;
}

options item

| 参数 | 说明 | 类型 | 可选值 | 默认值 | | -------- | -------- | ---------- | -------------------- | ------ | --- | | text | | 选项的标签 | string/number/object | — | — | | value | 选项的值 | string | — | — | | disabled | 是否禁用 | boolean | — | false | | label | string | — | — | — | | options | Array | — | — | — |

option

参数说明类型可选值默认值
urlstring
rootstring
textstring / Function
valuestring / Function

Powered by 腾讯视频会员平台技术中心