Ant Design Vue Select 选择器 全选 功能

devtools/2025/3/30 4:03:19/

Vue.js的组件库Ant Design Vue Select 选择器没有全选功能,如下图所示:

在项目中,我们自己实现了全选清空功能,如下所示:

代码如下所示:

<!--
=========================================================
* 参数配置 - 风力发电 - 曲线图
* 猴王软件学院 - 大强
* 2025-3-23
=========================================================
-->
<template><div><div class="flex-container"><div class="flex-item"><a-selectid="a-select-scene":options="options_scene"mode="combobox"style="width: 200px; height:50px;"@change="proChange_scene"showArrow="true"placeholder="请选择场景"v-model="selected_scene"></a-select></div><div class="flex-item"><a-selectid="a-select-node":options="options_node"mode="multiple"style="width: 200px; height:50px;"@change="proChange_node"showArrow="true"v-model:value="selected_node"><template #dropdownRender="{ menuNode: menu }"><div><a-button @click="allSelectedFun" type="primary" style="margin-right: 10px;">全选</a-button><a-button @click="clearFun">清空</a-button><a-divider style="margin: 5px 0;" /><v-nodes :vnodes="menu" /></div></template></a-select><div class="my-ant-select-selection-placeholder">请选择节点</div></div><div class="flex-item"><a-selectid="a-select-power":options="options_power"mode="combobox"style="width: 200px; height:50px;"@change="proChange_power"showArrow="true"placeholder="请选择功率"v-model="selected_power"></a-select></div><div class="flex-item"><a-button type="primary" preIcon="ant-design:search-outlined" @click="searchChart">查看</a-button></div></div><div ref="chartRef" :style="{ height, width }"></div></div>
</template>
<script lang="ts">
import {defineComponent, PropType, ref, Ref, reactive, watchEffect, unref, onMounted} from 'vue';
import {useECharts} from '/@/hooks/web/useECharts';
import {cloneDeep} from 'lodash-es';
import {Select} from "ant-design-vue";
import {initDictOptions} from "@/utils/dict";export default defineComponent({name: 'LineMulti',components: {Select,VNodes: (_, { attrs }) => {return attrs.vnodes;},},props: {chartData: {type: Array,default: () => [],required: true,},option: {type: Object,default: () => ({}),},type: {type: String as PropType<string>,default: 'line',},width: {type: String as PropType<string>,default: '100%',},height: {type: String as PropType<string>,default: 'calc(100vh - 78px)',},},emits: ['click'],setup(props, {emit}) {const chartRef = ref<HTMLDivElement | null>(null);const {setOptions, getInstance} = useECharts(chartRef as Ref<HTMLDivElement>);const option = reactive({tooltip: {trigger: 'axis',axisPointer: {type: 'shadow',label: {show: true,backgroundColor: '#333',},},},legend: {top: 30,},grid: {top: 60,},xAxis: {name: '时间(小时)',type: 'category',data: [],},yAxis: {name: '功率(万千瓦)',type: 'value',},series: [],});// 功率let options_power = ref<any>([]);let selected_power = ref('');// 场景let options_scene = ref<any>([]);let selected_scene = ref('');// 节点let options_node = ref<any>([]);// let selected_node = ref('');let selected_node = ref([]);/*** 功率改变函数* @param val*/function proChange_power(val) {selected_power.value = val;}/*** 场景改变函数* @param val*/function proChange_scene(val) {selected_scene.value = val;}/*** 节点改变函数* @param val*/function proChange_node(val) {selected_node.value = val;}// 全选函数function allSelectedFun() {options_node.value.forEach((item) => {let index = selected_node.value.indexOf(item.value);if (index == -1) {selected_node.value.push(item.value);}});};// 清空函数function clearFun() {selected_node.value.splice(0, selected_node.value.length);};/*** 查看图表*/function searchChart() {if (selected_scene.value == '') {alert("请选择场景!")return}if (selected_node.value == '') {alert("请选择节点!")return}if (selected_power.value == '') {alert("请选择功率!")return}if (props.option) {Object.assign(option, cloneDeep(props.option));}//节点let nodeArr = Array.from(new Set(props.chartData.map((item) => item.nodeName)));//轴数据let xAxisData = Array.from(new Set(props.chartData.map((item) => item.index)));let seriesData = [];nodeArr.forEach((node) => {selected_node.value.forEach((value) => {if (node === value) {let obj: any = {name: node, type: props.type};// update-begin-author:liusq date:2023-7-12 for: [issues/613] LineMulti 在数据不对齐时,横坐标计算错误let data = [];xAxisData.forEach((x) => {let dataArr = props.chartData.filter((item) =>x == item.index &&  // 索引selected_power.value == item.power &&  // 功率selected_scene.value == item.scene &&  // 场景node == item.nodeName // 节点);if (dataArr && dataArr.length > 0) {data.push(dataArr[0].value);} else {data.push(null);}});// update-end-author:liusq date:2023-7-12 for: [issues/613] LineMulti 在数据不对齐时,横坐标计算错误//data数据obj['data'] = data;seriesData.push(obj);}});});option.series = seriesData;option.xAxis.data = xAxisData;// option.legend.show = false;option.legend.selector = [{// 全选type: 'all',// 可以是任意你喜欢的标题title: '全选'},{// 反选type: 'inverse',// 可以是任意你喜欢的标题title: '反选'}];option.legend.selectedMode = false;option.legend.orient = 'vertical';option.legend.right = '0px';// option.legend.top = '-50px';console.log('option', option);setOptions(option);getInstance()?.off('click', onClick);getInstance()?.on('click', onClick);}function onClick(params) {emit('click', params);}/*** 初始化字典选项*/async function initDictConfig() {options_power.value = await initDictOptions('power');options_scene.value = await initDictOptions('scene');options_node.value = await initDictOptions('node');}onMounted(() => {//初始化字典选项initDictConfig();});return {options_power, options_scene, options_node,selected_power, selected_scene, selected_node,proChange_power, proChange_scene, proChange_node, chartRef, searchChart,allSelectedFun, clearFun};},
});
</script><style>
.flex-container {display: -webkit-flex;display: flex;
}.flex-item {width: 210px;
}.ant-select-multiple .ant-select-selection-overflow-item {flex: none;align-self: center;max-width: 100%;display: none;
}.my-ant-select-selection-placeholder {overflow: hidden;white-space: nowrap;text-overflow: ellipsis;flex: 1;color: rgba(0, 0, 0, 0.65);pointer-events: none;margin-top: -45px;margin-left: 20px;position: relative;
}
</style>


http://www.ppmy.cn/devtools/171490.html

相关文章

前端抽象化,打破框架枷锁:统一路由的设计

个人博客原文地址 此文章并不适合初级前端来看&#xff0c;它是抽象的架构设计&#xff0c;需要一定的TS基础和抽象思维&#xff0c;若带着思考的读完本文章相信会让你感到充实 当然你也可以复制&#xff0c;然后在自己项目中去实现它&#xff0c;然后用起来 只要你是在写前端…

MyBatis打印SQL日志的配置

配置MyBatis打印日志的步骤如下&#xff0c;支持多种日志框架&#xff08;如Logback、Log4j2等&#xff09;&#xff1a; 一、选择日志框架并添加依赖&#xff08;以常见组合为例&#xff09; 1. Logback&#xff08;推荐&#xff09; <!-- Maven 依赖 --> <depende…

线性代数核心概念与NumPy科学计算实战全解析

前言 学习方法&#xff1a; 思维导图&#xff0c;梳理 多记忆&#xff0c;函数名和功能&#xff0c;参数 学会应用&#xff0c;不要钻牛角尖 一、浅解线性代数 1.1标量 标量是一个只有大小没有方向的量。在数学上&#xff0c;标量通常表示为一个普通的数字&#xff0c;如‌质量…

网络中常用协议

一, TCP协议 TCP&#xff08;Transmission Control Protocol&#xff0c;传输控制协议&#xff09;是互联网核心协议之一&#xff0c;位于传输层&#xff0c;为应用层提供可靠的、面向连接的数据传输服务。 1. TCP的核心特点 特性说明面向连接通信前需通过三次握手建立连接&a…

[AI建模] 使用Pinokio本地化部署混元2D到3D AI建模服务

近年来,AI驱动的2D转3D建模技术发展迅猛,而Pinokio作为一个强大的AI模型管理与部署平台,使得在本地部署这些复杂的AI模型变得更加简单高效。本文将介绍如何使用Pinokio在本地部署混元2D到3D AI建模服务,并快速生成带或不带Texture的3D模型。 1. 在Pinokio Discover页面找到…

Blender模型旋转动画制作

在Blender中让模型旋转并制作成动画的步骤如下&#xff1a; 选择模型 打开Blender&#xff0c;确保模型已在场景中。 右键点击模型以选中它。 插入初始旋转关键帧 将时间轴光标移动到第1帧。 按 R 键旋转模型&#xff0c;或直接在属性面板调整旋转值。 按 I 键&#xff0c…

深入 SVG:矢量图形、滤镜与动态交互开发指南

1.SVG 详细介绍 SVG&#xff08;Scalable Vector Graphics&#xff09; 是一种基于 XML 的矢量图形格式&#xff0c;用于描述二维图形。 1. 命名空间 (Namespace) ★ 了解 命名空间 URI&#xff1a;http://www.w3.org/2000/svg 用途&#xff1a;在 XML 或 XHTML 中区分不同标…

conda 常用命令

常用 Conda 命令整理环境管理 conda create --name 环境名 &#xff1a;创建新环境 conda activate 环境名 &#xff1a;激活环境 conda deactivate&#xff1a;退出环境 conda env list&#xff1a;列出所有环境 conda remove --name 环境名 --all &#xff1a;删除环…