vue3 + ts + element-plus(封装form搜索栏)

 一、安装 element-plus

element-plus官网icon-default.png?t=N7T8https://element-plus.org/zh-CN/


1、npm安装
npm install element-plus --save
2、yarn安装
yarn add element-plus
3、pnpm安装
pnpm install element-plus
4、main.ts导入(vue3)
import ElementPlus from 'element-plus'

import 'element-plus/dist/index.css'

app.use(ElementPlus)
二、封装搜索表单
<template>
  <el-card shadow="never">
    <el-form :inline="true" :model="form">
      <template v-for="(item, index) in formItems" :key="index">
        <el-form-item :label="item.label">
          <component
            :is="getComponentType(item.type)"
            v-model="form[item.field]"
            :placeholder="item.placeholder"
            :options="item.options"
            @keyup.enter="onEnterKey"
          ></component>
        </el-form-item>
      </template>
      <el-form-item>
        <el-button type="primary" round @click="search">搜索</el-button>
        <el-button round @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts" setup>
import { ElInput } from 'element-plus';
import CustomSelect from './CustomSelect.vue';
import { ref, defineProps, defineEmits } from 'vue';

const props = defineProps({
    formItems: {
        type: Array as () => Array<{ type: string, label: string, placeholder: string, field: string, options?: Array<{ label: string, value: string | number }> }>,
        required: true
    }
});

const emit = defineEmits(['search', 'reset', 'enterKey']);

const form = ref<Record<string, any>>({});

props.formItems.forEach(item => {
    form.value[item.field] = '';
});

const search = () => {
    emit('search', form.value);
};

const reset = () => {
    props.formItems.forEach(item => {
        form.value[item.field] = '';
    });
    emit('reset');
};

const onEnterKey = () => {
    emit('enterKey', form.value);
};

const getComponentType = (type: string) => {
    switch (type) {
        case 'input':
            return ElInput;
        case 'select':
            return CustomSelect;
        default:
            return ElInput;
    }
};
</script>
1、CustomSelect.vue文件
<template>
  <el-select :style="{ width: '100%' }" v-model="innerValue" :placeholder="placeholder" @change="handleChange" :multiple="isMultiple">
    <el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value" />
  </el-select>
</template>

<script lang="ts" setup>
import { ref, watch, defineProps, defineEmits } from 'vue';

const props = defineProps({
    modelValue: {
        type: [String, Number, Array],
        required: true
    },
    placeholder: String,
    options: {
        type: Array as () => { label: string; value: string | number }[],
        required: true
    },
    multiple: {
        type: Boolean,
        default: false
    }
});

const emit = defineEmits(['update:modelValue']);

const innerValue = ref(props.modelValue);

const handleChange = (value) => {
    innerValue.value = value;
    emit('update:modelValue', value);
};

watch(() => props.modelValue, (newValue) => {
    innerValue.value = newValue;
});

const isMultiple = ref(props.multiple);
</script>
三、使用封装的搜索表单
<template>
    <iot-search :formItems="formItems" @search="handleSearch" @reset="handleReset"
        @enterKey="handleSearch" />
</template>
<script lang="ts" setup>
import IotSearch from '@/components/iot-search.vue';

const formItems = ref<any>([
    {
        type: 'input',
        label: '名字',
        placeholder: '请输入名字',
        field: 'name',
    },
    {
        type: 'select',
        label: '类型',
        placeholder: '请选择类型',
        field: 'type',
        options: [
            { label: "type1", value: "0" },
            { label: "type2", value: "1" },
        ]
    },
    {
        type: 'select',
        label: '类型',
        placeholder: '请选择类型',
        field: 'type',
        options: [] // 如果下拉框数组是从接口返回,可以使用map返回接口里面的数据,遍历formItems接收数据
    },
])

const handleSearch = (formData: any) => {
   console.log(formData,'搜索表单')
}

const handleReset = () => {
  console.log('重置表单')
}
</script>

  • 13
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先你需要在项目中安装 `element-plus`,可以通过以下命令进行安装: ``` npm install element-plus --save ``` 然后在你的 Vue 项目中引入 `element-plus` 的 `Table` 组件,以及需要使用的相关样式: ```js import { defineComponent } from 'vue'; import { ElTable, ElTableColumn } from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; export default defineComponent({ components: { ElTable, ElTableColumn }, data() { return { tableData: [], // 后台获取的数据 }; }, mounted() { // 在这里调用后台接口获取数据,并将返回的数据赋值给 tableData }, render() { return ( <div> <el-table data={this.tableData}> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </div> ); }, }); ``` 在上面的代码中,我们使用了 `ElTable` 和 `ElTableColumn` 组件来渲染表格,其中 `data` 属性绑定了从后台获取的数据 `tableData`,每个 `el-table-column` 标签的 `prop` 属性绑定了对应数据对象的属性名,`label` 属性则是表格列的标题。 当然,你还需要在项目中引入 `element-plus` 的样式,这里我们直接引入了整个 `index.css` 文件来覆盖默认样式。如果你只需要使用部分组件,可以按需引入对应的样式文件。 以上就是使用 `element-plus` 的 `Table` 组件渲染后台数据的基本方法,你可以根据具体需求进行进一步的定制和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值