欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3 如何通過json配置生成查詢表單

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

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

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

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

使用規(guī)則
1. 組件支持使用v-model管理值,當(dāng)v-model不配置時也可以通過@finish事件獲取表單值
2. 當(dāng)FormRender組件設(shè)置v-model后,schema配置項(xiàng)中defaultValue設(shè)置的默認(rèn)值無效
3. 項(xiàng)目默認(rèn)查詢和重置事件,也支持插槽自定義事件
4. 通過插槽自定義事件時,可以通過插槽獲取表單值,也可以通過組件暴露屬性和方法獲取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)識 & 表單項(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)查詢和重置功能,支持自定義 -->
    <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: '查詢',
    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. 簡單渲染和取值:未使用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 }[]>([])
// 默寫數(shù)據(jù)需要通過網(wǎng)絡(luò)請求獲取,所有需要使用到 computed
const schema = computed<SchemaItem[]>(() => [
  {
    key: 'content',
    label: '名稱',
    type: 'input',
    defaultValue: '張三',
    props: {
      placeholder: '請輸入名稱',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'orderNo',
    label: '訂單號',
    defaultValue: '20250012',
    component: ElInput,
    props: {
      placeholder: '請輸入訂單號',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'state',
    label: '狀態(tài)',
    type: 'select',
    props: {
      placeholder: '請選擇狀態(tài)',
      clearable: true,
      options: options.value,
      style: {
        width: '200px'
      }
    }
  }
])
function handleSubmit(value: ObjAny) {
  console.log(value)
}
onMounted(() => {
 // 模擬網(wǎng)絡(luò)請求數(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 }[]>([])
// 默寫數(shù)據(jù)需要通過網(wǎng)絡(luò)請求獲取,所有需要使用到 computed
// 當(dāng)使用v-model時, defaultValue值將失效
const schema = computed<SchemaItem[]>(() => [
  {
    key: 'content',
    label: '名稱',
    type: 'input',
    props: {
      placeholder: '請輸入名稱',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'orderNo',
    label: '訂單號',
    component: ElInput,
    props: {
      placeholder: '請輸入訂單號',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'state',
    label: '狀態(tài)',
    type: 'select',
    props: {
      placeholder: '請選擇狀態(tài)',
      clearable: true,
      options: options.value,
      style: {
        width: '200px'
      }
    }
  }
])
function handleSubmit(value: ObjAny) {
  console.log(value)
}
onMounted(() => {
 // 模擬網(wǎng)絡(luò)請求數(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)">查詢</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 }[]>([])
// 默寫數(shù)據(jù)需要通過網(wǎng)絡(luò)請求獲取,所有需要使用到 computed
// 當(dāng)使用v-model時, defaultValue值將失效
const schema = computed<SchemaItem[]>(() => [
  {
    key: 'content',
    label: '名稱',
    type: 'input',
    props: {
      placeholder: '請輸入名稱',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'orderNo',
    label: '訂單號',
    component: ElInput,
    props: {
      placeholder: '請輸入訂單號',
      clearable: true,
      style: {
        width: '200px'
      }
    }
  },
  {
    key: 'state',
    label: '狀態(tài)',
    type: 'select',
    props: {
      placeholder: '請選擇狀態(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ò)請求數(shù)據(jù)
  options.value = [
    {
      value: '1',
      label: 'Option1'
    },
    {
      value: '2',
      label: 'Option2'
    },
    {
      value: '3',
      label: 'Option3'
    }
  ]
})
</script>

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

相關(guān)文章

  • vue項(xiàng)目怎樣用nginx反向代理WebSocket請求地址

    vue項(xiàng)目怎樣用nginx反向代理WebSocket請求地址

    這篇文章主要介紹了vue項(xiàng)目怎樣用nginx反向代理WebSocket請求地址問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • vue中對象數(shù)組去重的實(shí)現(xiàn)

    vue中對象數(shù)組去重的實(shí)現(xiàn)

    這篇文章主要介紹了vue中對象數(shù)組去重的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 詳解vue3+quasar彈窗的幾種方式

    詳解vue3+quasar彈窗的幾種方式

    本文主要介紹了vue3+quasar彈窗的幾種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • vue2如何使用vue-i18n搭建多語言切換環(huán)境

    vue2如何使用vue-i18n搭建多語言切換環(huán)境

    這篇文章主要介紹了vue2-使用vue-i18n搭建多語言切換環(huán)境的相關(guān)知識,在data(){}中獲取的變量存在更新this.$i18n.locale的值時無法自動切換的問題,需要刷新頁面才能切換語言,感興趣的朋友一起看看吧
    2023-12-12
  • 如何處理?Vue3?中隱藏元素刷新閃爍問題

    如何處理?Vue3?中隱藏元素刷新閃爍問題

    本文主要探討了網(wǎng)頁刷新時,原本隱藏的元素會一閃而過的問題,以及嘗試過的解決方案并未奏效,通過實(shí)例代碼介紹了如何處理?Vue3?中隱藏元素刷新閃爍問題,感興趣的朋友跟隨小編一起看看吧
    2024-10-10
  • mpvue小程序循環(huán)動畫開啟暫停的實(shí)現(xiàn)方法

    mpvue小程序循環(huán)動畫開啟暫停的實(shí)現(xiàn)方法

    這篇文章主要介紹了mpvue小程序循環(huán)動畫開啟暫停的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Vue2 配置 Axios api 接口調(diào)用文件的方法

    Vue2 配置 Axios api 接口調(diào)用文件的方法

    本篇文章主要介紹了Vue2 配置 Axios api 接口調(diào)用文件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • vue項(xiàng)目多租戶環(huán)境變量的設(shè)置

    vue項(xiàng)目多租戶環(huán)境變量的設(shè)置

    本文主要介紹了vue項(xiàng)目多租戶環(huán)境變量的設(shè)置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Vue.js集成Word實(shí)現(xiàn)在線編輯功能

    Vue.js集成Word實(shí)現(xiàn)在線編輯功能

    在現(xiàn)代Web應(yīng)用中,集成文檔編輯功能變得越來越常見,特別是在協(xié)作環(huán)境中,能夠直接在Web應(yīng)用內(nèi)編輯Word文檔可以極大地提高工作效率,本文將詳細(xì)介紹如何在Vue.js項(xiàng)目中集成Word在線編輯功能,需要的朋友可以參考下
    2024-08-08
  • 前端登錄退出處理Token問題(獲取、緩存、失效處理)及代碼實(shí)現(xiàn)方法

    前端登錄退出處理Token問題(獲取、緩存、失效處理)及代碼實(shí)現(xiàn)方法

    token是一個用戶信息的表示,在登錄中將會從后端拿到token,然后用戶才可以進(jìn)行往后的一系列操作,這篇文章主要給大家介紹了關(guān)于前端登錄退出處理Token問題(獲取、緩存、失效處理)及代碼實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2024-01-01

最新評論