当前位置: 首页 > news >正文

Ant Design for UI 选择下拉框

1. 单选框  与多选框

<template><div class="demo-page" style="padding: 40px; max-width: 1200px; margin: 0 auto; font-family: 'Microsoft YaHei', Arial, sans-serif;"><h1 style="color: #1890ff; text-align: center; margin-bottom: 10px;">📊 下拉框功能演示</h1><p style="text-align: center; color: #666; margin-bottom: 40px;">模拟从后端加载数据 | Vue 2 + ant-design-vue</p><a-spin :spinning="loading" tip="加载数据中..." style="display: block; margin-bottom: 30px;"><div style="background: #f8f9fa; padding: 24px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);display: flex;gap: 24px;align-items: flex-start;"><!-- 单选:选择省份 --><div style="flex: 1; min-width: 280px;"><label style="display: block; margin-bottom: 8px; color: #333; font-weight: 500;">选择省份:</label><a-selectv-model="selectedProvince"placeholder="请选择一个省份"style="width: 100%;"@change="handleProvinceChange"><a-select-option v-for="p in provinces" :key="p.value" :value="p.value">{{ p.label }}</a-select-option></a-select></div><!-- 多选:选择城市 --><div style="flex: 1; min-width: 280px;"><label style="display: block; margin-bottom: 8px; color: #333; font-weight: 500;">选择城市(多选):</label><a-selectmode="multiple"v-model="selectedCities"placeholder="请选择你喜欢的城市"style="width: 100%;":disabled="loading"><a-select-option v-for="c in cities" :key="c.value" :value="c.value">{{ c.label }}</a-select-option></a-select></div></div></a-spin><!-- 显示结果 --><div style="margin-top: 30px; padding: 20px; background: #e6f7ff; border-radius: 8px; border-left: 4px solid #1890ff;"><h3>🎯 当前选择结果:</h3><p><strong>选中的省份:</strong> {{ provinceText || '未选择' }}</p><p><strong>选中的城市:</strong> {{ selectedCities.length ? selectedCities.map(v => cityMap[v]).join(', ') : '未选择' }}</p></div></div>
</template><script>
export default {name: 'Home',data() {return {loading: true,selectedProvince: undefined,selectedCities: [],provinces: [{ value: 'zhejiang', label: '浙江省' },{ value: 'jiangsu', label: '江苏省' },{ value: 'guangdong', label: '广东省' },{ value: 'sichuan', label: '四川省' },{ value: 'shandong', label: '山东省' }],cities: [{ value: 'hangzhou', label: '杭州' },{ value: 'ningbo', label: '宁波' },{ value: 'wenzhou', label: '温州' },{ value: 'shaoxing', label: '绍兴' },{ value: 'nanjing', label: '南京' },{ value: 'suzhou', label: '苏州' },{ value: 'wuxi', label: '无锡' },{ value: 'guangzhou', label: '广州' },{ value: 'shenzhen', label: '深圳' },{ value: 'chengdu', label: '成都' }]}},computed: {provinceText() {const p = this.provinces.find(item => item.value === this.selectedProvince)return p ? p.label : ''},cityMap() {const map = {}this.cities.forEach(c => { map[c.value] = c.label })return map}},methods: {handleProvinceChange(value) {console.log('选择了省份:', value)}},created() {setTimeout(() => {this.loading = false}, 1200)}
}
</script><style scoped>
/* ✅ 使用 :deep() 替代 /deep/,现代写法 */
:deep(.ant-select-arrow) {color: black !important;opacity: 1 !important;
}:deep(.ant-select-arrow::before) {content: '>' !important;color: black !important;font-weight: 600;font-size: 14px;transform: rotate(90deg);transition: transform 0.3s;
}:deep(.ant-select-selection:hover .ant-select-arrow::before) {color: #1890ff !important; /* 悬停变蓝色,专业 */
}:deep(.ant-select-open .ant-select-arrow::before) {transform: rotate(-90deg) !important;
}/* 美化多选标签 */
:deep(.ant-select-selection__choice) {background-color: #1890ff !important;border-color: #1890ff !important;height: 24px;line-height: 22px;
}:deep(.ant-select-selection__choice__content) {color: white !important;
}<style scoped>
/* 设置下拉框输入框背景为白灰色 */
:deep(.ant-select-selection),
:deep(.ant-select-selection--multiple) {background-color: #f0f0f0 !important; /* 白灰色背景,可以根据需要调整色值 */
}/* 可选:为悬停和聚焦状态设置不同的背景颜色 */
:deep(.ant-select-selection:hover),
:deep(.ant-select-selection:focus) {background-color: #e0e0e0 !important; /* 悬停或聚焦时稍微深一点的白灰色 */
}
</style>

2. 下拉框纯静态展示

<template><div class="demo-page" style="padding: 40px; max-width: 1200px; margin: 0 auto; font-family: 'Microsoft YaHei', Arial, sans-serif;"><h1 style="color: #1890ff; text-align: center; margin-bottom: 10px;">📋 下拉框值展示(点击展开)</h1><p style="text-align: center; color: #666; margin-bottom: 40px;">点击展开展示下拉框中的所有可选值 | 静态展示</p><!-- 🔹 展示所有省份 --><div style="display: flex; justify-content: space-between; margin-bottom: 20px;"><div style="display: flex; align-items: center; gap: 16px; width: 45%;"><span style="font-weight: 500; color: #333;">所有可选省份:</span><a-popover placement="bottomLeft" trigger="click"><template slot="content"><div style="max-height: 200px; overflow-y: auto; padding: 8px; width: 200px;"><div v-for="p in provinces" :key="p.value">{{ p.label }}</div></div></template><a-button type="link">点击查看</a-button></a-popover></div><!-- 🔹 展示所有城市 --><div style="display: flex; align-items: center; gap: 16px; width: 200px;"><span style="font-weight: 500; color: #333;">所有可选城市:</span><a-popover placement="bottomLeft" trigger="click"><template slot="content"><!-- ✅ 在这里设置弹出面板的宽度 --><div style="max-height: 150px; overflow-y: auto; padding: 8px; width: 100px;"><div v-for="c in cities" :key="c.value">{{ c.label }}</div></div></template><a-button type="link">点击查看</a-button></a-popover></div></div></div>
</template><script>
export default {name: 'Home',data() {return {// 模拟数据provinces: [{ value: 'zhejiang', label: '浙江省' },{ value: 'jiangsu', label: '江苏省' },{ value: 'guangdong', label: '广东省' },{ value: 'sichuan', label: '四川省' },{ value: 'shandong', label: '山东省' }],cities: [{ value: 'hangzhou', label: '杭州' },{ value: 'ningbo', label: '宁波' },{ value: 'wenzhou', label: '温州' },{ value: 'shaoxing', label: '绍兴' },{ value: 'nanjing', label: '南京' },{ value: 'suzhou', label: '苏州' },{ value: 'wuxi', label: '无锡' },{ value: 'guangzhou', label: '广州' },{ value: 'shenzhen', label: '深圳' },{ value: 'chengdu', label: '成都' }]}}
}
</script><style scoped>
/* 调整样式使其更加紧凑 */
.demo-page {max-width: 100%;
}.ant-btn-link {padding: 0;color: #1890ff;text-decoration: underline;
}.ant-btn-link:hover {color: #40a9ff;
}.ant-popover-inner-content {padding: 8px;background-color: #fff;border-radius: 4px;box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}.ant-popover-inner-content div {padding: 4px 0;
}/* 确保每组元素在同一行显示 */
.flex-container {display: flex;justify-content: space-between;
}.flex-item {display: flex;align-items: center;gap: 16px;
}.flex-item span {font-weight: 500;color: #333;
}
</style>

http://www.xdnf.cn/news/1366723.html

相关文章:

  • BIO、NIO 和 AIO
  • 2025.8.25回溯算法-集合
  • Typora + PicList + Gitee 图床完整配置教程
  • 【ElasticSearch】json查询语法和可用的客户端
  • ESP32开发WSL_VSCODE环境搭建
  • Mysql系列--8、索引
  • Java延迟任务实现方案详解:从DelayQueue到实际应用
  • 2.3零基础玩转uni-app轮播图:从入门到精通 (咸虾米总结)
  • 【Docker基础】Docker-compose进阶配置:健康检查与服务就绪
  • K8s Pod驱逐机制详解与实战
  • C++ extern 关键字面试深度解析
  • 开源 C++ QT Widget 开发(六)通讯--TCP调试
  • 安全合规:AC(上网行为安全)--下
  • vue 一键打包上传
  • Genymotion 虚拟机如何安装 APK?(ARM 插件安装教程)
  • ICCV 2025|TRACE:无需标注,用3D高斯直接学习物理参数,从视频“预知”未来!
  • 二、添加3D形状
  • More Effective C++ 条款07:不要重载、和,操作符
  • 【系统架构设计师】数据库设计(一):数据库技术的发展、数据模型、数据库管理系统、数据库三级模式
  • 审核问题——首次进入APP展示隐私政策弹窗
  • 大模型(一)什么是 MCP?如何使用 Charry Studio 集成 MCP?
  • 深分页实战
  • 计算机网络:HTTP、抓包、TCP和UDP报文及重要概念
  • GPT5的Test-time compute(测试时计算)是什么?
  • Legion Y7000P IRX9 DriveList
  • HTTP 与 HTTPS 深度解析:从原理到实际应用
  • 链表OJ习题(1)
  • 1. 并发产生背景 并发解决原理
  • pytest 并发执行用例(基于受限的测试资源)
  • 现代C++工具链实战:CMake + Conan + vcpkg依赖管理