首頁 > 軟體

Vue 動態生成資料欄位的範例

2022-04-02 19:00:16

動態生成資料欄位範例

1.父元件定義data裡面的資料欄位

非同步請求獲取資料(一種設定資料,一種實際資料)

data () {
  return {
    config: [],
    list: []
  };
}

2.子元件接收資料

props: {
  config: Array,
  list: Array
},
data () {
  return {
    newConfig: [],
    configLength: 0,
    newList: []
  };
}

3.因為獲取資料是非同步操作

因此需要監聽資料變化,當有資料時展示子元件

configLoaded: false,
listLoaded: false

定義上面兩個變數來判斷資料是否載入完成,在非同步獲取完資料後賦值為true,並監聽

watch: {
  configLoaded (n, o) {
        this.configLoaded = n;
  },
  listLoaded (n, o) {
    this.listLoaded = n;
  }
},

4.計算屬性計算兩個變數是否均完成

並執行v-if

computed: {
  showItem () {
    return this.configLoaded && this.listLoaded;
  }
},
<list-item :config="config" :list="list" v-if="showItem"></list-item>

5.子元件完整程式碼

<template>
  <div>
    <div class="item iconfont border-bottom" v-for="(item,index) of newList" :key="index">
      <p v-for="(m,i) of item" :key="i">{{m.name}} {{m.text}}</p>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'Item',
    props: {
      config: Array,
      list: Array
    },
    data () {
      return {
        newConfig: [],
        configLength: 0,
        newList: []
      };
    },
    mounted () {
      this.toConfig();
    },
    methods: {
      toConfig () {
        this.configLength = this.config.length;
        for (let i in this.config) {
          let configItem = this.config[i];
          this.newConfig.push({name: configItem.fieldName, text: configItem.fieldDesc});
        }
        for (let l in this.list) {
          let item = this.list[l];
          let childList = [];
          for (var c in this.newConfig) {
            let config = this.newConfig[c];
            for (let key in item) {
              if (config.name === key) {
                childList.push({name: config.text, text: item[key]});
              }
            }
          }
          this.newList.push(childList);
        }
      }
    }
  };
</script>
 
<style lang="stylus" ref="stylesheet/stylus">
</style>

表單動態生成欄位  

checkbox 多選,沒有預設值的時候,一定要給一個空陣列,不然就展示不出來

<el-form ref="myForm" :model="form" :rules="rules" :disabled="disabledBoolean">
 <el-row>
<el-col>
  <div v-for="(item ,index) in extendFieldColumns" :key="index" >
    <el-col v-if="item.type === 'input'" :span="defaultSpan">
      <el-form-item  :label-width="defaultLabelWidth" size="small" :rules="extendFieldRules[item.prop]" :prop="'extendField.'+item.prop" :label="item.label+':'" >
        <el-input  v-model="form.extendField[item.prop]" :maxlength="item.maxlength" placeholder="請輸入內容"></el-input>
      </el-form-item>
    </el-col>
    <el-col v-if="item.type === 'radio'"  :span="defaultSpan">
      <el-form-item  :label-width="defaultLabelWidth" size="small" :rules="extendFieldRules[item.prop]" :prop="'extendField.'+item.prop" :label="item.label+':'"  >
        <el-radio-group v-model="form.extendField[item.prop]" >
          <el-radio v-for="(option,index1) in item.dicData"  :key="index1" :label="option.label"  >{{option.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-col>
    <el-col v-if="item.type === 'select'" :span="defaultSpan" >
      <el-form-item  :label-width="defaultLabelWidth" size="small" :prop="'extendField.'+item.prop"  :label="item.label+':'"  :rules="extendFieldRules[item.prop]">
        <el-select  v-model="form.extendField[item.prop]"  placeholder="請選擇">
          <el-option
v-for="(option,index2) in item.dicData"
:key="index2"
:label="option.label"
:value="option.label">
          </el-option>
        </el-select>
      </el-form-item>
    </el-col>
    <el-col v-if="item.type === 'checkbox'" :span="defaultSpan" >
      <el-form-item  :label-width="defaultLabelWidth" size="small" :prop="'extendField.'+item.prop"  :label="item.label+':'" :rules="extendFieldRules[item.prop]">
        <el-checkbox-group v-model="form.extendField[item.prop]"  >
          <el-checkbox v-for="city in item.dicData"  :label="city.label" :key="city.label">{{city.label}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-col>
    <el-col v-if="item.type === 'number'" :span="defaultSpan" >
      <el-form-item  :label-width="defaultLabelWidth" size="small" :prop="'extendField.'+item.prop" :label="item.label+':'" :rules="extendFieldRules[item.prop]">
        <el-input-number v-model="form.extendField[item.prop]"  :max="item.maxlength"  ></el-input-number>
      </el-form-item>
    </el-col>
    <el-col v-if="item.type === 'textarea'" :span="defaultSpan" >
      <el-form-item  :label-width="defaultLabelWidth" size="small" :prop="'extendField.'+item.prop" :label="item.label+':'" :rules="extendFieldRules[item.prop]">
        <el-input
          v-model="form.extendField[item.prop]"
          :maxlength="item.maxlength"
          type="textarea"
          placeholder="請輸入內容"
        >
        </el-input>
      </el-form-item>
    </el-col>
    <el-col v-if="item.type === 'date'" :span="defaultSpan" >
      <el-form-item  :label-width="defaultLabelWidth" size="small" :prop="'extendField.'+item.prop" :label="item.label+':'" :rules="extendFieldRules[item.prop]">
        <el-date-picker
          v-model="form.extendField[item.prop]"
          type="date"
          placeholder="選擇日期">
        </el-date-picker>
      </el-form-item>
    </el-col>
  </div>
</el-col>
</el-row>
</el-form>

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。 


IT145.com E-mail:sddin#qq.com