草庐IT

element-ui的el-form表单和el-table校验嵌套使用校验el-input和el-select

cyndi_超努力 2023-04-18 原文

场景:前端开发中,经常会遇到比较多的表单填写页面,其中有el-form,el-table,表格的每一列中又嵌套着输入框或者下拉框,然后每个还需要做单独的校验

效果:

  点击保存可校验el-input和el-select是否有值,不符合校验规则就标红提醒

 

1.el-form嵌套el-table

  • 1.el-form里面嵌套el-table
    <el-form :model="formData" :rules="formData.rules" ref="formRef">
        <el-table
           :data="formData.tableData"
           style="width: 100%">
           <el-table-column
             prop="id"
             label="单号"
             width="100">
           </el-table-column>
         </el-table>
    </el-form>
    
  • 2.在el-table-column自定义内容里面,嵌套el-form-item,里面再放el-input或者el-select结构
     <el-table-column
       prop="name"
        label="商品名"
        width="180">
        <template slot-scope="scope">
            <el-form-item>
              <el-input v-model="scope.row.name"></el-input>
            </el-form-item>
        </template>
      </el-table-column>
    
  • 3.数据的结构
    因为el-form是最外层的,所以formData是个对象,对象里放el-table用到的数组和rules
    这样定义是为了校验时用到rules

 

 formData: {
    rules: {
        name: {
          required: true,
          message: '请输入商品名',
          tirgger: ['blur', 'change']
        },
        selected: {
          required: true,
          message: '请选择',
          tirgger: 'change'
        }
      },
      tableData: [
        {id: '001'},
        {id: '002'},
        {id: '003'},
      ]
    },

2.定义规则

1.el-form绑定: rules使用formData.rules整个对象,同时定义一个ref

<el-form :model="formData" :rules="formData.rules" ref="formRef">

2.保证prop必须唯一
自定义prop:使用 列表数据属性名+列的下标scope.$index+列的数据属性名

 <el-table-column
   prop="name"
    label="商品名"
    width="180">
    <template slot-scope="scope">
        <el-form-item :prop="'tableData.' + scope.$index + '.name'" :rules="formData.rules.name">
          <el-input v-model="scope.row.name"></el-input>
        </el-form-item>
    </template>
  </el-table-column>

注意:如果不是使用列表数据属性名tableData,会出现报错
Error: please transfer a valid prop path to form item

 3.保证每一个el-form-item都要配置rules属性
rules使用formData.rules对应的属性的规则

3.完整代码

<template>
  <div>
    <el-form :model="formData" :rules="formData.rules" ref="formRef">
      <el-table
        :data="formData.tableData"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="单号"
          width="100">
        </el-table-column>
        <el-table-column
          prop="name"
          label="商品名"
          width="180">
          <template slot-scope="scope">
            <el-form-item :prop="'tableData.' + scope.$index + '.name'" :rules="formData.rules.name">
              <el-input v-model="scope.row.name"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column
          prop="class"
          label="分类">
          <template slot-scope="scope">
           <el-form-item :prop="'tableData.' +scope.$index + '.class'" :rules="formData.rules.selected">
             <el-select v-model="scope.row.class">
                <el-option
                  v-for="item in classOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
           </el-form-item>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <el-button @click="submitForm('formRef')">提交</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        rules: {
          name: {
            required: true,
            message: '请输入商品名',
            tirgger: ['blur', 'change']
          },
          selected: {
            required: true,
            message: '请选择',
            tirgger: 'change'
          }
        },
        tableData: [
          {id: '001'},
          {id: '002'},
          {id: '003'},
        ]
      },
      classOption: [
        {
          value: 0,
          label: '分类1'
        },
        {
          value: 0,
          label: '分类1'
        }
      ]
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

有关element-ui的el-form表单和el-table校验嵌套使用校验el-input和el-select的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  3. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  4. ruby - rbenv 安装 ruby​​ 校验和不匹配 osx - 2

    我已经在mountainlion上成功安装了rbenv和ruby​​build。运行rbenvinstall1.9.3-p392结束于:校验和不匹配:ruby-1.9.3-p392.tar.gz(文件已损坏)预期f689a7b61379f83cbbed3c7077d83859,得到1cfc2ff433dbe80f8ff1a9dba2fd5636它正在下载的文件看起来没问题,如果我使用curl手动下载文件,我会得到同样不正确的校验和。有没有人遇到过这个?他们是如何解决的? 最佳答案 tl:博士;使用浏览器从http://ftp.rub

  5. ruby-on-rails - 事件记录 : Select max of limit - 2

    我正在尝试将以下SQL查询转换为ActiveRecord,它正在融化我的大脑。deletefromtablewhereid有什么想法吗?我想做的是限制表中的行数。所以,我想删除少于最近10个条目的所有内容。编辑:通过结合以下几个答案找到了解决方案。Temperature.where('id这给我留下了最新的10个条目。 最佳答案 从您的SQL来看,您似乎想要从表中删除前10条记录。我相信到目前为止的大多数答案都会如此。这里有两个额外的选择:基于MurifoX的版本:Table.where(:id=>Table.order(:id).

  6. ruby-on-rails - rails 上的 ruby : radio buttons for collection select - 2

    我有一个集合选择:此方法的单选按钮是什么?谢谢 最佳答案 Rails3中没有这样的助手。在Rails4中,它是collection_radio_buttons. 关于ruby-on-rails-rails上的ruby:radiobuttonsforcollectionselect,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/18525986/

  7. ruby - RVM pkg 安装校验和错误 - 2

    root@li417-132:~#rvmpkginstallzlibFetchingzlib-1.2.7.tar.gzto/usr/local/rvm/archivesThereisnochecksumfor'http://prdownloads.sourceforge.net/libpng/zlib-1.2.7.tar.gz'or'zlib-1.2.7.tar.gz',it'snotpossibletovalidateit.Ifyouwishtocontinuewithunverifieddownloadadd'--verify-downloads1'afterthecommand.

  8. ruby - Rails 3 - 我可以将开始日期设置为 date_select 方法吗? - 2

    date_select方法只能设置:start_year,但我想设置开始日期(例如3个月前的日期)(但没有这样的选项)。那么,我可以将开始日期设置为date_select方法吗?或者,要制作这样的选择框,我应该使用select_tag和options_for_select吗?或者,有什么解决办法吗?谢谢, 最佳答案 有可能……例如:start_year–设置年份选择的开始年份。默认为Time.now.year-5参见thisresource. 关于ruby-Rails3-我可以将开始日期

  9. ruby-on-rails - 从 ActiveAdmin has_many 表单助手中删除 "Add new"按钮 - 2

    我在事件管理员编辑页面中有嵌套资源,但我只想允许管理员编辑现有资源的内容,而不是添加新的嵌套资源。我的代码看起来像这样:formdo|f|f.inputsdof.input:authorf.input:contentf.has_many:commentsdo|comment_form|comment_form.input:contentcomment_form.input:_destroy,as::boolean,required:false,label:'Remove'endendf.actionsend但它在输入下添加了“添加新评论”按钮。我怎样才能禁用它,并只为主窗体保留f.ac

  10. ruby-on-rails - 用于 Rails 的 HAML 表单 - 2

    我目前正在尝试将ERB布局转换为HAML。这是我不断收到的错误:index.html.haml:18:syntaxerror,unexpected')'));}\n#{_hamlout.format_...这是HAML页面:.row-fluid.span6%h2TodoList.span6%h2{:style=>"text-align:right;"}document.write(today)%hr.divider.row-fluid.span6%h2.small_headNewTask=render:partial=>'layouts/form_errors',:locals=>{:

随机推荐