vue + element 表单的简单公用组件,表格的集成写法

表单的公用组件,mForm.vue

<template>
  <el-form ref="form" :inline="inline" :model="dataObject" :label-width="labelWidth">
    <el-form-item v-for="(item,index) in formInput" :key="index" :label="item.label">
      <template v-if="item.type==='textarea'">
        <el-input type="textarea" v-model="dataObject[item.code]" :style="'width:'+inputWidth+'px'"></el-input>
      </template>
      <template v-else-if="item.type==='radio'">
        <el-radio-group v-model="dataObject[item.code]">
          <el-radio v-for="(n,i) in item.list" :key="i" :value="n.value" :label="n.label"></el-radio>
        </el-radio-group>
      </template>
      <template v-else-if="item.type==='checkbox'">
        <el-checkbox-group v-model="dataObject[item.code]">
          <el-checkbox v-for="(n,i) in item.list" :key="i" :value="n.value" :label="n.label"></el-checkbox>
        </el-checkbox-group>
      </template>
      <template v-else-if="item.type==='select'">
        <el-select v-model="dataObject[item.code]" filterable placeholder="请选择">
          <el-option v-for="n in item.list" :key="n.value" :label="n.label" :value="n.value">
          </el-option>
        </el-select>
      </template>
      <template v-else>
        <el-input size="mini" v-model="dataObject[item.code]" :style="'width:'+inputWidth+'px'"></el-input>
      </template>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  data() {
    return {
      dataObject: {}
    }
  },
  props: {
    inline: { type: Boolean, default: false },
    labelWidth: { type: String, default: '80px' },
    inputWidth: { type: String, default: '100px' },
    initData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    formInput: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  created() {
    this.dataObject = Object.assign({}, this.initData)
  },
  methods: {
    getVlue() {
      return this.dataObject
    }
  }
}
</script>

应用

<m-form :inline="inline" :labelWidth="'140px'" :inputWidth="'200'" :formInput="forms" ref="formGroup" />

表格

        <el-table border :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
          <el-table-column v-for="t in thead" :key="t.id" :prop="t.prop" :label="t.label" :width="t.width"></el-table-column>
        </el-table>

data

      thead: [
        { prop: 'xm', label: '姓名' },
        { prop: 'dq', label: '地区' },
        { prop: 'dh', label: '电话' }
      ],

posted @ 2021-07-29 16:11  wwj007  阅读(158)  评论(0编辑  收藏  举报
……