搜索组件vue3 ant 封装

搜索 组件

<template>
  <a-card>
    <a-form
      ref="formRef"
      name="advanced_search"
      :model="searchInfo"
      :label-col="{ span: 8 }"
      :wrapper-col="{ span: 16 }"
      layout="inline"
    >
      <div v-for="(item, index) in searchData" :key="index">
        <a-form-item v-if="item.type == 'input'" :label="item.label" :name="item.value">
          <a-input
            v-model:value="searchInfo[item.value]"
            :placeholder="item.placeholder"
            style="width: 200px"
          />
        </a-form-item>
        <a-form-item v-if="item.type == 'select'" :label="item.label" :name="item.value">
          <a-select
            v-model:value="searchInfo[item.value]"
            :placeholder="item.placeholder"
            allowClear
            style="width: 200px">
            <a-select-option v-for="(ele,index) in item.options" :key="index" :value="ele.value">
              {{ele.label}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item v-if="item.type == 'selects'" :label="item.label" :name="item.value">
          <a-select
            v-model:value="searchInfo[item.value]"
            :placeholder="item.placeholder"
            :options="item.options"
            mode="multiple"
            max-tag-count="responsive"
            allowClear
            style="width: 210px"
          ></a-select>
        </a-form-item>
        <a-form-item v-if="item.type === 'picker'" :label="item.label" :name="item.value">
          <a-date-picker
            v-model:value="searchInfo[item.value]"
            show-time
            type="date"
            :placeholder="item.placeholder"
            style="width: 100%"
          />
        </a-form-item>
        <a-form-item v-if="item.type === 'pickers'" :label="item.label" :name="item.value">
          <a-range-picker
            v-model:value="searchInfo[item.value]"
            :showTime="showTime"
            :format="format"
            :value-format="valueFormat"
            style="width: 100%"
          />
        </a-form-item>
      </div>
      <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
        <a-button type="primary" @click="onSubmit">查 询</a-button>
      </a-form-item>
      <a-form-item>
        <a-button style="margin-left: 10px" @click="() => formRef.resetFields()"
          >重 置</a-button
        >
      </a-form-item>
    </a-form>
  </a-card>
</template>
<script setup lang="ts">
import { reactive, ref, toRaw } from "vue";
import type { FormInstance } from 'ant-design-vue';

const props = defineProps({
  // 数据,必传
  searchInfo: { type: Object, default: Function, },
  // 配置描述,必传
  searchData: { type: Array,  default: Function, },
  // 是否显示时间
  showTime: { type: Boolean,  default: false, },
  // 时间区间 格式 
  format: { type: String, default: "YYYY-MM-DD", },
  valueFormat: {  type: String, default: "YYYY-MM-DD", },
})

const emits = defineEmits(['submit-data' ])

const formRef = ref<FormInstance>();
const searchInfo = reactive(props.searchInfo);
const searchData = reactive(props.searchData);

const onSubmit = () => {
  emits("submit-data", toRaw(searchInfo));
};

</script>
<style scoped lang="less" >
.ant-card-bordered {
  border: none;
  margin: 0px -24px;
}
</style>

调用

<Search class="search-box" :searchData="searchData" @submitData="submit" :searchInfo="info"></Search>

import Search from "@/components/search/index.vue";

// 查询
  searchInfo ={
	  product: '',
	  process: null,
	  pgmVersion: '',
	  owner: '',
	 }
  searchArr = reactive([
    { type: "input", value: "product", label: "Product", placeholder: "请输入内容", },
    { type: "select", value: "process", label: "Process", placeholder: "请选择",
		options: [
			{ label: "RDBI", value: "RDBI" },
			{ label: "FT1", value: "FT1" },
			{ label: "FT2", value: "FT2" },
			{ label: "FT3", value: "FT3" },
			{ label: "FT4", value: "FT4" },
		],
	},
    { type: "input", value: "pgmVersion", label: "PGMVersion", placeholder: "请输入内容", },
    { type: "input", value: "owner", label: "Owner", placeholder: "请输入内容", },
  ])
  searchData = toRaw(this.searchArr);
  info = toRaw(this.searchInfo)
  submit = () => this.onChange(1, this.paginationOpt.pageSize )
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您解答这个问题。 首先,我们可以使用 Ant Design Vue 中的 Modal 组件封装一个弹框。下面是一个简单的示例代码: ```html <template> <a-modal :visible="visible" @ok="handleOk" @cancel="handleCancel"> <p>{{ message }}</p> </a-modal> </template> <script> import { defineComponent } from 'vue'; import { Modal } from 'ant-design-vue'; export default defineComponent({ name: 'MyModal', components: { Modal, }, props: { visible: Boolean, message: String, }, methods: { handleOk() { this.$emit('ok'); }, handleCancel() { this.$emit('cancel'); }, }, }); </script> ``` 在这个示例中,我们使用了 Ant Design Vue 中的 Modal 组件,并将它封装成一个名为 MyModal 的组件。我们在组件中定义了两个 props:visible 和 message,分别用来控制弹框的显示和传递弹框的内容。我们还定义了两个方法:handleOk 和 handleCancel,分别在用户点击确认和取消按钮时触发,并通过 $emit 方法向父组件传递事件。 接下来,我们可以在父组件中引入 MyModal 组件,并使用 v-model 来控制弹框的显示。下面是一个示例代码: ```html <template> <div> <a-button @click="showModal">打开弹框</a-button> <my-modal v-model="modalVisible" :message="modalMessage" @ok="handleOk" @cancel="handleCancel" /> </div> </template> <script> import { defineComponent, ref } from 'vue'; import MyModal from './MyModal.vue'; export default defineComponent({ name: 'MyComponent', components: { MyModal, }, setup() { const modalVisible = ref(false); const modalMessage = ref(''); const showModal = () => { modalVisible.value = true; modalMessage.value = '这是弹框的内容'; }; const handleOk = () => { modalVisible.value = false; console.log('用户点击了确认按钮'); }; const handleCancel = () => { modalVisible.value = false; console.log('用户点击了取消按钮'); }; return { modalVisible, modalMessage, showModal, handleOk, handleCancel, }; }, }); </script> ``` 在这个示例中,我们首先引入了 MyModal 组件,并在模板中使用 v-model 来控制弹框的显示。我们还定义了一个 showModal 方法,用来在用户点击按钮时显示弹框,并传递弹框的内容。在 MyModal 组件中,我们将父组件传递的 visible 和 message props 绑定到 Modal 组件上,并通过 $emit 方法向父组件传递事件。 这样,我们就成功地封装了一个弹框组件,并在父组件中调用了它。希望这个示例能够帮助到您。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值