vue之封装预约类组件
vue之封装预约类组件
0. 展示所有数据,【禁用】的展示【灰色】,【没选中】的展示【白色】,【选中的】时间范围展示【红色】
1. 点击两次,锁定时间范围。
2. 如果有跨越了禁用时间段的话,则重新选择。
3. 【禁用的时间范围】 和 【全部的时间范围】,后端会返回。
4. 插件会返回选择的时间。
<template>
<view class="item" v-bind:class="{active:item.active,disabled:item.disabled}" v-for="(item, index) in list"
:key="index" v-on:click="clickItem(item,index)">
<view style="display: flex;justify-content: space-between;">
<view>
{{item.time}}
</view>
<view v-if="item.disabled">
该条已被<text style="color: white;">{{item.bookedBy}}</text>预约
</view>
</view>
</view>
</template>
<!-- 预约类时间段选择组件的开发思路:
0. 展示所有数据,【禁用】的展示【灰色】,【没选中】的展示【白色】,【选中的】时间范围展示【红色】
1. 点击两次,锁定时间范围。
2. 如果有跨越了禁用时间段的话,则重新选择。
3. 【禁用的时间范围】 和 【全部的时间范围】,后端会返回。
4. 插件会返回选择的时间。
-->
<script>
export default {
props: {
checkTime: {
type: Array
},
allTime: {
type: Array
},
},
data() {
return {
list: this.allTime,
disabledList: this.checkTime,
clickNumber: 0,
indexArr: [],
}
},
created() {
this.initTime();
},
computed: {
timeList() {
let list = []
this.list.forEach(item => {
if (item.active) {
list.push(item)
}
})
let obj = {
start: '',
end: ''
}
if (list.length) {
obj.start = list[0].time.split('-')[0]
obj.end = list[list.length - 1].time.split('-')[1];
}
return obj;
}
},
methods: {
initTime() {
this.list.forEach(item => {
this.disabledList.forEach(vitem => {
const hasTime = item.time == vitem.time
if (hasTime) {
item.disabled = true
}
})
})
},
clickItem(item, index) {
if (item.disabled) {
return
}
this.clickNumber++;
if (this.clickNumber == 1) {
this.indexArr.push(index)
}
if (this.clickNumber == 2) {
this.indexArr.push(index)
}
console.log(this.indexArr)
if (this.clickNumber == 3) {
this.indexArr = [index]
}
this.indexArr = this.indexArr.sort((a, b) => a - b);
if (this.clickNumber % 3 == 0) {
this.clickNumber = 1;
this.list.forEach(v => v.active = false);
item.active = true
}
this.getCheckList(item)
},
getCheckList(vitem) {
try {
console.log("getCheckList", this.list, this.indexArr)
this.list.forEach((item, idx) => {
const [start, end] = this.indexArr
if (idx >= start && idx <= end) {
if (item.disabled) {
throw new Error("包含disabled状态数据")
}
item.active = true
}
})
} catch (err) {
this.list.forEach(v => v.active = false);
vitem.active = true;
}
vitem.active = true;
this.$emit('getTime', this.timeList)
}
}
}
</script>
<style>
.item {
line-height: 40px;
background: #fff;
border-bottom: 1px solid #D2D2D2;
}
.item.active {
background: darkred;
color: black;
}
.item.disabled {
background: #888888;
color: white;
}
</style>
<TimeSelection @getTime='getTime' ref="child" :allTime="timeSelectList"
:checkTime="timeSelectDisabledList" />
getTime(val) {
console.log("getTime组件选择后的结果", val)
},
timeSelectList: [],
timeSelectDisabledList: [],