1、html
<a-popover v-model="visible" title="请选择时间" trigger="click">
<template slot="content">
<a-date-picker
v-model="add_time.day"
:disabledDate="disabledDay"
style="width: 120px;margin-right: 5px"/>
<a-time-picker
v-model="add_time.hour"
style="width: 80px"
:allowClear="false"
:disabledHours="disabledHour"
:disabledMinutes="disabledMinute"
format="HH:mm"
v-show="visible"/>
<a-button type="primary" style="margin-left: 15px" @click="handleAdd" v-show="visible">确定</a-button>
</template>
<a-button class="editable-add-btn" type="primary">添加一行</a-button>
</a-popover>
效果是这个样子:
2、js部分
data部分
add_time: {
day: moment(),
hour: moment()
},
visible: false,
methods部分
// 禁用年月日
disabledDay (current) {
return current && current > moment().endOf('day')
},
// 禁用小时
disabledHour () {
const hours = []
// 昨天或者更久之前
if (this.add_time.day.diff(moment(), 'days') === 0) {
const timeHours = moment().format('HH')
for (var i = 0; i < 24; i++) {
if (i > timeHours) {
hours.push(i)
}
}
}
return hours
},
// 禁用分钟
disabledMinute (selectedHour) {
const minute = []
if (this.add_time.day.diff(moment(), 'days') === 0 && moment().hour(selectedHour).diff(moment(), 'hours') === 0) {
const timeMinute = moment().format('mm')
for (var j = 0; j < 59; j++) {
if (j > timeMinute) {
minute.push(j)
}
}
}
return minute
},
// 添加
handleAdd () {
const data = []
this.span_key.forEach(item => {
const child = {}
child.key = item
if (item === 'A') {
child.value = moment(this.add_time.day).format('YYYY-MM-DD')
} else if (item === 'B') {
child.value = moment(this.add_time.hour).format('HH:mm')
} else {
child.value = ''
}
data.push(child)
})
const newData = [data]
接口({
file: this.fileId || this.$route.query.fileId,
data: newData,
}).then(res => {
this.$message.success('添加成功')
this.$refs.table.refresh()
}).catch(err => {
if (err.code === 10177) {
this.$message.error('添加失败,该条记录已存在')
}
})
this.visible = false
},