element UI 组件封装--搜索表单(含插槽和内嵌组件)

简介: element UI 组件封装--搜索表单(含插槽和内嵌组件)

组件封装–搜索表单 searchForm.vue

可根据需要,参考姓名和工作自行增加更多常用的默认搜索项

<template>
  <div style="padding: 30px; width: 300px">
    <el-form
      ref="searchFormRef"
      :model="searchData"
      :label-width="searchOption.labelWidth || '80px'"
      :size="searchOption.size || 'mini'"
    >
      <el-form-item
        v-if="searchOption.name"
        label="姓名"
        prop="name"
        :rules="{ required: true, message: '不能为空' }"
      >
        <el-input v-model="searchData.name"></el-input>
      </el-form-item>

      <SearchJob v-if="searchOption.job" :searchData="searchData" />
      <slot> </slot>

      <el-form-item>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import SearchJob from "./search_job.vue";
export default {
  components: {
    SearchJob,
  },
  props: {
    searchData: Object,
    searchOption: Object,
  },
  methods: {
    search() {
      this.$refs.searchFormRef.validate((valid) => {
        if (valid) {
          alert("通过表单校验,开始搜索!");
        }
      });
    },
    reset() {
      this.$emit("reset");
    },
  },
};
</script>

内嵌组件 search_job.vue

具体原理可参考

https://blog.csdn.net/weixin_41192489/article/details/127966234

<template>
  <el-form-item
    label="工作"
    prop="job"
    :rules="{ required: true, message: '不能为空' }"
  >
    <el-input v-model="searchData.job"></el-input>
  </el-form-item>
</template>

<script>
export default {
  props: {
    searchData: Object,
  },
};
</script>

使用方法 index.vue

  • 可直接在 SearchForm 内部通过默认插槽添加更多搜索项
  • 必要变量有 searchData 和 searchOption
  • 必要方法有 reset
<template>
  <div style="padding: 30px; width: 300px">
    <SearchForm
      :searchData="searchData"
      :searchOption="searchOption"
      @reset="reset"
    >
      <el-form-item
        label="爱好"
        prop="hobby"
        :rules="{ required: true, message: '不能为空' }"
      >
        <el-input v-model="searchData.hobby"></el-input>
      </el-form-item>
    </SearchForm>
  </div>
</template>

<script>
import SearchForm from "./searchForm.vue";
export default {
  components: {
    SearchForm,
  },
  data() {
    return {
      searchData: {},
      // 搜索表单的配置--是否显示默认搜索项
      searchOption: {
        labelWidth: "100px",
        size: "small",
        name: true,
        job: true,
      },
    };
  },
  methods: {
    reset() {
      this.searchData = {};
    },
  },
};
</script>
目录
相关文章
|
2天前
|
数据安全/隐私保护
Element UI 密码输入框--可切换显示隐藏,自定义图标
Element UI 密码输入框--可切换显示隐藏,自定义图标
7 0
|
2天前
Element UI 表格【列宽自适应】
Element UI 表格【列宽自适应】
4 0
|
2天前
|
前端开发
ElementPlus卡片如何能够一行呈四,黑马UI前端布局视频资料,element样式具体的细节无法修改,F12找到那个位置,可能在其他组件写了错误,找到那个位置,围绕着位置解决问题最快了,卡片下边
ElementPlus卡片如何能够一行呈四,黑马UI前端布局视频资料,element样式具体的细节无法修改,F12找到那个位置,可能在其他组件写了错误,找到那个位置,围绕着位置解决问题最快了,卡片下边
|
2月前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
|
2月前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
|
2月前
|
前端开发 JavaScript API
SAP UI5 sap.ui.require.toUrl 的作用介绍
SAP UI5 sap.ui.require.toUrl 的作用介绍
|
2月前
|
JSON 前端开发 测试技术
SAP UI5 sap.ui.core.util.MockServer.simulate 方法介绍
SAP UI5 sap.ui.core.util.MockServer.simulate 方法介绍
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
|
2月前
|
缓存 JavaScript 前端开发
如何理解 SAP UI5 的 sap.ui.define 函数?
如何理解 SAP UI5 的 sap.ui.define 函数?
|
2月前
|
监控 测试技术
SAP 电商云修改 Product Catalog Staged 版本数据后,同步到 online 版本的 UI 操作
SAP 电商云修改 Product Catalog Staged 版本数据后,同步到 online 版本的 UI 操作