首页 > 其他分享 >封装a-select下拉增加复选框,支持全选和取消全选

封装a-select下拉增加复选框,支持全选和取消全选

时间:2023-10-30 17:24:22浏览次数:30  
标签:vue const value 复选框 全选 import select

由于select下拉框中内容过多,使用下拉一个一个选取太过于麻烦,所以在下拉中增加全选和取消全选操作,增加复选框选择。

版本   vue3     Ant Design [email protected]

1.我们在看Ant Design Vue官网中,可以发现这个dropdownRender,它可以定义下拉框中的内容。

 2.封装vue组件

<template>
  <a-select
    v-model:value="inputValue"
    mode="tags"
    v-bind="$attrs"
    placeholder="请选择数据"
    :style="{ width: props.width ? props.width : '200px' }"
    option-label-prop="label"
  >
    <template #dropdownRender="{ menuNode: menu }">
      <div
        style="padding: 4px 8px; cursor: pointer;"
        @mousedown="(e) => e.preventDefault()"
        @click="addItem"
      >
        <a-button
          type="primary"
          style="margin-right: 10px;"
          @click="allSelectedFun"
          >全选</a-button
        >
        <a-button @click="clearFun">清空</a-button>
        <a-divider style="margin: 4px 0;" />
        <v-nodes :vnodes="menu" />
      </div>
    </template>
    <template v-for="item in props.options">
      <a-select-option :value="item.value" :label="item.label">
        <a-checkbox :checked="inputValue.includes(item.value)"></a-checkbox>
        {{ item.label }}
      </a-select-option>
    </template>
  </a-select>
</template>
<script lang="ts">
import { defineComponent, ref, watch } from 'vue';
export default defineComponent({
  components: {
    VNodes: (_, { attrs }) => {
      return attrs.vnodes;
    },
  },
});
</script>
<script lang="ts" setup>
import type { CascaderProps } from 'ant-design-vue';
import { ref } from 'vue';
import { PlusOutlined } from '@ant-design/icons-vue';
const props = defineProps<{
  options: any;//传入的列表,仅支持value label格式
  value: any;//外面v-model绑定的值   必须为v-model:value="绑定的值(根据需求自己定义)"
  width?: string;//设置a-select宽度
}>();
// 监听文本框输入
const inputValue = ref(props.value);
//将传入的v-model值赋值给当前a-select
watch(
  () => props.value,
  (newValue) => {
    inputValue.value = newValue;
  }
);
// 全选
const allSelectedFun = () => {
  props.options.forEach((item) => {
    let index = inputValue.value.indexOf(item.value);
    if (index == -1) {
      inputValue.value.push(item.value);
    }
  });
};
// 清空
const clearFun = () => {
    //使用splice是为了可以监听到变化
  inputValue.value.splice(0, inputValue.value.length);
};
</script>

<style lang="less" scoped></style>

3.main.ts中注册组件

import { createApp } from 'vue';
import Antd from 'ant-design-vue';
import App from './App.vue';
import router from './router';
import { createPinia } from 'pinia';
import piniaPluginPersist from 'pinia-plugin-persist';
// 引入选择组件
import AASelect from '@/components/aa-select/aa-select.vue';

// 全局组件挂载
const store = createPinia();
const app = createApp(App);
store.use(piniaPluginPersist);
 app.component('AASelect', AASelect);
app.use(router).use(Antd).use(store).mount('#app');

4.vue中使用

<AASelect 
          :width="'245px'" 
          :options="options" 
          v-model:value="selectData" 
          placeholder="请选择分析内容">
        </AASelect>

5.展示形式

 

标签:vue,const,value,复选框,全选,import,select
From: https://www.cnblogs.com/lisir-blogshare/p/17798347.html

相关文章

  • 解决MYSQL查询报错 Expression #4 of SELECT list is not in GROUP BY clause and con
    原因:在MySQL5.7.5后,默认开启了ONLY_FULL_GROUP_BY,所以导致了之前的一些SQL无法正常执行,其实,是我们的SQL不规范造成的,因为groupby之后,返回的一些数据是不确定的,所以才会出现这个错误。执行下面的命令后,重启你的代码,就可以了selectversion(),@@sql_mode;SETsql_mode=(SELECTRE......
  • k8s-label和selector
    说明k8s通过lable来为资源打上标签,通过selector来查找。而不是像传统mysql对象之间关联使用强关联外键属性比如deployment需要关联RS则通过RS打上标签,deployment通过配置select选择器去查找标签的特性label有如下的一些特点:label可以被附加到各种资源对象上一个资源对象......
  • 无涯教程-Clojure - select-keys函数
    返回包含键的值。select-keys-语法以下是语法。(select-keyshmapkeys)参数   - "hmap"是哈希键和值的映射。"keys"是需要从HashMap中选择的键列表。返回值 - 根据键的select子句从映射返回键。select-keys-示例(nsclojure.examples.example(:gen......
  • 《最新出炉》系列初窥篇-Python+Playwright自动化测试-23-处理select下拉框-下篇
    1.简介上一篇中宏哥主要讲解和分享了一下,我们常见或者传统的select下拉框的操作,但是近几年又出现了了一种新的select下拉框,其和我们传统的select下拉框完全不一样,那么我们如何使用playwright对其进行定位操作了。宏哥今天就来讲解和分享一下仅供大家参考,不喜勿喷。2.新的select......
  • select...for update到底是加了行锁,还是表锁?
    前言前几天,知识星球中的一个小伙伴,问了我一个问题:在MySQL中,事务A中使用select...forupdatewhereid=1锁住了,某一条数据,事务还没提交,此时,事务B中去用select...whereid=1查询那条数据,会阻塞等待吗?select...forupdate在MySQL中,是一种悲观锁的用法,一般情况下,会锁住一行数据,但......
  • vue el-select 下拉菜单 数据回显不在清除数据可编辑状态
    思路:当点击options的时候,让select失去焦点(跳到其他的地方),添加el-input就是为了把光标跳转到其上面,然后将其隐藏掉,则为看不到光标点,但是在点击第一次之后获取焦点focus之后,隐藏掉下拉菜单的时候@blur是无效的,此时只能通过监听下拉框是否是隐藏和显示状态来让其失去焦点事件,所以......
  • 关于在vue 中翻译select 下拉数据的操作
    可以使用object.keys()import*asfiltersfrom"./filters";//globalfiltersObject.keys(filters).forEach((key)=>{Vue.filter(key,filters[key]);//eslint-disable-lineno-undef});<el-select:value="detailRow.prpLpayeeD......
  • 《最新出炉》系列初窥篇-Python+Playwright自动化测试-22-处理select下拉框-上篇
    1.简介在实际自动化测试过程中,我们也避免不了会遇到下拉框选择的测试,因此宏哥在这里直接分享和介绍一下,希望小伙伴或者童鞋们在以后工作中遇到可以有所帮助。今天,我们讲下playwright的下拉框怎么处理,在使用selenium定位的过程中,我们可以选择使用selenium的Select类定位操作选择框......
  • el-select、el-dropdown 下拉框位置偏移
    el-select、el-dropdown下拉框位置偏移<el-dropdownclass="nav_item":class="activeIndex==2?'active':''"><spanclass="el-......
  • 2023ICLR_SFNet: Selective frequency network for image restoration
    1.在运行SFNet代码时,前后代码保持不变,运行两次结果发生变化,把下面这段代码注掉就可以保持前后两次运行结果一致,不确定是否是nn.BatchNorm2d计算均值和方差导致classdynamic_filter(nn.Module):def__init__(self,inchannels,mode,kernel_size=3,stride=1,group=8)......