1. 程式人生 > >element ui在table中放入input且實現驗證

element ui在table中放入input且實現驗證

<template>
  <div class="app-container" style="overflow: auto;">
    <el-table
      :data="list"
      size="small"
      element-loading-text="Loading"
      border
      highlight-current-row
    >
      <el-table-column label="會員賬號">
        <template slot-scope="scope">
          <el-form :model="scope.row" :rules="rules">
            <el-form-item prop="login">
              <el-input v-show="true" v-model="scope.row.login" placeholder="請輸入會員賬號"/>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="真實姓名">
        <template slot-scope="scope">
          <el-form :model="scope.row" :rules="rules">
            <el-form-item prop="real_name">
              <el-input v-show="true" v-model="scope.row.real_name" placeholder="請輸入真實姓名"/>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="會員暱稱">
        <template slot-scope="scope">
          <el-form :model="scope.row" :rules="rules">
            <el-form-item prop="nickname">
              <el-input v-show="true" v-model="scope.row.nickname" placeholder="請輸入會員暱稱"/>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="會員手機">
        <template slot-scope="scope">
          <el-form :model="scope.row" :rules="rules">
            <el-form-item prop="mobile">
              <el-input v-show="true" v-model="scope.row.mobile" placeholder="請輸入手機號碼"/>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-form ref="scope.row" :model="scope.row">
            <el-form-item>
              <el-button @click="handleSubmit(scope.$index,scope.row)">重試</el-button>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { retry } from '@/api/table'
import { validateName } from '@/utils/validate'
import { validateMobile } from '@/utils/validate'

export default {
  data() {
    // validator
    const validateNameInput = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('此項必填!'))
      } else {
        callback()
      }
    }
    const validateMobileInput = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手機號碼必填!'))
      } else if (!validateMobile(value)) {
        return callback(new Error('請填寫正確的手機號碼格式!'))
      } else {
        callback()
      }
    }

    return {
      list: [],
      // input框失焦校驗
      rules: {
        nickname: [{ validator: validateNameInput, trigger: 'blur' }],
        real_name: [{ validator: validateNameInput, trigger: 'blur' }],
        mobile: [{ validator: validateMobileInput, trigger: 'blur' }],
        login: [{ validator: validateNameInput, trigger: 'blur' }]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      const errorData = JSON.parse(this.$route.query.errorData)
      this.list = JSON.parse(errorData)
    },
    handleSubmit(index, row) {
      // 提交校驗
      if (validateName(row.login) && validateName(row.real_name) && validateName(row.nickname) && validateMobile(row.mobile)) {
       //資料提交和錯誤catch
    }
  }
}
</script>