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

vue3+arco design通過動態(tài)表單方式實現(xiàn)自定義篩選功能

 更新時間:2024年05月10日 10:10:31   作者:linab112  
這篇文章主要介紹了vue3+arco design通過動態(tài)表單方式實現(xiàn)自定義篩選,本文主要實現(xiàn)通過動態(tài)表單的方式實現(xiàn)自定義篩選的功能,用戶可以自己添加篩選的項目,篩選條件及篩選內(nèi)容,需要的朋友可以參考下

1.說明

        (1) 本文主要實現(xiàn)通過動態(tài)表單的方式實現(xiàn)自定義篩選的功能,用戶可以自己添加篩選的項目,篩選條件及篩選內(nèi)容。

        (2) 每個項目的篩選包含篩選項目,篩選條件,篩選方式及篩選值。

                篩選項目代表表中的字段,如姓名,年齡等

                篩選條件包含等于,不等于,大于,小于,包含等

                篩選類型包含手動輸入值的方式,通過下拉進(jìn)行選擇的方式,和表中的其他字段進(jìn)行比較的方式

                篩選值可以手動輸入,可以通過下拉進(jìn)行選擇,也可以選擇某個日期。

2.示例

<template>
  <a-modal :visible="visibleFlag" @ok="handleOk" @cancel="handleCancel" unmountOnClose width="auto">
    <div style="text-align: right">
      <a-button @click="handleAdd()">Add</a-button>
    </div>
    <a-form :model="form" layout='vertical' style="width: 1000px;height: 400px;" ref="formRef">
      <div v-for="(item,index) in form.data">
        <a-row :gutter="10">
          <a-col :span="5">
            <a-form-item :field="`data[${index}].filterColumn`" label="Column" :hide-label="index != 0"
                         :rules="[{required:true,message:'Column不能為空'}]"
                         validate-trigger="blur">
              <a-select v-model="item.filterColumn" :style="{width:'500px'}" placeholder="Please select ..."
                        @blur="columnBlur(form.data[index].filterColumn, form.data[index].filterType , index)" allow-clear>
                <a-option v-for="item of columns" :value="item.value" :label="item.label"/>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item :field="`data[${index}].filterOperation`" label="Operation" :hide-label="index != 0"
                         :rules="[{required:true,message:'Operation不能為空'}]"
                         validate-trigger="blur">
              <a-select v-model="item.filterOperation" :style="{width:'500px'}"
                        placeholder="Please select ..." allow-clear>
                <a-option v-for="item of filterOpeData[index]" :value="item.value" :label="item.label"/>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="5">
            <a-form-item :field="`data[${index}].filterType`" label="Type" :hide-label="index != 0"
                         :rules="[{required:true,message:'Type不能為空'}]"
                         validate-trigger="blur">
              <a-select v-model="item.filterType" :style="{width:'500px'}" placeholder="Please select ..."
                        @blur="typeBlur(form.data[index].filterColumn, form.data[index].filterType, index)" allow-clear>
                <a-option v-for="item of filterTypeData" :value="item.value" :label="item.label"/>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="5">
            <a-form-item :field="`data[${index}].filterValue`" label="Value" :hide-label="index != 0"
                         :rules="[{required:true,message:'Value不能為空'}]"
                         validate-trigger="blur">
              <a-input v-model="item.filterValue" :style="{width:'500px'}"
                       placeholder="Please enter something"
                       allow-clear v-if="valueFlag[index] == 0"/>
              <a-select v-model="item.filterValue" :style="{width:'400px'}" placeholder="Please select ..."
                        v-if="valueFlag[index] == 1" allow-clear>
                <a-option v-for="item of colValList[index]" :value="item.value" :label="item.label"/>
              </a-select>
              <a-date-picker v-model="item.filterValue" :style="{width:'400px'}"
                             v-if="valueFlag[index] == 2" />
            </a-form-item>
          </a-col>
          <a-col :span="3">
            <a-button v-show="index != 0" @click="handleDelete(index)" :style="{marginLeft:'10px'}">Delete</a-button>
          </a-col>
        </a-row>
      </div>
    </a-form>
  </a-modal>
</template>
<script lang="ts" setup>
import {onMounted, ref} from 'vue';
import {Message} from '@arco-design/web-vue';
import {FormInstance} from '@arco-design/web-vue/es/form';
const formRef = ref<FormInstance>();
const visibleFlag = ref(false)
// 0:輸入框;1:下拉選擇器;2:日期
const valueFlag = ref([] as any)
valueFlag.value.push(0)
const form = ref({
  data: [{
    filterColumn: '',
    filterOperation: '',
    filterType: '',
    filterValue: ''
  }]
})
const colValList = ref([] as any)
const filterOpeData = ref([] as any)
const filterTypeData = [{
  value: '0',
  label: 'Value',
}, {
  value: '1',
  label: 'Column',
}, {
  value: '2',
  label: 'User List',
}]
const operationData1 = [
  {value: '1', label: 'EQUAL'},
  {value: '2', label: 'GREATER_THAN'},
  {value: '3', label: 'GREATER_THAN_OR_EQUAL'},
  {value: '4', label: 'LESS_THAN'},
  {value: '5', label: 'LESS_THAN_OR_EQUAL'},
  {value: '6', label: 'NOT_EQUAL'}]
const operationData2 = [
  {value: '1', label: 'EQUAL'},
  {value: '2', label: 'GREATER_THAN'},
  {value: '3', label: 'GREATER_THAN_OR_EQUAL'},
  {value: '4', label: 'LESS_THAN'},
  {value: '5', label: 'LESS_THAN_OR_EQUAL'},
  {value: '6', label: 'NOT_EQUAL'},
  {value: '9', label: 'CONTAINS'},
  {value: '10', label: 'STARTS_WITH'},
  {value: '11', label: 'ENDS_WITH'},
  {value: '12', label: 'DOES_NOT_START_WITH'},
  {value: '13', label: 'DOES_NOT_END_WITH'},
  {value: '14', label: 'DOES_NOT_CONTAIN'},
  {value: '15', label: 'STARTS_OR_ENDS_WITH'}
]
const handleOk = async () => {
  const validRes = await formRef.value?.validate();
  if (!validRes) {
    visibleFlag.value = false;
    console.log(JSON.stringify(form.value.data))
  } else {
    console.log("校驗失敗")
  }
}
const handleCancel = () => {
  visibleFlag.value = false;
}
const handleDelete = (index: any) => {
  form.value.data.splice(index, 1);
  valueFlag.value.splice(index, 1);
  colValList.value.splice(index, 1);
}
const handleAdd = () => {
  form.value.data.push({
    filterColumn: '',
    filterOperation: '',
    filterType: '',
    filterValue: ''
  })
  valueFlag.value.push(0)
}
const columnBlur = (colVal: any, typeVal: any, index: any) => {
  if (!colVal) {
    return
  }
  const col = columns.value.find((item: { value: any; }) => item.value == colVal)
  if (col.type == 'text' || col.type == 'number') {
    filterOpeData.value[index] = operationData2
  } else {
    filterOpeData.value[index] = operationData1
  }
  if (col.type == 'list' && typeVal != '1') {
    valueFlag.value[index] = 1
    colValList.value[index] = [{
      value: '1',
      label: '處理提交'
    }, {
      value: '2',
      label: '處理中'
    }, {
      value: '3',
      label: '處理完成'
    }]
  }
  if (col.type == 'date') {
    valueFlag.value[index] = 2
  }
}
const typeBlur = (colVal: any, typeVal: any, index: any) => {
  if (typeVal == '1') {
    if (!colVal) {
      Message.error({content: 'column信息不能為空', position: 'top'});
      return
    } else {
      const col = columns.value.find((item: { value: any; }) => item.value == colVal)
      if (col.type == 'list') {
        Message.error({content: 'list類型的column不支持和其他列進(jìn)行比較', position: 'top'});
        form.value.data[index].filterType = ''
        return
      }
      valueFlag.value[index] = 1
      if (col.type == 'date') {
        colValList.value[index] = columns.value.filter((item: { type: string; }) => item.type == 'date')
      } else if (col.type == 'text') {
        colValList.value[index] = columns.value.filter((item: { type: string; }) => item.type == 'text' || item.type == 'number')
      } else if (col.type == 'number') {
        colValList.value[index] = columns.value.filter((item: { type: string; }) => item.type == 'number')
      } else if (col.type == 'list') {
      } else {
        colValList.value[index] = []
      }
    }
  }
}
// 打開彈窗
const openDialog = () => {
  visibleFlag.value = true;
};
// 導(dǎo)出方法在父組件中進(jìn)行使用
defineExpose({openDialog});
const columns = ref()
const getColumnList = () => {
  columns.value = [{
    value: '12',
    label: 'genoId',
    type: 'text'
  },
    {
      value: '13',
      label: 'status',
      type: 'list'
    },
    {
      value: '14',
      label: 'createTime',
      type: 'date'
    },
    {
      value: '15',
      label: 'qty',
      type: 'number'
    }
  ]
}
// 組件完成初始渲染并創(chuàng)建 DOM 節(jié)點后運行
onMounted(async () => {
  await getColumnList()
})
</script>
<script lang="ts">
export default {
  name: "dynamicFilter"
}
</script>
<style scoped>
</style>

 說明:

        ①通過表單的方式進(jìn)行實現(xiàn),表單關(guān)聯(lián)的數(shù)據(jù)源form是對象格式,如下:

const form = ref({
  data: [{
    filterColumn: '',
    filterOperation: '',
    filterType: '',
    filterValue: ''
  }]
})

       對象內(nèi)是一個id為data的數(shù)組,數(shù)組內(nèi)的每條數(shù)組則存儲一個項目的篩選內(nèi)容,包含篩選項目,篩選條件,篩選方式及篩選值。數(shù)據(jù)源設(shè)置為對象是為了進(jìn)行校驗處理,如果是數(shù)組格式,無法實現(xiàn)動態(tài)表單的的校驗(不同的前端框架,可能會有所不同,但推薦使用對象格式)

        ②動態(tài)表單的實現(xiàn)
        在form標(biāo)簽內(nèi)嵌套div標(biāo)簽,并在div標(biāo)簽上進(jìn)行循環(huán)form.data,如果動態(tài)的項目只有一個,可以直接在form-item上進(jìn)行循環(huán)處理。具體的每個表單項可以通過form.data[index].表單項名的方式進(jìn)行綁定,也可以通過item.表單項名的方式進(jìn)行綁定。點擊新增按鈕,會向form.data中push一條新數(shù)據(jù),添加的篩選項目后面有刪除按鈕,點擊刪除按鈕,通過form.data.splice方法刪除數(shù)組中的元素。

        ③動態(tài)表單的校驗處理

        在每個表單項中添加校驗規(guī)則,不要在form上添加校驗規(guī)則

        在arco design框架中的動態(tài)表單校驗如下:

            <a-form-item :field="`data[${index}].filterColumn`" label="Column" :hide-label="index != 0"
                         :rules="[{required:true,message:'Column不能為空'}]"
                         validate-trigger="blur">
              <a-select v-model="item.filterColumn" :style="{width:'500px'}" placeholder="Please select ..."
                        @blur="columnBlur(form.data[index].filterColumn, form.data[index].filterType , index)" allow-clear>
                <a-option v-for="item of columns" :value="item.value" :label="item.label"/>
              </a-select>
            </a-form-item>

 通過rules關(guān)聯(lián)校驗規(guī)則,注意field屬性的設(shè)置,在arco design中field需要進(jìn)行如上設(shè)置,結(jié)果例如:data[0].filterColumn。

在arco design中是設(shè)置field屬性,在element ui中是設(shè)置prop屬性,并且內(nèi)容的設(shè)置方式也不一樣,需要注意。

        ④提交時的校驗

        當(dāng)前表單嵌套在對話框中,點擊確定按鈕時,需要先進(jìn)行表單的校驗處理,如下:

const handleOk = async () => {
  const validRes = await formRef.value?.validate();
  if (!validRes) {
    visibleFlag.value = false;
    console.log(JSON.stringify(form.value.data))
  } else {
    console.log("校驗失敗")
  }
}

通過調(diào)用表單的ref對象的validate方法,如果校驗失敗則返回失敗的項目的field屬性內(nèi)容及錯誤信息,如果成功則返回undefined,所以通過判斷返回結(jié)果就可以判斷校驗成功與否。 

校驗失敗的返回信息如下

{
	"data[0].filterColumn": {
		"label": "Column",
		"field": "data[0].filterColumn",
		"type": "string",
		"isRequiredError": true,
		"message": "Column不能為空"
	},
	"data[0].filterOperation": {
		"label": "Operation",
		"field": "data[0].filterOperation",
		"type": "string",
		"isRequiredError": true,
		"message": "Operation不能為空"
	},
	"data[0].filterType": {
		"label": "Type",
		"field": "data[0].filterType",
		"type": "string",
		"isRequiredError": true,
		"message": "Type不能為空"
	},
	"data[0].filterValue": {
		"label": "Value",
		"field": "data[0].filterValue",
		"type": "string",
		"isRequiredError": true,
		"message": "Value不能為空"
	},
	"data[1].filterColumn": {
		"label": "Column",
		"field": "data[1].filterColumn",
		"type": "string",
		"isRequiredError": true,
		"message": "Column不能為空"
	},
	"data[1].filterOperation": {
		"label": "Operation",
		"field": "data[1].filterOperation",
		"type": "string",
		"isRequiredError": true,
		"message": "Operation不能為空"
	},
	"data[1].filterType": {
		"label": "Type",
		"field": "data[1].filterType",
		"type": "string",
		"isRequiredError": true,
		"message": "Type不能為空"
	},
	"data[1].filterValue": {
		"label": "Value",
		"field": "data[1].filterValue",
		"type": "string",
		"isRequiredError": true,
		"message": "Value不能為空"
	},
	"data[2].filterColumn": {
		"label": "Column",
		"field": "data[2].filterColumn",
		"type": "string",
		"isRequiredError": true,
		"message": "Column不能為空"
	},
	"data[2].filterOperation": {
		"label": "Operation",
		"field": "data[2].filterOperation",
		"type": "string",
		"isRequiredError": true,
		"message": "Operation不能為空"
	},
	"data[2].filterType": {
		"label": "Type",
		"field": "data[2].filterType",
		"type": "string",
		"isRequiredError": true,
		"message": "Type不能為空"
	},
	"data[2].filterValue": {
		"label": "Value",
		"field": "data[2].filterValue",
		"type": "string",
		"isRequiredError": true,
		"message": "Value不能為空"
	}
}

校驗通過后輸出的form.data信息,如下:

[{
	"filterColumn": "12",
	"filterOperation": "1",
	"filterType": "0",
	"filterValue": "23"
}, {
	"filterColumn": "13",
	"filterOperation": "1",
	"filterType": "0",
	"filterValue": "1"
}, {
	"filterColumn": "14",
	"filterOperation": "2",
	"filterType": "0",
	"filterValue": "2024-05-14"
}]

3.運行截圖

校驗失敗

輸入內(nèi)容后,校驗通過

 4.總結(jié)

①注意表單綁定的數(shù)據(jù)源的格式及不同前端框架的校驗的方式

②為了使畫面更加友好,可以將表單放在表格中

到此這篇關(guān)于vue3+arco design通過動態(tài)表單方式實現(xiàn)自定義篩選的文章就介紹到這了,更多相關(guān)vue3自定義篩選內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue路由跳轉(zhuǎn)的5種方式及擴展

    Vue路由跳轉(zhuǎn)的5種方式及擴展

    這篇文章主要給大家介紹了關(guān)于Vue路由跳轉(zhuǎn)的5種方式及擴展,在Vue中路由是一種用于導(dǎo)航和管理頁面之間跳轉(zhuǎn)的機制,Vue Router是Vue官方提供的路由管理器,需要的朋友可以參考下
    2023-11-11
  • vue引用public文件夾中文件的多種方式

    vue引用public文件夾中文件的多種方式

    由于一些演示需要對一些簡單頁面進(jìn)行配置,由于打包build后的vue項目基本已經(jīng)看不出原樣,因此需要創(chuàng)建一個文件,并在打包的時候不會進(jìn)行編譯,所以文件放在public,這篇文章主要給大家介紹了關(guān)于vue引用public文件夾中文件的多種方式,需要的朋友可以參考下
    2024-02-02
  • vue實現(xiàn)路由懶加載的3種方法示例

    vue實現(xiàn)路由懶加載的3種方法示例

    這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)路由懶加載的3種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 解析Vue2 dist 目錄下各個文件的區(qū)別

    解析Vue2 dist 目錄下各個文件的區(qū)別

    本篇文章主要介紹了解析Vue2 dist 目錄下各個文件的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • vue3.0如何使用computed來獲取vuex里數(shù)據(jù)

    vue3.0如何使用computed來獲取vuex里數(shù)據(jù)

    這篇文章主要介紹了vue3.0如何使用computed來獲取vuex里數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue使用file-saver插件保存各種格式文件方式

    vue使用file-saver插件保存各種格式文件方式

    這篇文章主要介紹了vue使用file-saver插件保存各種格式文件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • vue點擊當(dāng)前路由高亮小案例

    vue點擊當(dāng)前路由高亮小案例

    這篇文章主要為大家詳細(xì)介紹了vue點擊當(dāng)前路由高亮小案例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • vuex Module將 store 分割成模塊的操作

    vuex Module將 store 分割成模塊的操作

    這篇文章主要介紹了vuex Module將 store 分割成模塊,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • vue-cli3 打包優(yōu)化之 splitchunks詳解

    vue-cli3 打包優(yōu)化之 splitchunks詳解

    這篇文章主要介紹了vue-cli3 打包優(yōu)化之 splitchunks的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • 在vue中實現(xiàn)PDF文件流預(yù)覽功能

    在vue中實現(xiàn)PDF文件流預(yù)覽功能

    這篇文章主要為大家詳細(xì)介紹如何在vue中實現(xiàn)PDF文件流預(yù)覽功能,文中的實現(xiàn)步驟講解詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考價值,需要的可以參考一下
    2023-12-12

最新評論

浏阳市| 紫金县| 蓝田县| 合作市| 盐山县| 云和县| 临沂市| 科技| 翁牛特旗| 文登市| 通海县| 广河县| 油尖旺区| 北安市| 固安县| 龙川县| 灌阳县| 连南| 伊通| 庄浪县| 临安市| 台安县| 太保市| 凌海市| 府谷县| 徐水县| 麻阳| 略阳县| 马公市| 洛隆县| 高陵县| 海南省| 高州市| 克什克腾旗| 禹州市| 多伦县| 西充县| 厦门市| 三都| 修水县| 平山县|