溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Vue怎么動態(tài)生成數(shù)據(jù)字段

發(fā)布時間:2022-04-06 11:00:10 來源:億速云 閱讀:306 作者:iii 欄目:開發(fā)技術

本篇內(nèi)容主要講解“Vue怎么動態(tài)生成數(shù)據(jù)字段”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Vue怎么動態(tài)生成數(shù)據(jù)字段”吧!

    動態(tài)生成數(shù)據(jù)字段實例

    1.父組件定義data里面的數(shù)據(jù)字段

    異步請求獲取數(shù)據(jù)(一種配置數(shù)據(jù),一種實際數(shù)據(jù))

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

    2.子組件接收數(shù)據(jù)

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

    3.因為獲取數(shù)據(jù)是異步操作

    因此需要監(jiān)聽數(shù)據(jù)變化,當有數(shù)據(jù)時展示子組件

    configLoaded: false,
    listLoaded: false

    定義上面兩個變量來判斷數(shù)據(jù)是否加載完成,在異步獲取完數(shù)據(jù)后賦值為true,并監(jiān)聽

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

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

    并執(zhí)行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>

    表單動態(tài)生成字段  

    checkbox 多選,沒有默認值的時候,一定要給一個空數(shù)組,不然就展示不出來

    <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="請輸入內(nèi)容"></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="請輸入內(nèi)容"
            >
            </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>

    到此,相信大家對“Vue怎么動態(tài)生成數(shù)據(jù)字段”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

    vue
    AI