效果如圖css
上完整代碼 只須要在本地新建一個index.html 文件 而後將代碼複製進去就能夠看到效果html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <!-- import CSS --> <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" /> </head> <body> <div id="app"> <el-button type="primary" @click="addListItem()" size="mini" >添加</el-button > <el-button type="success" @click="saveList('form')" size="mini">保存</el-button> <el-form :model="model" ref="form" size="small"> <el-table :data="model.list" style="width: 100%;" row-key="key"> <el-table-column v-for="item in tableHeadData" :key="item.key" :label="item.label" :width="item.width" > <template slot-scope="scope"> <el-form-item :prop="'list.' + scope.$index + '.'+item.name" :rules="scope.row.isEdit ? rules[item.name] : {}" > <!-- 判斷是展現列表仍是新增 判斷編輯狀態下是input仍是select --> <span v-if="!scope.row.isEdit && isSelect.indexOf(item.name) < 0">{{ scope.row[item.name] }}</span> <el-input v-model="scope.row[item.name]" v-if="scope.row.isEdit && isSelect.indexOf(item.name) < 0" ></el-input> <el-select v-model="scope.row[item.name]" v-if="isSelect.indexOf(item.name) > -1" :disabled="!scope.row.isEdit" > <el-option v-for="childItem in scope.row[item.name+'List']" :key="childItem.value" :label="childItem.label" :value="childItem.value" > </el-option> </el-select> </el-form-item> </template> </el-table-column> </el-table> <!-- </el-form-item> --> </el-form> </div> </body> <!-- import Vue before Element --> <script src="https://unpkg.com/vue/dist/vue.js"></script> <!-- import JavaScript --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> <script> new Vue({ el: '#app', data: function() { // 能夠把校驗規則單獨提出一個js文件 經過export方式導 // 寫在這裏是爲了此篇博客的展現 var validatePass = (rule, value, callback) => { console.log(rule, value) if (value === '') { callback(new Error('請輸入Warehouse')) } if (value) { // 用寫死value的方式 模擬接口請求 if (value == '345') { callback(new Error('Warehouse已經存在')) } else { callback() } } } return { // list數據 model: { list: [] }, newModel: [], // 表頭字段 tableHeadData: [ { name: 'warehouse', width: 160, label: 'Warehouse' }, { name: 'description', width: 160, label: 'Description' }, { name: 'warehouseType', width: 160, label: 'Warehouse Type' }, { name: 'extWarehouse', width: 160, label: 'Ext Warehouse' }, { name: 'goodsReceiptStep', width: 160, label: 'Goods Receipt Step' }, { name: 'goodsIssueStep', width: 160, label: 'Goods Issue Step' }, { name: 'pickingStrategy', width: 160, label: 'Picking Strategy' }, { name: 'email', width: 210, label: 'Email' }, ], // 下拉選項判斷條件 isSelect: ['warehouseType', 'goodsReceiptStep', 'pickingStrategy'], // 校驗規則 rules: { warehouse: [{ validator: validatePass, trigger: 'blur' }], description: [ { required: true, message: '請輸入description', trigger: 'blur'} ], warehouseType: [ { required: true, message: '請選擇', trigger: 'change' } ], // 非必填可是填了就要複合email的格式 email: [ { type: 'email', message: '請輸入正確的Emall', trigger: 'blur' } ] } } }, mounted() { // 模擬接口請求賦值 this.model.list = [{ warehouse: '345', description: '345', warehouseType: '1', extWarehouse: '1', goodsReceiptStep: '1', goodsIssueStep: '1', pickingStrategy: 'Y', email: 'guozongzhang1@163.com', warehouseTypeList: [ { label: '內部倉庫', value: '1' }, { label: '外部倉庫', value: '2' } ], goodsReceiptStepList: [ { label: '一步', value: '1' }, { label: '二步', value: '2' } ], pickingStrategyList: [ { label: '跳過pick', value: 'Y' }, { label: '不跳過pick', value: 'N' } ] }] }, methods: { // 添加list addListItem() { let addItem = {} this.tableHeadData.forEach(item => { addItem[item.name] = '' addItem['isEdit'] = true addItem.key = Date.now() addItem['warehouseTypeList'] = [ { label: '內部倉庫', value: '1' }, { label: '外部倉庫', value: '2' } ] addItem['goodsReceiptStepList'] = [ { label: '一步', value: '1' }, { label: '二步', value: '2' } ] addItem['pickingStrategyList'] = [ { label: '跳過pick', value: 'Y' }, { label: '不跳過pick', value: 'N' } ] }) this.model.list.unshift(addItem) }, // 保存 saveList(formName) { this.$refs[formName].validate(valid => { if (valid) { console.log('success') } else { console.log('error submit!!') return false } }) } } }) </script> </html>