期待效果:表格内的每行的入场时间与离场时间互相限制但不影响其它行的这两项选择器的选择。
做限制element官网给的方法为:
首先表格内数据是这样写的:
<el-table-column prop="planArriveDate" label="计划入场时间" align="center" min-width="150">
<template slot-scope="scope">
<el-date-picker
v-model="scope.row.planArriveDate"
type="date"
style="width: 100%"
value-format="yyyy-MM-dd"
:picker-options="newStartOptions"
@change="timeStatus(scope.row)"
@focus="focusTime(scope.row)"
clearable
placeholder="计划入场时间">
</el-date-picker>
</template>
</el-table-column>
<el-table-column prop="planExitDate" label="计划离场时间" align="center" min-width="150">
<template slot-scope="scope">
<el-date-picker
v-model="scope.row.planExitDate"
type="date"
style="width: 100%"
value-format="yyyy-MM-dd"
:picker-options="newExitOptions"
@change="timeStatus(scope.row)"
@focus="focusTime(scope.row)"
clearable
placeholder="计划离场时间">
</el-date-picker>
</template>
</el-table-column>
然后是change方法和focus方法:
// 时间开始选择器
timeStatus(row) {
this.overDate = row.planExitDate
this.startDate = row.planArriveDate
},
// 获取焦点后,开始/完成时间为当前行的开始/完成时间
focusTime(row) {
this.overDate = row.planExitDate
this.startDate = row.planArriveDate
},
最后在computed内做限制:
computed: {
newStartOptions() {
const timeEnd = this.overDate
return {
disabledDate(time) {
if (!timeEnd) {
return time.getTime() < Date.now()
} else {
return time.getTime() > (new Date(timeEnd.substring(0, 11))).getTime() || time.getTime() < Date.now()
}
}
}
},
newExitOptions() {
const timeStart = this.startDate
return {
disabledDate(time) {
if (!timeStart) {
return time.getTime() < Date.now()
} else {
return time.getTime() < (new Date(timeStart.substring(0, 11))).getTime()
}
}
}
}
},