<el-table
ref="singleTable"
:data="ruleForm.tableData"
:header-cell-style="{ textAlign: 'center' }"
style="width: 100%"
class="tableClass"
>
<!-- <el-table-column label="序号" type="index" width="50px" align="center"/> -->
<el-table-column label="月" width="60">
<template slot-scope="scope">
<el-form-item
label-width="0"
:prop="'tableData.' + scope.$index + '.t0280Month'"
:rules="[
{
validator: (rule, value, callback) => {
FormValidate.monthValidateValue(
rule,
value,
callback,
ruleForm.t0279Year
);
},
trigger: 'blur',
},
]"
>
<el-input v-model="scope.row.t0280Month" maxlength="10" @keyup.native="keyArrow($event, scope.$index, 0)" :ref="'arrow'+scope.$index+'_0'"/>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="日" width="60">
<template slot-scope="scope">
<el-form-item
label-width="0"
:prop="'tableData.' + scope.$index + '.t0280Day'"
:rules="[
{
validator: (rule, value, callback) => {
FormValidate.dayValidateValue(
rule,
value,
callback,
ruleForm.t0279Year,
scope.row.t0280Month
);
},
trigger: 'blur',
},
]"
>
<el-input v-model="scope.row.t0280Day" maxlength="10" @keyup.native="keyArrow($event, scope.$index, 1)" :ref="'arrow'+scope.$index+'_1'"/>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="时" width="60">
<template slot-scope="scope">
<el-form-item
label-width="0"
:prop="'tableData.' + scope.$index + '.t0280Hour'"
:rules="[
{
validator: (rule, value, callback) => {
FormValidate.hourValidateValue(rule, value, callback);
},
trigger: 'blur',
},
]"
>
<el-input v-model="scope.row.t0280Hour" maxlength="10" @keyup.native="keyArrow($event, scope.$index, 2)" :ref="'arrow'+scope.$index+'_2'"/>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="分" width="60">
<template slot-scope="scope">
<el-form-item
label-width="0"
:prop="'tableData.' + scope.$index + '.t0280Minute'"
:rules="[
{
validator: (rule, value, callback) => {
FormValidate.minuteValidateValue(rule, value, callback);
},
trigger: 'blur',
},
{ trigger: 'blur' },
]"
>
<el-input v-model="scope.row.t0280Minute" maxlength="10" @keyup.native="keyArrow($event, scope.$index, 3)" :ref="'arrow'+scope.$index+'_3'"/>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="水尺编号" width="160">
<template slot-scope="scope">
<el-form-item
label-width="0"
:prop="'tableData.' + scope.$index + '.t0280Bh'"
:rules="{
min: 0,
max: 10,
message: '长度不能大于10个字符',
trigger: 'blur',
}"
>
<el-input v-model="scope.row.t0280Bh" maxlength="10" @keyup.native="keyArrow($event, scope.$index, 4)" :ref="'arrow'+scope.$index+'_4'"/>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="水尺读数(米)" width="160">
<template slot-scope="scope">
<el-form-item label-width="0" :prop="'tableData.'+ scope.$index +'.t0280Dushu'" :rules="[{ pattern: /^\d+$|^\d*\.\d+$/g, message: '必须为数字'},{ pattern: /^[\\+\\-]?\d+(\.\d{1,2})?$/, message:'最多小数两位'},{ pattern: /^[0-9]{1,3}(\.[0-9]{1,2})?$/, message:'不能大于1000'}]">
<el-input v-model="scope.row.t0280Dushu" maxlength="10" @keyup.native="keyArrow($event, scope.$index, 5)" :ref="'arrow'+scope.$index+'_5'"/>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="水位(米)" width="160">
<template slot-scope="scope">
<el-form-item label-width="0" :prop="'tableData.'+ scope.$index +'.t0280Shuiwei'" :rules='[{validator: (rule,value,callback)=>{FormValidate.validateValue(rule,value,callback)}, trigger: "blur"},{ pattern: /(^\u6CB3\u5E72$)|(^\u8FDE\u5E95\u51BB$)|(^[\\+\\-]?\d+(\.\d{1,2})?$)|^\"$/, message: "格式有误"}]'>
<el-input v-model="scope.row.t0280Shuiwei" maxlength="10" @keyup.native="keyArrow($event, scope.$index, 6)" :ref="'arrow'+scope.$index+'_6'"/>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="取样桶号" width="160">
<template slot-scope="scope">
<el-form-item
label-width="0"
:prop="'tableData.' + scope.$index + '.t0280Tonghao'"
>
<el-input v-model="scope.row.t0280Tonghao" maxlength="10" @keyup.native="keyArrow($event, scope.$index, 7)" :ref="'arrow'+scope.$index+'_7'"/>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="备注" width="160">
<template slot-scope="scope">
<el-form-item
label-width="0"
:prop="'tableData.' + scope.$index + '.t0280Remark'"
:rules="{
min: 0,
max: 20,
message: '长度不能大于20个字符',
trigger: 'blur',
}"
>
<el-input v-model="scope.row.t0280Remark" maxlength="20" @keyup.native="keyArrow($event, scope.$index, 8)" :ref="'arrow'+scope.$index+'_8'"/>
</el-form-item>
</template>
</el-table-column>
<el-table-column
property="address"
label="操作"
width="100px"
align="center"
>
<template slot-scope="scope">
<el-button
size="mini"
type="text"
@click="handleUpdate(scope.$index, scope.row)"
>添加</el-button
>
<el-button
size="mini"
v-if="ruleForm.tableData.length > 1"
type="text"
@click="handleDelete(scope.row, scope.$index)"
>
删除
</el-button>
</template>
</el-table-column>
<el-table-column
property="address"
label="调整顺序"
width="100px"
align="center"
>
<template slot-scope="scope">
<el-button
v-if="scope.$index != 0"
@click="upward(scope.$index)"
size="mini"
>↑</el-button
>
<el-button
v-if="scope.$index != ruleForm.tableData.length - 1"
@click="downward(scope.$index)"
size="mini"
>↓</el-button
>
</template>
</el-table-column> </el-table
><br />
必须有:ref
//键盘方向键事件
keyArrow(e,rowIndex,columnIndex){
// 定义一个内部函数getinputAttrKey,接收两个参数:行索引rIndex和列索引cIndex
function getinputAttrKey(rIndex,cIndex){
// 返回格式为'arrow[rowIndex]_[columnIndex]'的字符串
return 'arrow'+rIndex+'_'+cIndex;
}
console.log("keyArrow",e.keyCode);
//当前开始触发的输入框
let nowInputRefAttr = getinputAttrKey(rowIndex,columnIndex)
let tableLen = this.ruleForm.tableData.length;
if (tableLen == 0) return
// 上键
if (e.keyCode == 38) {
console.log("上键");
if(rowIndex == 0){
console.log("第一行不能往上了");
return;
}
this.$refs[nowInputRefAttr].blur();
let inputRefAttr = getinputAttrKey(rowIndex-1,columnIndex)
console.log("inputRefAttr",inputRefAttr);
this.$nextTick(() => {
this.$refs[inputRefAttr].focus()
})
}
// 下键
if(e.keyCode == 40){
console.log("下键");
if(rowIndex == (tableLen - 1)){
console.log("最后一行不能往下了");
return;
}
this.$refs[nowInputRefAttr].blur();
let inputRefAttr = getinputAttrKey(rowIndex+1,columnIndex)
console.log("inputRefAttr",inputRefAttr);
this.$nextTick(() => {
this.$refs[inputRefAttr].focus()
})
}
// 左键
if (e.keyCode == 37) {
console.log("左键");
if(columnIndex == 0){
console.log("第一列行不能往左了");
return;
}
this.$refs[nowInputRefAttr].blur();
let inputRefAttr = getinputAttrKey(rowIndex,columnIndex-1)
console.log("inputRefAttr",inputRefAttr);
this.$nextTick(() => {
this.$refs[inputRefAttr].focus()
})
}
// 右键
if (e.keyCode == 39) {
console.log("右键");
if(columnIndex == 8){
//最后一列input框最大值是8,就写死,不同表格列数改一下,从0开始
console.log("最后一列行不能往右了");
return;
}
this.$refs[nowInputRefAttr].blur();
let inputRefAttr = getinputAttrKey(rowIndex,columnIndex+1)
console.log("inputRefAttr",inputRefAttr);
this.$nextTick(() => {
this.$refs[inputRefAttr].focus()
})
}
},
//各种验证
import { dataEnum } from "./dataEnum";
let FormValidate = {}
// 手机号码
FormValidate.MobilePhone = /^1[0-9]{10}$/
FormValidate.MobilePhoneMsg = '请输入正确的手机号码';
// 座机号码
FormValidate.Landline = /^(\d{3,4}-)?\d{7,8}(-\d{3,4})?$/
FormValidate.LandlineMsg = '请输入正确的座机号码';
// 手机和座机
FormValidate.GeneralNumber = /(^(\d{3,4}-)?\d{7,8}(-\d{3,4})?$)|(^1[0-9]{10}$)/
FormValidate.GeneralNumberMsg = '请输入正确的手机或座机号码';
// 必填验证
FormValidate.validateValue = function(rule, value, callback,year) {
// console.log("vvv",rule);
if(value+"" == 'null' || value+"" == 'NULL'){
value = "";
}
//console.log("vvv2",value+"");
if ((value+"").replace(/^\s+|\s+$/g,"").length < 1){
callback(new Error("必填"));
}
return callback();
}
// 年份自定义验证
FormValidate.yearValidateValue = function(rule, value, callback,row) {
if ((value+"").replace(/^\s+|\s+$/g,"").length < 1){
callback(new Error("必填"));
}
// let t0008Year = row.t0008Year;
// let now = new Date();
// let year = now.getFullYear(); // 得到年份
// if (t0008Year > year || t0008Year < 1950){
// callback(new Error("无效年份"));
// }
return callback();
}
// 月份验证
FormValidate.monthValidateValue = function(rule, value, callback,year) {
if (!year) {
return callback(new Error("年份有误"))
}
if ((value+"").replace(/^\s+|\s+$/g,"").length < 1){
callback(new Error("必填"));
}
if (!/^(0?[1-9]|1[0-2])$/g.test(value)){
callback(new Error("月份不存在"));
}
return callback();
}
// 日自定义验证
FormValidate.dayValidateValue = function(rule, value, callback,year,month,required = true) {
//console.log(rule, value, callback,year,month,"dayValidateValue");
if (!year) {
callback(new Error("年份有误"))
}
if(value+"" == 'null' || value+"" == 'NULL'){
value = "";
}
if ((value+"").replace(/^\s+|\s+$/g,"").length < 1){
if(required){
return callback(new Error("必填"));
}
return callback();
}
let m = parseInt(month)
let y = parseInt(year)
let thisMonthDate = new Date(y, m, 0);
let thisDays = thisMonthDate.getDate();
if (value < 1 || value > thisDays) {
callback(new Error("日不存在"));
}
callback();
}
// 小时自定义验证(0-23)
FormValidate.hourValidateValue = function(rule, value, callback) {
if ((value+"").replace(/^\s+|\s+$/g,"").length < 1){
callback(new Error("必填"));
}
if (!/^(2[0-3]|[0-1]?\d)$/g.test(value)){
callback(new Error("时间不存在"));
}
return callback();
}
<el-table-column label="读数(米)">
<template slot-scope="scope">
<el-form-item label-width="0" :prop="'tableData.'+ scope.$index +'.t0008DuShu'" :rules="[{ pattern: /^\d+$|^\d*\.\d+$/g, message: '必须为数字'},{ pattern: /^[\\+\\-]?\d+(\.\d{1,2})?$/, message:'最多小数两位'},{ pattern: /^[0-9]{1,3}(\.[0-9]{1,2})?$/, message:'不能大于1000'}]">
<el-input v-model="scope.row.t0008DuShu" :ref="'arrow'+scope.$index+'_5'" @keyup.native="keyArrow($event, scope.$index, 5)"/>
</el-form-item>
</template>
</el-table-column>
在页面自定义验证
<el-table-column align="center" label="降水时间"> <el-table-column align="center" label="起"> <el-table-column align="center" label="日" width="50"> <template v-slot="params"> <el-form-item label-width="0" :prop="'tableData.'+ params.$index +'.t0100QDay'" :rules="[{validator:(rule,value,callback)=>{FormValidate.dayValidateValue(rule,value,callback,ruleForm.Year,params.row.t0100GMonth)}, trigger: 'blur'}]"> <el-input :readonly="!isTableOperation" v-model="params.row.t0100QDay"/> </el-form-item> </template> </el-table-column> <el-table-column align="center" label="时" width="50"> <template v-slot="params"> <el-form-item label-width="0" :prop="'tableData.'+ params.$index +'.t0100QHour'" :rules="[{validator: (rule,value,callback)=>{FormValidate.hourValidateValue(rule,value,callback)}, trigger: 'blur'}]"> <el-input :readonly="!isTableOperation" v-model="params.row.t0100QHour"/> </el-form-item> </template> </el-table-column> <el-table-column align="center" label="分" width="50"> <template v-slot="params"> <el-form-item label-width="0" :prop="'tableData.'+ params.$index +'.t0100QMinute'" :rules="[{validator: (rule,value,callback)=>{FormValidate.minuteValidateValue(rule,value,callback)}, trigger: 'blur'}]"> <el-input :readonly="!isTableOperation" v-model="params.row.t0100QMinute"/> </el-form-item> </template> </el-table-column> </el-table-column> <el-table-column align="center" label="迄"> <el-table-column align="center" label="日" width="50"> <template v-slot="params"> <el-form-item label-width="0" :prop="'tableData.'+ params.$index +'.t0100ZDay'" :rules="[{validator:(rule,value,callback)=>{FormValidate.dayValidateValue(rule,value,callback,ruleForm.Year,params.row.t0100GMonth)}, trigger: 'blur'}]"> <el-input :readonly="!isTableOperation" v-model="params.row.t0100ZDay"/> </el-form-item> </template> </el-table-column> <el-table-column align="center" label="时" width="50"> <template v-slot="params"> <el-form-item label-width="0" :prop="'tableData.'+ params.$index +'.t0100ZHour'" :rules="[{validator: (rule,value,callback)=>{FormValidate.hourValidateValue(rule,value,callback)}, trigger: 'blur'}]"> <el-input :readonly="!isTableOperation" v-model="params.row.t0100ZHour"/> </el-form-item> </template> </el-table-column> <el-table-column align="center" label="分" width="50"> <template v-slot="params"> <el-form-item label-width="0" :prop="'tableData.'+ params.$index +'.t0100ZMinute'" :rules="[{validator: (rule,value,callback)=>{FormValidate.minuteValidateValue(rule,value,callback)}, trigger: 'blur'},{validator: (rule,value,callback)=>{minuteValidateValue1(rule,value,callback,params.row)}, trigger: 'blur'}]" > <el-input :readonly="!isTableOperation" v-model="params.row.t0100ZMinute" @blur="handleBlur(params.row)"/> </el-form-item> </template> </el-table-column> methods: { //验证 minuteValidateValue1(rule,value,callback,row){ console.log(row); if (!row.t0100GMonth) { return callback(new Error("月份有误")) } if (!row.t0100QDay) { return callback(new Error("起日有误")) } if (!row.t0100ZDay) { return callback(new Error("讫日有误")) } if (!row.t0100QHour) { return callback(new Error("起时有误")) } if (!row.t0100ZHour) { return callback(new Error("讫时有误")) } if (!row.t0100QMinute) { return callback(new Error("起分有误")) } if (!row.t0100ZMinute) { return callback(new Error("讫分有误")) } } //计算时间 handleBlur(row){ let t0100GMonth = '' let t0100QDay = '' let t0100QHour = '' let t0100QMinute = '' let t0100ZDay = '' let t0100ZHour = '' let t0100ZMinute = '' if(row.t0100GMonth.length == 1){ t0100GMonth = '0'+row.t0100GMonth }else{ t0100GMonth = row.t0100GMonth } if(row.t0100QDay.length == 1){ t0100QDay = '0'+row.t0100QDay }else{ t0100QDay = row.t0100QDay } if(row.t0100ZDay.length == 1){ t0100ZDay = '0'+row.t0100ZDay }else{ t0100ZDay = row.t0100ZDay } if(row.t0100QHour.length == 1){ t0100QHour = '0'+row.t0100QHour }else{ t0100QHour = row.t0100QHour } if(row.t0100ZHour.length == 1){ t0100ZHour = '0'+row.t0100ZHour }else{ t0100ZHour = row.t0100ZHour } if(row.t0100QMinute.length == 1){ t0100QMinute = '0'+row.t0100QMinute }else{ t0100QMinute = row.t0100QMinute } if(row.t0100ZMinute.length == 1){ t0100ZMinute = '0'+row.t0100ZMinute }else{ t0100ZMinute = row.t0100ZMinute } const date1 = new Date(this.ruleForm.Year+'-'+t0100GMonth+'-'+t0100QDay+'T'+t0100QHour+':'+t0100QMinute+':00'); const date2 = new Date(this.ruleForm.Year+'-'+t0100GMonth+'-'+t0100ZDay+'T'+t0100ZHour+':'+t0100ZMinute+':00'); // 计算两个日期时间之间的毫秒差 console.log(date2); const differenceMs = date2 - date1; console.log(differenceMs,'differenceMs'); // 将毫秒差转换为小时和分钟 row.t0100LiHour = Math.floor((differenceMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); row.t0100LiMinute = Math.floor((differenceMs % (1000 * 60 * 60)) / (1000 * 60)); }, }