element table下input v-model绑定数据并动态新增

本文介绍了一种设备阈值配置与报警管理系统的设计与实现,涵盖了阈值设定、颜色编码、报警内容输入及操作功能。系统采用Vue框架,通过表格形式展示设备阈值列表,支持动态增删行,确保数据的准确性和实时性。
<el-button type="primary" icon="el-icon-plus" @click="addRule">新增</el-button>
<el-form :rules="formData.rules" :model="formData" ref="formDom" class="demo-ruleForm">
              <el-table :data="formData.deviceThresholdList">
                <!-- v-loading="loading" -->
                <el-table-column label="序号" align="center" prop="index">
                  <template slot-scope="scope">
                    <span>{{scope.$index+1}}</span>
                  </template>
                </el-table-column>
                <el-table-column label="阈值" align="center" prop="thresholdStatus">
                  <template slot-scope="scope">
                    <el-form-item :prop="'deviceThresholdList.' + scope.$index + '.thresholdStatus'" :rules='formData.rules.thresholdStatus'>
                      <el-select v-model="scope.row.thresholdStatus">
                        <el-option v-for="item in thresholdOptions" :key="item.dictValue" :label="item.dictLabel"
                          :value="item.dictValue"></el-option>
                      </el-select>
                    </el-form-item>

                  </template>
                </el-table-column>
                <el-table-column label="Value" align="center" prop="vals">
                  <template slot-scope="scope">
                    <el-form-item :prop="'deviceThresholdList.' + scope.$index + '.thresholdStatus'" :rules='formData.rules.vals'>
                      <el-input v-model="scope.row.vals" placeholder="请输入阈值" size="medium"></el-input>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="颜色" align="center" prop="thresholdColor" width="180">
                  <template slot-scope="scope">
                    <el-form-item :prop="'deviceThresholdList.' + scope.$index + '.thresholdColor'" :rules='formData.rules.thresholdColor'>
                      <el-select v-model="scope.row.thresholdColor">
                        <el-option v-for="item in thresholdColorList" :key="item.value" :label="item.label" :value="item.value"></el-option>
                      </el-select>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column label="报警内容" align="center" prop="describe" width="180">
                  <template slot-scope="scope">
                    <el-form-item :prop="'deviceThresholdList.' + scope.$index + '.describe'" :rules='formData.rules.describe'>
                      <el-input type="textarea" v-model="scope.row.describe" :autosize="{minRows: 1,maxRows: 3}"
                        placeholder="请输入报警内容" size="medium"></el-input>
                    </el-form-item>

                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-tooltip class="item fs-20" effect="dark" content="删除" placement="top-start">
                      <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.$index,scope.row)"></el-button>
                      <!-- v-hasPermi="['system:house:remove']" -->
                    </el-tooltip>
                  </template>
                </el-table-column>
              </el-table>
            </el-form>


 export default {
    data() {
      return {
       thresholdOptions: [{
       dictLable:'测试',dictValue:'1'}], //阈值字典设置
      thresholdColorList: [{
            label: '碧绿色',
            value: '#7FFFAA'
          },
          {
            label: '海洋绿',
            value: '#2E8B57'
          },
          {
            label: '卡其布',
            value: '#F0E68C'
          },
          {
            label: '秋麒麟',
            value: '#DAA520'
          },
          {
            label: '橙红色',
            value: '#FF4500'
          },
          {
            label: '纯红',
            value: '#FF0000'
          },
          {
            label: '深灰色',
            value: '#A9A9A9'
          },
          {
            label: '深天蓝',
            value: ' 	#00BFFF'
          }
        ],
		formData: {
		          rules: {
		            thresholdStatus: {
		              required: true,
		              message: "必选字段",
		              trigger: "change"
		            },
		            describe: {
		              required: true,
		              message: "必填字段",
		              trigger: "blur"
		            },
		            vals: {
		              required: true,
		              message: "必填字段",
		              trigger: "blur"
		            },
		            thresholdColor: {
		              required: true,
		              message: "必须字段",
		              trigger: "change"
		            }
		          },
		          //阈值table
		          deviceThresholdList: [{
		            thresholdStatus: '1', //阈值
		            describe: '', //报警内容
		            thresholdColor: '', //颜色
		            vals: '' //值
		          }]
		        }
		      }
		 }
	},
	methods: {
     
      /**规则新增*/
      addRule() {
        var newValue = {
          thresholdStatus: '0', //阈值
          describe: '', //报警内容
          thresholdColor: '', //颜色
          vals: '' //值
        }
        //添加新的行数
        this.formData.deviceThresholdList.push(newValue);
      },
      /**规则删除*/
      handleDelete(index) {
        this.formData.tableData.splice(index, 1)
      },
      }
}


在Vue3中使用Element Plus时,如果你想确保`el-input`和`el-select`组件内的必填字段在表格中得到正确的验证,你可以结合Element Plus提供的表单验证功能`el-form`和`el-form-item`,以及自定义的Vuelidate或Vue官方的`v-model`验证规则。 首先,确保你已经安装了相应的依赖,如`@vue/cli-plugin-eslint`, `eslint-plugin-vue` 和 `vuelidate` 或者 `axios` (如果数据来自API): ```bash npm install eslint-plugin-vue vuelidate axios ``` 然后,在你的组件中创建一个`form`对象,在`el-form`上设置`model`为你需要验证的对象: ```html <template> <el-form :model="formData" ref="form"> <el-table :data="tableData"> <el-table-column prop="inputField" label="输入字段"> <template slot-scope="scope"> <el-input v-model="scope.row.inputField" :rules="{ required: true, message: '请输入内容', trigger: 'blur' }"></el-input> </template> </el-table-column> <el-table-column prop="selectField" label="选择字段"> <template slot-scope="scope"> <el-select v-model="scope.row.selectField" :rules="{ required: true, message: '请选择选项', trigger: 'change' }"> <el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value"></el-option> </el-select> </template> </el-table-column> </el-table> <!-- 添加表单提交按钮 --> <el-button type="primary" @click="submitForm">提交</el-button> </el-form> </template> <script> import { defineComponent, ref, computed } from 'vue'; import axios from 'axios'; export default defineComponent({ data() { return { formData: {}, // 初始化表单数据 tableData: [], // 表格数据 options: [] // 选择项列表 }; }, methods: { submitForm() { this.$refs.form.validate(valid => { if (valid) { // 数据验证通过,处理提交逻辑 axios.post('/api/submit', this.formData).then(response => { console.log('提交成功'); }); } else { console.error('表单验证失败'); } }); }, // 其他数据获取和填充函数... }, }); </script> ``` 在这个例子中,每个必填字段都有一个`required`验证规则,会在用户离开输入框或选择框时触发验证。如果用户尝试提交表单,`validate`方法会检查整个表单是否都有效。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值