Ant design Vue3 中的 a-select 组件使用方法详解

a-select 组件使用方法

a-select 搜索时过滤对应的 option 属性的value
实际开发中大部分使用label进行搜索 :optionFilterProp = “label” 即可使用label搜索
组件中返回值为value和label的List可直接使用搜索
如果返回值为其他属性 在使用之前的搜索不行了

为什么 placeholder 不显示 ?
placeholder 只有在 value = undefined 才会显示,对于其它的 null、0、‘’ 等等对于 JS 语言都是有意义的值。
实际使用 绑定的值为null 也会显示

示例1 : data值为 label-value

<a-select v-model:value="selectValue" :options="options1" placeholder="请选择" style="width: 200px;" showSearch  
optionFilterProp="label"></a-select>

let selectValue = ref<string | null>(null)
const options1 = ref<any>([
  {
    value: '1',
    label: 'Jack',
  },
  {
    value: '2',
    label: 'Lucy',
  },
  {
    value: '3',
    label: 'Disabled',
  },
  {
    value: '4',
    label: 'Yiminghe',
  },
]);

示例 2 : data返回值为 codeName-codeValue

当list返回值为 codeName-codeValue 的list时处理方法有2种

  1. 把codeName-codeValue 转成 label-value数据格式 可直接搜索
  2. 使用 :field-names=“{ label: ‘codeName’, value: 'codeValue ’ }” 自定义节点 不可直接搜索
    方法2 使用:filterOption=“filterOption” 去进行搜索
    示例中使用的是第二种方法
 <a-select v-model:value="selectValue" :options="options1" placeholder="请选择" style="width: 200px;" showSearch
optionFilterProp="label" :field-names="{ label: 'codeName', value: 'codeValue' }" :filterOption="filterOption"></a-select>

let selectValue = ref<string | null>(null)
const options1 = ref<any>([
  {
    codeValue: '1',
    codeName: 'Jack',
  },
  {
    codeValue: '2',
    codeName: 'Lucy',
  },
  {
    value: '4',
    codeName: 'Yiminghe',
  },
]);

// 自定义搜索
let filterOption = (value: any, option: any) => {
  console.log(option)
  return option.codeName.indexOf(value) >= 0
}
  1. 获取选中选项的其他key值

正常选中的时候 只会获取到value值 如果要获取该数据的其他数值, 例如 id name 等等
使用 change方法 该方法有俩个参数 value option(选中当前的对象的所有数据)

把示例中的 option1 改为 
const options1 = ref<any>([
  {
    codeValue: '1',
    codeName: 'Jack',
    id: 13,
    isu:'2'
  },
  {
    codeValue: '2',
    codeName: 'Lucy',
    id: 12,
    isu:'3'
  },
  {
    value: '4',
    codeName: 'Yiminghe',
    id: 11,
    isu:'1'
  },
]);

let selectChange = (value: string, option: any) => {
  console.log(option)
}
打印的 option 为下列数据
//{
//    "codeValue": "2",
//    "codeName": "Lucy",
//    "id": 12,
//    "isu": "3"
//}

如果是多选的话 option 为数组 ,我们将 mode 属性设置为 “multiple”,从而启用多选模式

好了XDM,bye~~~

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值