最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3 如何通過(guò)json配置生成查詢(xún)表單

 更新時(shí)間:2025年09月15日 16:28:33   作者:Martin-Luo  
本文給大家介紹Vue3如何通過(guò)json配置生成查詢(xún)表單,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

功能實(shí)現(xiàn)背景

通過(guò)Vue3實(shí)現(xiàn)后臺(tái)管理項(xiàng)目一定含有表格功能,通常離不開(kāi)表單。于是通過(guò)json配置來(lái)生成表單的想法由然而生

注意:
1.項(xiàng)目依賴(lài)element-plus

使用規(guī)則
1. 組件支持使用v-model管理值,當(dāng)v-model不配置時(shí)也可以通過(guò)@finish事件獲取表單值
2. 當(dāng)FormRender組件設(shè)置v-model后,schema配置項(xiàng)中defaultValue設(shè)置的默認(rèn)值無(wú)效
3. 項(xiàng)目默認(rèn)查詢(xún)和重置事件,也支持插槽自定義事件
4. 通過(guò)插槽自定義事件時(shí),可以通過(guò)插槽獲取表單值,也可以通過(guò)組件暴露屬性和方法獲取el-form屬性&方法和表單值

項(xiàng)目代碼

  1. 創(chuàng)建type/index.ts文件
import { type Component } from 'vue'
export type ObjAny = { [T: string]: any }
export interface SchemaItem {
  key: string // 唯一標(biāo)識(shí) & 表單項(xiàng)v-model的屬性
  label: string // form-item 的label屬性
  type?: 'input' | 'select' // 支持 el-input 和 el-select組件
  defaultValue?: any // 當(dāng)組件未配置v-model屬性,可自定義默認(rèn)值
  component?: Component // 自定義組件
  props?: { [K: string]: any } // 組件屬性:繼承el-form表單組件屬性
}
  1. 創(chuàng)建FormRender.vue文件
<template>
  <el-form ref="formRef" :model="dataForm" :inline="true">
    <el-form-item v-for="item in schema" :key="item.key" :label="item.label" :prop="item.key">
      <!-- 自定義組件 -->
      <template v-if="item.component">
        <component :is="item.component" v-bind="item.props" v-model="dataForm[item.key]" />
      </template>
      <!-- el-select -->
      <template v-else-if="item.type === 'select'">
        <el-select v-bind="item.props" v-model="dataForm[item.key]" />
      </template>
      <!-- 默認(rèn): el-input -->
      <template v-else>
        <el-input v-bind="item.props" v-model="dataForm[item.key]" />
      </template>
    </el-form-item>
    <!-- 事件插槽,默認(rèn)查詢(xún)和重置功能,支持自定義 -->
    <slot name="handle" :data="{ ...dataForm }">
      <el-form-item v-if="showFinish || showReset">
        <el-button v-if="showFinish" :loading="loading" type="primary" @click="handleClick">{{ textFinish }}</el-button>
        <el-button v-if="showReset" type="primary" @click="handleReset">{{ textReset }}</el-button>
      </el-form-item>
    </slot>
  </el-form>
</template>
<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { reactive, useTemplateRef } from 'vue'
import {ObjAny, SchemaItem} from 'type/index.ts'
defineOptions({
  name: 'FormRender'
})
const props = withDefaults(
  defineProps<{
    showFinish?: boolean
    showReset?: boolean
    textFinish?: string
    textReset?: string
    schema: SchemaItem[]
  }>(),
  {
    showFinish: true,
    showReset: true,
    textFinish: '查詢(xún)',
    textReset: '重置'
  }
)
const emit = defineEmits<{
  (e: 'finish', data: ObjAny): void
  (e: 'reset', data: ObjAny): void
}>()
const dataForm = defineModel() as ObjAny
const loading = defineModel('loading', { type: Boolean, default: false })
const formRef = useTemplateRef<FormInstance | null>('formRef')
initForm()
/**
 * 當(dāng)組件未定義 v-model,內(nèi)部生成form data
 */
function initForm() {
  if (dataForm.value === undefined) {
    const defaultForm: { [T: string]: any } = reactive({})
    props.schema.forEach(item => {
      defaultForm[item.key] = item.defaultValue || ''
    })
    if (dataForm.value === undefined) {
      dataForm.value = defaultForm
    }
  }
}
/**
 * finish
 */
function handleClick() {
  emit('finish', { ...dataForm.value })
}
/**
 * reset
 */
function handleReset() {
  formRef.value?.resetFields()
  emit('reset', { ...dataForm.value })
}
// 默認(rèn)暴露的屬性和方法,可自行添加
defineExpose({
  elFormInstance: formRef,
  reset: handleReset
})
</script>

案例

  1. 簡(jiǎn)單渲染和取值:未使用v-model
<FormRender :schema="schema" @finish="handleSubmit" />
<script lang="ts" setup>
import FormRender from './FormRender.vue'
import {ElInput} from 'element-plus'
import {ref, onMounted, computed} from 'vue'
import {ObjAny, SchemaItem} from 'type/index.ts'
const options = ref<{ label: string; value: string }[]>([])
// 默寫(xiě)數(shù)據(jù)需要通過(guò)網(wǎng)絡(luò)請(qǐng)求獲取,所有需要使用到 computed
const schema = computed<SchemaItem[]>(() => [
  {
    key: 'content',
    label: '名稱(chēng)',
    type: 'input',
    defaultValue: '張三',
    props: {
      placeholder: '請(qǐng)輸入名稱(chēng)',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'orderNo',
    label: '訂單號(hào)',
    defaultValue: '20250012',
    component: ElInput,
    props: {
      placeholder: '請(qǐng)輸入訂單號(hào)',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'state',
    label: '狀態(tài)',
    type: 'select',
    props: {
      placeholder: '請(qǐng)選擇狀態(tài)',
      clearable: true,
      options: options.value,
      style: {
        width: '200px'
      }
    }
  }
])
function handleSubmit(value: ObjAny) {
  console.log(value)
}
onMounted(() => {
 // 模擬網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)
  options.value = [
    {
      value: '1',
      label: 'Option1'
    },
    {
      value: '2',
      label: 'Option2'
    },
    {
      value: '3',
      label: 'Option3'
    }
  ]
})
</script>
  1. 使用v-model
<FormRender v-model='data' :schema="schema" @finish="handleSubmit" />
<script lang="ts" setup>
import FormRender from './FormRender.vue'
import {ElInput} from 'element-plus'
import {ref, onMounted, computed} from 'vue'
import {ObjAny, SchemaItem} from 'type/index.ts'
const data = ref({
  content: '張三',
  orderNo: '20250012',
  state: ''
})
const options = ref<{ label: string; value: string }[]>([])
// 默寫(xiě)數(shù)據(jù)需要通過(guò)網(wǎng)絡(luò)請(qǐng)求獲取,所有需要使用到 computed
// 當(dāng)使用v-model時(shí), defaultValue值將失效
const schema = computed<SchemaItem[]>(() => [
  {
    key: 'content',
    label: '名稱(chēng)',
    type: 'input',
    props: {
      placeholder: '請(qǐng)輸入名稱(chēng)',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'orderNo',
    label: '訂單號(hào)',
    component: ElInput,
    props: {
      placeholder: '請(qǐng)輸入訂單號(hào)',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'state',
    label: '狀態(tài)',
    type: 'select',
    props: {
      placeholder: '請(qǐng)選擇狀態(tài)',
      clearable: true,
      options: options.value,
      style: {
        width: '200px'
      }
    }
  }
])
function handleSubmit(value: ObjAny) {
  console.log(value)
}
onMounted(() => {
 // 模擬網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)
  options.value = [
    {
      value: '1',
      label: 'Option1'
    },
    {
      value: '2',
      label: 'Option2'
    },
    {
      value: '3',
      label: 'Option3'
    }
  ]
})
</script>
  1. 使用slot自定義事件
<FormRender ref="formRenderRef" v-model='data' :schema="schema">
  <template v-slot:handle="{ data }">
    <el-form-item>
      <el-button type="primary" @click="handleSubmit(data)">查詢(xún)</el-button>
      <el-button @click="handleReset">重置</el-button>
    </el-form-item>
  </template>
</FormRender>
<script lang="ts" setup>
import FormRender from './FormRender.vue'
import {ElInput} from 'element-plus'
import {ref, onMounted, computed} from 'vue'
import {ObjAny, SchemaItem} from 'type/index.ts'
const formRenderRef = useTemplateRef('formRenderRef')
const data = ref({
  content: '張三',
  orderNo: '20250012',
  state: ''
})
const options = ref<{ label: string; value: string }[]>([])
// 默寫(xiě)數(shù)據(jù)需要通過(guò)網(wǎng)絡(luò)請(qǐng)求獲取,所有需要使用到 computed
// 當(dāng)使用v-model時(shí), defaultValue值將失效
const schema = computed<SchemaItem[]>(() => [
  {
    key: 'content',
    label: '名稱(chēng)',
    type: 'input',
    props: {
      placeholder: '請(qǐng)輸入名稱(chēng)',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'orderNo',
    label: '訂單號(hào)',
    component: ElInput,
    props: {
      placeholder: '請(qǐng)輸入訂單號(hào)',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'state',
    label: '狀態(tài)',
    type: 'select',
    props: {
      placeholder: '請(qǐng)選擇狀態(tài)',
      clearable: true,
      options: options.value,
      style: {
        width: '200px'
      }
    }
  }
])
function handleSubmit(value: ObjAny) {
  console.log(value)
}
const handleReset = () => {
  formRenderRef.value?.reset()
}
onMounted(() => {
 // 模擬網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)
  options.value = [
    {
      value: '1',
      label: 'Option1'
    },
    {
      value: '2',
      label: 'Option2'
    },
    {
      value: '3',
      label: 'Option3'
    }
  ]
})
</script>

到此這篇關(guān)于Vue3 如何通過(guò)json配置生成查詢(xún)表單的文章就介紹到這了,更多相關(guān)vue json查詢(xún)表單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

延吉市| 红安县| 左权县| 华宁县| 高雄县| 峡江县| 云南省| 宁蒗| 颍上县| 增城市| 太湖县| 阿克苏市| 屏南县| 商河县| 金寨县| 来安县| 皋兰县| 阿勒泰市| 都安| 长子县| 陇西县| 筠连县| 苏州市| 桐梓县| 南木林县| 雷州市| 乾安县| 潮州市| 都昌县| 新建县| 耿马| 永年县| 西乡县| 建宁县| 田阳县| 呼伦贝尔市| 柳江县| 华坪县| 合水县| 昌江| 曲松县|