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

el-form简单封装一个列表页中的搜索栏

父组件如何使用    代码中注释很多, 应该很容易理解

<template><div><wgySearchv-model="searchDefault":fields="searchFields"@reset="reset"@submit="submit"><!-- 通过 slot 自定义的组件  传啥都行 --><template slot="delivery"><el-switch v-model="searchDefault.delivery" /></template></wgySearch></div>
</template><script>
import wgySearch from './wgySearch';// 这两个组件是给search组件传递的
import wgySelect from './wgySelect';
import wgyDatePicker from './wgyDatePicker';data 中// 搜索栏中的默认值searchDefault: {name: '',region: '',delivery: '',date: '',},// 配置项searchFields: [{label: '活动名称',model: 'name',},{label: '活动区域',model: 'region',template: {// tpl传一个wgySelect组件,专门用来只用来传数据tpl: wgySelect,attrs: {multiple: true,list: [{ label: 'A', value: 1 },{ label: 'B', value: 2 },],},},},{label: '即时配送',slot: 'delivery',  // 这里配置项是slot, 模板中才能使用slot对应的字段},{label: '活动时间',model: 'date',template: {tpl: wgyDatePicker,attrs: {type: 'daterange','value-format': 'timestamp',},},},],reset和submit这两方法是搜索和重置的时候调用的
</script>

wgySelect组件

<template><el-selectv-model="value1"v-bind="$attrs"@input="input"><el-optionv-for="item in list":key="item.value":label="item.label":value="item.value"/></el-select>
</template>
<script>
export default {name: 'WgySelect',props: {value: {type: [Number, String, Array],required: true,},list: {type: Array,required: true,},},data() {return {value1: this.value,};},methods: {input(val) {this.$emit('input', val);},},
};
</script>

wgyDatePicker组件

<template><el-date-pickerv-model="value1"range-separator="至"start-placeholder="开始日期"v-bind="$attrs"end-placeholder="结束日期"@input="input"/>
</template><script>
export default {name: 'WgyDatepicker',props: {value: {type: [Array, Number, String],default: () => '',},},data() {return {value1: this.value,};},methods: {input(val) {this.$emit('input', val || '');},},
};
</script>

wgySearch 这个就是最主要的搜索组件了

<template><div><!-- 如果插槽  operatorLeft 或 operatorRight 存在则展示顶部区域 --><divv-if="$slots.operatorLeft || $slots.operatorRight"class="slot-operator clearfix"><div class="pull-left"><slot name="operatorLeft"></slot><!-- 表单的左上方区域  --></div><div class="pull-right"><slot name="operatorRight"></slot><!-- 表单的右上方区域 --></div></div><div class="form-ctn"><el-form:ref="formAttrs.ref || 'form'":model="formData":inline="true":label-width="getLabelWidth"label-position="right"v-bind="formAttrs"size="small"@submit.native.prevent="submit"@reset.native.prevent="reset"><div :style="getWrapperStyle"><el-form-itemv-for="(field, index) in innerFields":key="`${field.model || field.slot}${index}`":prop="field.prop":label="field.label":rules="field.rules":style="{display: (index + 1) > maxShowNum && !showAll ? 'none' : 'inline-block'}"><!-- 如果有telplate可能是复杂类型,比如日期,下拉框等 --><wgy-templatev-if="field.template"v-model="formData[field.model]":tpl="field.template.tpl":style="{width: `${getTempWidth(field.template.attrs)}px`}":attrs="field.template.attrs"v-on="field.template.events"/><!-- 没有telplate的话,就是输入框input --><!-- --><template v-else><!--如果input是个插槽,就展示插槽,否则展示自己的input绑定当前配置的model透传itemAttrs--><slotv-if="field.slot":name="field.slot"></slot><el-inputv-elsev-model.trim="formData[field.model]":placeholder="field.placeholder"clearableclass="w180"v-bind="field.itemAttrs"/></template></el-form-item><slot name="suffixCustomitems"></slot><div:class="btnClass":style="getBtnStyle"><el-buttonv-if="fields.length > maxShowNum"type="text"@click="showAll = !showAll"><i :class="showAll ? 'el-icon-caret-top' : 'el-icon-caret-bottom'"></i>{{ showAll ? '收起' : '展开' }}</el-button><el-buttonv-if="showSubmit"type="primary"native-type="submit"class="search-btn">{{ leftButton }}</el-button><el-buttonv-if="showReset"native-type="reset"class="reset-btn">{{ rightButton }}</el-button><span class="ml10"><slot name="searchbtn"></slot></span></div></div></el-form></div><divv-if="withDivider"class="divider"></div></div>
</template><script>
import wgyTemplate from './wgyTemplate';export default {components: {wgyTemplate,},props: {// 绑定的值value: {type: Object,required: true,},leftButton: {type: String,default: () => '查询',},rightButton: {type: String,default: () => '重置',},// 每行元素个数lineNumber: {type: Number,default: () => 4,},// label 的宽度labelWidth: {type: String,default: () => '100px',},// 透传给el-form的属性配置formAttrs: {type: Object,default: () => ({}),required: false,},// 配置字段fields: {type: Array,required: true,},// 是否显示搜索按钮showSubmit: {type: Boolean,default: true,},// 是否显示重置按钮showReset: {type: Boolean,default: true,},// 最大默认显示搜索项个数 ,超过这个数字后会收起maxShowNum: {type: Number,default: 8,},// 按钮组classbtnClass: {type: String,default: '',},// 自动包含下方的分割线(列表页常用)withDivider: {type: Boolean,default: true,},},data() {return {// 绑定的值formData: this.value,// 配置字段 < 默认显示搜索项个数showAll: this.fields.length < this.maxShowNum,};},computed: {// 处理placeholderinnerFields() {return this.fields.map((field) => {const { template, label } = field;// 根据是否有template属性,生成默认的placeholderconst defaultPlaceholder = template ? `请选择${label}` : `请输入${label}`;if (template) {// 如果有template属性,但没有attrs属性,初始化attrs为一个空对象template.attrs = template.attrs || {};// 如果attrs对象没有placeholder属性,使用默认的placeholdertemplate.attrs.placeholder = template.attrs.placeholder || defaultPlaceholder;} else {// 如果没有template属性,且没有placeholder属性,使用默认的placeholderfield.placeholder = field.placeholder || defaultPlaceholder;}return field;});},getWrapperStyle() {// 动态改变该元素的布局return {display: 'flex',flexWrap: 'wrap',};},getIsSameLine() {// 判断表单的字段数量是否小于等于每行的元素个数并且字段数量不能被每行的元素个数整除const { fields, lineNumber } = this;return fields.length <= lineNumber && !(fields.length % lineNumber === 0);},// 按钮的样式getBtnStyle() {return {textAlign: this.getIsSameLine ? 'left' : 'right',flex: 1,minWidth: '220px',marginBottom: '10px',};},/*** 计算表单标签的宽度* 如果用户已经设置了 labelWidth,则直接返回用户设置的值* 否则,根据表单字段的标签长度动态计算标签宽度*/getLabelWidth() {if (this.labelWidth) {return this.labelWidth;}// 获取表单字段标签的最大长度const maxLength = Math.max(...this.fields.map((item) => item.label.length));if (maxLength <= 4) {return '80px';}if (maxLength > 4 && maxLength <= 6) {return '90px';}if (maxLength > 6 && maxLength < 10) {return '120px';}return '100px';},validate() {return this.$refs.form.validate;},},watch: {// 外层变化实时通知内部, 内部变化实时通知外部value: {deep: true,handler(newVal) {this.formData = newVal;},},formData: {deep: true,handler(newVal) {this.$emit('input', newVal);},},},methods: {// 搜索submit() {this.$emit('submit');},// 重置reset() {this.$emit('reset');},/*获取每个输入框的宽度如果attrs中有传入, 使用传入的宽度如果是日期选择器,宽度是210如果是日期时间选择器,宽度是340其他的宽度都是180*/getTempWidth(attrs = {}) {const { width, type } = attrs;if (width) {return width;}if (type === 'daterange') {return 210;}if (type === 'datetimerange') {return 340;}return 180;},},
};
</script><style lang="scss">
.search-box {background: #fff;.slot-operator {border-bottom: 1px solid #eee;padding-bottom: 10px;margin-bottom: 10px;}.form-ctn {position: relative;.search-btn,.reset-btn {min-width: 0;}.reset-btn {margin-left: 8px;}}.el-form-item {margin-bottom: 16px;white-space: nowrap;}.el-form-item--small .el-form-item__label {padding-right: 5px;}.el-form-item--small .el-form-item__content {min-width: 180px;.el-date-editor--daterange .el-range-input {min-width: 70px;}}
}
.divider {box-sizing: border-box;background-color: #fafafb;height: 10px;border-top: 1px solid #eeeeee;border-bottom: 1px solid #eeeeee;
}
</style>

wgyTemplate  是wgySearch组件中的子组件

<template><component:is="dynamicComponent":value="value"v-bind="attrs"v-on="listeners"/>
</template><script>export default {name: 'WgyTemplate',props: {tpl: {type: [String, Object],required: true,},value: {type: [Number, String, Object, Array],required: true,},attrs: {type: Object,default: () => ({}),},},computed: {// 要渲染的组件dynamicComponent() {return this.tpl;},// 收集所有需要绑定到动态组件上的事件监听器listeners() {return {input: this.input,...this.$listeners,};},},methods: {input(value) {this.$emit('input', value);},},
};
</script>

相关文章:

el-form简单封装一个列表页中的搜索栏

父组件如何使用 代码中注释很多, 应该很容易理解 <template><div><wgySearchv-model"searchDefault":fields"searchFields"reset"reset"submit"submit"><!-- 通过 slot 自定义的组件 传啥都行 --><te…...

【Python 2】列表 模式匹配 循环 dict set 可变对象与不可变对象

Python内置的一种数据类型是列表&#xff1a;list 变量classmates就是一个list。用len()函数可以获得list元素的个数 用索引来访问list中每一个位置的元素 当索引超出了范围时&#xff0c;Python会报一个IndexError错误&#xff0c;所以&#xff0c;要确保索引不要越界&#xf…...

深度学习—cv动物/植物数据集

文章目录 动物相关植物相关 动物相关 Edinburgh Pig Behavior Video Dataset:https://homepages.inf.ed.ac.uk/rbf/PIGDATA/ WLD 动物目标检测数据集: https://github.com/hellock/WLD 猪脸识别&#xff1a;https://blog.51cto.com/u_15404184/5289690 AFD动物面部数据集&…...

高效团队协作软件推荐:提升工作效率的优选方案!

使用团队协作软件有什么好处&#xff1f;可以摆脱过时的电子表格&#xff0c;有了单一的真实来源&#xff0c;您可以随时检查任何任务并获得可用的最新信息。 一目了然地查看所有正在进行的工作&#xff0c;看板式面板、甘特图和燃尽图等可视化工具可让您随时轻松获得项目的高级…...

Mac中使用virtualenv和virtualenvwrapper

Virtualenv 介绍 在使用 Python 开发的过程中&#xff0c;工程一多&#xff0c;难免会碰到不同的工程依赖不同版本的库的问题&#xff1b;亦或者是在开发过程中不想让物理环境里充斥各种各样的库&#xff0c;引发未来的依赖灾难。 因此&#xff0c;我们需要对于不同的工程使…...

wpf webBrowser控件 常用的函数和内存泄漏问题

介绍 WebBrowsers可以让我们在窗体中进行导航网页。 WebBrowser控件内部使用ie的引擎&#xff0c;因此使用WebBrowser我们必须安装ie浏览器&#xff08;windows默认安装的&#xff09;。 使用 直接在xmal中使用webBrowser控件 <WebBrowser x:Name"WebBrowser1"…...

AI游戏设计的半年度复盘;大模型+智能音箱再起波澜;昇思大模型技术公开课第2期;出海注册经验分享;如何使用LoRA微调Llama 2 | ShowMeAI日报

&#x1f440;日报&周刊合集 | &#x1f3a1;生产力工具与行业应用大全 | &#x1f9e1; 点赞关注评论拜托啦&#xff01; &#x1f525; 进步or毁灭&#xff1a;Nature 调研显示 1600 科学家对AI的割裂态度 国际顶级期刊 Nature 最近一项调研很有意思&#xff0c;全球 160…...

多线程 - 锁策略 CAS

常见的锁策略 此处谈到的锁策略,不局限于 Java,C,Python,数据库,操作系统……但凡是涉及到锁,都是可以应用到下列的锁策略的 乐观锁 vs 悲观锁 锁的实现者,预测接下来锁冲突(锁竞争,两个线程针对一个对象加锁,产生阻塞等待了)的概率是大,还是不大,根据这个冲突的概率,来接下…...

VP记录——The 2021 CCPC Weihai Onsite

网址 2021CCPC威海 赛时过题与罚时 A.Goodbye, Ziyin! 签到题&#xff0c;队友写的 #include<bits/stdc.h> using namespace std; int cnt[10], de[1000010]; int main() {int n;cin >> n;for(int i 1; i < n; i) {int u, v;scanf("%d %d", &…...

JavaWeb---Servlet

1.Srvlet概述 Servlet是运行在java服务器端的程序&#xff0c;用于接收和响应来着客户端基于HTTP协议的请求 如果想实现Servlet的功能&#xff0c;可以通过实现javax。servlet。Servlet接口或者继承它的实现类 核心方法&#xff1a;service&#xff08;&#xff09;&#xf…...

英语——方法篇——单词——谐音法+拼音法——50个单词记忆

theatre&#xff0c;剧场&#xff0c;太后th吃eat热re食物&#xff0c;就去剧场了 loud dolphin&#xff0c;做do脸皮厚plh在。。。里 humid&#xff0c;hu湖mi米d的 blender&#xff0c;b爸lend借给er儿。 tragedy&#xff0c;tr土人...

35道Rust面试题

这套Rust面试题包括了填空题、判断题、连线题和编码题等题型。 选择题 1 &#xff0c;下面哪个是打印变量language的正确方法&#xff1f; A&#xff0c;println("{}", language); B&#xff0c;println(language); C&#xff0c;println!("{}", langu…...

01 时钟配置初始化,debug

1. 开启debug series&#xff0c;否则只能下载一次&#xff0c;再次下载要配置boot 2.f0外部时钟配置 h750 配置 实测可用...

Halcon我的基础教程(一)(我的菜鸟教程笔记)-halcon仿射变换(Affine Transformation)的探究与学习

目录 什么是仿射变换?仿射变换有哪些方式?任何仿射变换都能由以下基本变换构造而来:在Halocn中,仿射变换具有重要的作用,那我们本文章重点讨论仿射变换基础性知识。 使用Halcon中的重要算子,仿射变换一般解决步骤,案例应用会在以后的文章中我们重点解答与讨论。 我们首先…...

c++视觉---中值滤波处理

中值滤波&#xff08;Median Filter&#xff09;是一种常用的非线性平滑滤波方法&#xff0c;用于去除图像中的噪声。它不像线性滤波&#xff08;如均值滤波或高斯滤波&#xff09;那样使用权重来计算平均值或加权平均值&#xff0c;而是选择滤波窗口内的像素值中的中间值作为输…...

Edge使用猴油脚本实战(实验室安全考试系统刷在线时长——网站永久自动刷新)

介绍 篡改猴 (Tampermonkey) 是拥有 超过 1000 万用户 的最流行的浏览器扩展之一。它允许用户自定义并增强您最喜爱的网页的功能。用户脚本是小型 JavaScript 程序&#xff0c;可用于向网页添加新功能或修改现有功能。使用 篡改猴&#xff0c;您可以轻松在任何网站上创建、管理…...

Vue 中 KeepAlive 内置缓存使用

KeepAlive 介绍及使用场景 KeepAlive 是 vue 中的内置组件&#xff0c;当多个组件动态切换时可以对实例状态进行缓存&#xff0c;用法如下 <router-view v-slot"{ Component }"><keep-alive><component :is"Component" /></keep-al…...

语言模型编码中/英文句子格式详解

文章目录 前言一、Bert的vocab.txt内容查看二、BERT模型转换方法(vocab.txt)三、vocab内容与模型转换对比四、中文编码总结 前言 最近一直在学习多模态大模型相关内容&#xff0c;特别是图像CV与语言LLM模型融合方法&#xff0c;如llama-1.5、blip、meta-transformer、glm等大…...

【Node.js】路由

基础使用 写法一&#xff1a; // server.js const http require(http); const fs require(fs); const route require(./route) http.createServer(function (req, res) {const myURL new URL(req.url, http://127.0.0.1)route(res, myURL.pathname)res.end() }).listen…...

matlab 2ask 4ask 信号调制

1 matlab 2ask close all clear all clcL =1000;Rb=2822400;%码元速率 Fs =Rb*8; Fc=Rb*30;%载波频率 Ld =L*Fs/Rb;%产生载波信号 t =0:1/Fs:L/Rb;carrier&...

【网络】每天掌握一个Linux命令 - iftop

在Linux系统中&#xff0c;iftop是网络管理的得力助手&#xff0c;能实时监控网络流量、连接情况等&#xff0c;帮助排查网络异常。接下来从多方面详细介绍它。 目录 【网络】每天掌握一个Linux命令 - iftop工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景…...

基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真

目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.算法仿真参数 5.算法理论概述 6.参考文献 7.完整程序 1.程序功能描述 通过动态调整节点通信的能量开销&#xff0c;平衡网络负载&#xff0c;延长WSN生命周期。具体通过建立基于距离的能量消耗模型&am…...

【第二十一章 SDIO接口(SDIO)】

第二十一章 SDIO接口 目录 第二十一章 SDIO接口(SDIO) 1 SDIO 主要功能 2 SDIO 总线拓扑 3 SDIO 功能描述 3.1 SDIO 适配器 3.2 SDIOAHB 接口 4 卡功能描述 4.1 卡识别模式 4.2 卡复位 4.3 操作电压范围确认 4.4 卡识别过程 4.5 写数据块 4.6 读数据块 4.7 数据流…...

Python Ovito统计金刚石结构数量

大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...

JavaScript 数据类型详解

JavaScript 数据类型详解 JavaScript 数据类型分为 原始类型&#xff08;Primitive&#xff09; 和 对象类型&#xff08;Object&#xff09; 两大类&#xff0c;共 8 种&#xff08;ES11&#xff09;&#xff1a; 一、原始类型&#xff08;7种&#xff09; 1. undefined 定…...

Linux nano命令的基本使用

参考资料 GNU nanoを使いこなすnano基础 目录 一. 简介二. 文件打开2.1 普通方式打开文件2.2 只读方式打开文件 三. 文件查看3.1 打开文件时&#xff0c;显示行号3.2 翻页查看 四. 文件编辑4.1 Ctrl K 复制 和 Ctrl U 粘贴4.2 Alt/Esc U 撤回 五. 文件保存与退出5.1 Ctrl …...

NPOI操作EXCEL文件 ——CAD C# 二次开发

缺点:dll.版本容易加载错误。CAD加载插件时&#xff0c;没有加载所有类库。插件运行过程中用到某个类库&#xff0c;会从CAD的安装目录找&#xff0c;找不到就报错了。 【方案2】让CAD在加载过程中把类库加载到内存 【方案3】是发现缺少了哪个库&#xff0c;就用插件程序加载进…...

掌握 HTTP 请求:理解 cURL GET 语法

cURL 是一个强大的命令行工具&#xff0c;用于发送 HTTP 请求和与 Web 服务器交互。在 Web 开发和测试中&#xff0c;cURL 经常用于发送 GET 请求来获取服务器资源。本文将详细介绍 cURL GET 请求的语法和使用方法。 一、cURL 基本概念 cURL 是 "Client URL" 的缩写…...

stm32wle5 lpuart DMA数据不接收

配置波特率9600时&#xff0c;需要使用外部低速晶振...

Linux中《基础IO》详细介绍

目录 理解"文件"狭义理解广义理解文件操作的归类认知系统角度文件类别 回顾C文件接口打开文件写文件读文件稍作修改&#xff0c;实现简单cat命令 输出信息到显示器&#xff0c;你有哪些方法stdin & stdout & stderr打开文件的方式 系统⽂件I/O⼀种传递标志位…...