vue+element custom query component

vue+element custom query component

This article mainly introduces the Vue project. On the premise of introducing element, the components are encapsulated twice to achieve direct layout through configuration items.

1. Query Condition Componentization

Combined with the use of EventBus.js , event delivery is more efficient and various complex life cycle dependencies can be avoided.
In the components/dataForm folder, dataForm.vue is used as the carrier of the component collection and configuration items are created

1.1 dataForm code example

// dataForm.vue
<template>
  <el-row>
    <input-form v-for="(item,index) in options" :key="'dataForm_'+index"> 
      <!-- Input box-->
      <input-form
        :key="index"
        v-if="item.type == 'input'"
        :data='item'
      ></input-form>
  </el-row>
</template>
<script>
import EventBus from "@/assets/js/eventBus.js"
import InputForm "@/components/dataForm/InputForm"
export default {
  components:
    InputForm,
  },
  props: {
    /**
     * Form configuration items * @param {Object} option Configuration parameters, as follows:
     * type: form item type, String, optional value input
     */
    options:
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
   return {}
  },
  created() {
   // This is mainly to realize the display and hiding of other subcomponents in the drop-down box EventBus.$on("refreshDataForm", e => {
      this.refreshDataForm(e);
    });
  },
  // Page destruction event destruction beforeDestroy() {
    EventBus.$off("refreshDataForm")
    EventBus.$off("handleClick")
  },
  methods: {
   // Update form data refreshDataForm(item) {
      let data = this.options
      data.forEach((e, i) => {
        if (item.type == e.type && item.name == e.name) {
          this.options[i] = item
        }
      })
    },
    // The child component click event responds to the parent component, and the data is transferred handleClick(data) {
     EventBus.$emit("handleClick",data)
    },
    //Format the form data (you can check if the required items are required)
    getDataForm() {
     let data = this.options
     let formObj = {}
     let error = ''
     try {
      data.forEach(e => {
       if (e.type === ''input) {
        if (e.require && !e.content) {
         error = 'Please enter' + e.label
         throw error
        }
        formObj[e.name] = e.content
       } else if (e.type === 'select' || e.type === 'dataSelect') {
        if (e.require && !e.content) {
         error = 'Please select' + e.label
         throw error
        }
        formObj[e.name] = e.content
       } else if (e.type === 'date' || e.type === 'dataRadio') {
        if (e.require && !e.content) {
         error = 'Please select' + e.label
         throw error
        }
        formObj[e.beginName] = e.beginRegTime
        formObj[e.endName] = e.endRegTime
       } else if (e.type === 'image') {
        formObj[e.name] = e.file || e.content
       } else if (e.type === 'upload') {
        formObj[e.name] = e.file || e.content
        if (e.delFileName) { // Delete the attachment collection and custom name and default name formObj[e.delFileName] = e.delFileIds.join(',')
        } else {
         formObj['delFileName'] = e.delFileIds.join(',')
        }
       }
      })
      return formObj
     } catch (error) {
      this.$message({ message:error, type: 'error'})
     }
    }
  }
}

1.2 Subgroup inputForm.vue

Note: The components referenced here can also be referenced individually by the page

<template>
  <el-col>
    <el-col :span="data.boxSpan?data.boxSpan:boxSpan" v-if="!data.isHide">
     <el-col :span="data.infoSpan?data.infoSpan:infoSpan" >
      <el-col :span="data.infoSpan?data.infoSpan:infoSpan" v-if="data.labelSpan!=0">
       <label class="el-form-item_label" :class="{'require': data.require}" v-html="data.label"></label>
      </el-col>
      <el-col :span="data.contentSpan?data.contentSpan:contentSpan" v-if="data.contentSpan!=0">
       <el-input :class="{'base_textarea': data.textarea}" v-modle.trim="data.content" :type="data.textarea?'textarea':''" :disable="data.readOnly" :placeholder="setPlaceholder" maxlength="200"></el-input>
      </el-col>
     </el-col>
      <span v-text="title"></span>
    </div>
  </el-col>
</template>
<script>
export default {
 props: {
  // Type input input box type: {
   type: String,
   default: 'input'
  },
  //Default fence layout 8/24
  boxSpan: {
   type: Number,
   default: 8
  },
  // Default fence layout 24/24
  infoSpan: {
   type: Number,
   default: 24
  },
  //Default fence layout 8/24
  span: {
   type: Number,
   default: 8
  },
  //Default fence layout 16/24
  contentSpan: {
   type: Number,
   default: 16
  },
  /**
  * name keyword * type form type * label form title * content form content * readOnly read-only default no * isHide hidden default no * textarea text type default no **/
  data: {
   type: Object,
   default() {
    return []
   }
  }
 },
 computed: {
  setPlaceholder() {
   if(this.data.readOnly && !this.data.content) {
    return ''
   } 
   return 'Please enter'
  }
 }
}
</script>
<style scoped>
 // Required style.require::after {
  content: '*';
  color:red;
 }
 // flex layout title vertically centered.el-form-item__label {
  float:right;
  margin-botton:0;
  line-height: 20px;
  display: flex;
  align-items: center;
  min-height: 40px;
 }
</style>

1.3 Parent page reference and use

<template>
  <el-row>
    <data-form :options='searchArray' ref='searchArray'></input-form>
  </el-row>
</template>

<script>
 import EventBus from "@/assets/js/eventBus.js"
 import DataForm "@/components/dataForm/dataForm"
 export default {
  components:
   DataForm
  },
  data() {
   return {
    // Query menu configuration searchArray: [
     {
      name: 'personName',
      type: 'input',
      label: 'Username',
      content: ''
     },
     {
      name: 'personName2',
      type: 'input',
      label: 'Username 2',
      content: ''
     }
    ]
   }
  },
  created() {
   // Listen for child component parameters EventBus.$on('refreshDataForm', e => {
    this.refreshDataForm(e)
   })
  },
  destroyed() {
   // Destroy the subcomponent parameter listening EventBus.$off('refreshDataForm')
  },
  methods: {
   // Listen for subcomponent operations refreshDataForm(e) {
    //Logic code this.$forceUpdate()
   },
   //Data query handleQuery() {
     // Get component parameters and return to json format let searchArray = this.$refs.searchArray.getDataForm()
    // If there are required items, the return value is null, and a pop-up window will appear if (!searchArray) {
     return 
    }
    //Query interface logic}
  }
 }
</script>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue implements dynamic query rule generation component
  • Detailed explanation of using Vue component to query data by time period
  • Sample code for fuzzy query of Vue input box
  • Vue implements the fuzzy query method of Input input box
  • Vue.js implements paging query function
  • Vue implements the function of clicking on time to obtain time period query
  • Detailed example of query operation in Vue.js
  • Using Vue.js framework to implement train ticket query system (with source code)
  • Implementing paging query function based on vue.js
  • Example code for implementing paging query using Bootstrap4 + Vue2

<<:  Detailed explanation of MySQL 8.0 password expiration policy

>>:  Detailed explanation of how to configure the tomcat external server in HBuilderX to view and edit the jsp interface

Recommend

The functions and differences between disabled and readonly

1: readonly is to lock this control so that it can...

Native JS realizes the special effect of spreading love by mouse sliding

This article shares with you a js special effect ...

Method for realizing Internet interconnection by VMware virtual machine bridging

After installing VMware and creating a new virtua...

Six ways to reduce the size of Docker images

Since I started working on Vulhub in 2017, I have...

New usage of watch and watchEffect in Vue 3

Table of contents 1. New usage of watch 1.1. Watc...

How to start a Vue.js project

Table of contents 1. Node.js and Vue 2. Run the f...

Detailed explanation of Mysql logical architecture

1. Overall architecture diagram Compared to other...

CSS float (float, clear) popular explanation and experience sharing

I came into contact with CSS a long time ago, but...

Sample code for separating the front-end and back-end using FastApi+Vue+LayUI

Table of contents Preface Project Design rear end...

MySQL 5.7.18 installation tutorial under Windows

This article explains how to install MySQL from a...

Detailed steps to install nginx on Apple M1 chip and deploy vue project

brew install nginx Apple Mac uses brew to install...

Web design skills: iframe adaptive height problem

Maybe some people have not come across this issue ...

Implementation of importing and exporting vue-element-admin projects

vue-element-admin import component encapsulation ...

How to deal with too many Docker logs causing the disk to fill up

I have a server with multiple docker containers d...