vue电影座位选座,最简单的写法没有之一

废话不多说,直接上代码,由于代码比较简单,不多做解析

<template>
    <div class="sm">
        <div class="sm-title"></div>
        <div class="sm-line"></div>
        <div style="display: flex; justify-content: center">
            <div>
                <div style="display: flex" v-for="(item1, index1) in seatList" :key="index1">
                    <!-- v-if="JSON.stringify(item1) !== '{}'" -->
                    <template>
                        <div @click="seatClk(item)" class="sm-img" v-for="item in item1" :key="item.id">
                            <img
                                v-if="item.status === 'N'"
                                class="o-w o-h"
                                src="https://hijinka.oss-cn-shanghai.aliyuncs.com/uploads/mall1/20220307/669dde5d9fe28a377c151cadecb8dd65.png"
                                alt=""
                            />
                            <img
                                v-if="item.status === 'Y' && !item.isCheck"
                                class="o-w o-h"
                                src="https://hijinka.oss-cn-shanghai.aliyuncs.com/uploads/mall1/20220307/123044531581e70133020f0265bcabb2.png"
                                alt=""
                            />
                            <img
                                v-if="item.status === 'Y' && item.isCheck"
                                class="o-w o-h"
                                src="https://hijinka.oss-cn-shanghai.aliyuncs.com/uploads/mall1/20220307/899c7f9e0365ce6d97f9d5fe89ffe878.png"
                                alt=""
                            />
                        </div>
                    </template>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'sysIndex',
    components: {},
    data() {
        return {
            // 页面渲染的数据
            seatList: [],
            // 后端返回的数据
            arrList: [
                {
                    id: 1,
                    seatNo: '1排1座',
                    status: 'N',
                    rowNo: '1',
                    colNo: '1'
                },
                {
                    id: 2,
                    seatNo: '1排2座',
                    status: 'Y',
                    rowNo: '1',
                    colNo: '2'
                },
                {
                    id: 3,
                    seatNo: '1排3座',
                    status: 'Y',
                    rowNo: '1',
                    colNo: '3'
                },
                {
                    id: 4,
                    seatNo: '1排4座',
                    status: 'Y',
                    rowNo: '1',
                    colNo: '4'
                },
                {
                    id: 5,
                    seatNo: '1排5座',
                    status: 'Y',
                    rowNo: '1',
                    colNo: '5'
                },

                {
                    id: 6,
                    seatNo: '2排2座',
                    status: 'Y',
                    rowNo: '2',
                    colNo: '2'
                },
                {
                    id: 7,
                    seatNo: '2排3座',
                    status: 'Y',
                    rowNo: '2',
                    colNo: '3'
                },
                {
                    id: 8,
                    seatNo: '2排4座',
                    status: 'Y',
                    rowNo: '2',
                    colNo: '4'
                },

                {
                    id: 9,
                    seatNo: '3排4座',
                    status: 'Y',
                    rowNo: '3',
                    colNo: '4'
                }
            ]
        };
    },
    mounted() {
        // 找到行的最大值
        const rowMax = this.findObjInArrMax(this.arrList, 'rowNo', true);
        // 找到列的最大值
        const colMax = this.findObjInArrMax(this.arrList, 'colNo', true);
        const rowNoMax = parseInt(rowMax.rowNo) + 1;
        const colNoMax = parseInt(colMax.colNo) + 1;
        // 将一维数组装成二维数组
        let list = [];
        for (var index = 0; index < rowNoMax; index++) {
            list[index] = [];
            for (var index1 = 0; index1 < colNoMax; index1++) {
                list[index][index1] = {};
            }
        }
        // 后端返回的数据组合到二维数组里面
        list.forEach((item1, index1) => {
            item1.forEach((item2, index2) => {
                this.arrList.forEach((item, index) => {
                    item.isCheck = false;
                    list[item.rowNo][item.colNo] = item;
                });
            });
        });
        console.log('list', list);
        this.seatList = list;
    },
    methods: {
        // 点击座位表
        seatClk(item) {
            // 如果是可以选择,而且没有选中状态
            if (item.status === 'Y' && !item.isCheck) {
                item.isCheck = true;
            } else if (item.status === 'Y' && item.isCheck) {
                item.isCheck = false;
            }
            console.log(item);
            this.$forceUpdate();
        },
        // 找到数组对象中的最大值
        findObjInArrMax(list, atr, returnVal) {
            let res = Math.max.apply(
                Math,
                list.map((item) => {
                    return item[atr];
                })
            );
            if (returnVal) {
                return list.filter((item) => {
                    return item[atr] == res;
                })[0];
            } else {
                return res;
            }
        },
        // 找到数组对象中的最小值
        findObjInArrMin(list, atr, returnVal) {
            let res = Math.min.apply(
                Math,
                list.map((item) => {
                    return item[atr];
                })
            );
            if (returnVal) {
                return list.filter((item) => {
                    return item[atr] == res;
                })[0];
            } else {
                return res;
            }
        }
    }
};
</script>

<style>
.sm-img {
    width: 26px;
    height: 26px;
}
.sm {
    position: relative;
}
.sm-line {
    width: 0;
    height: 100vh;
    border: 1px dashed #ccc;
    position: absolute;
    top: 18px;
    left: 50%;
}
.sm-title {
    background-color: #dddddd;
    width: 190px;
    height: 17px;
    -webkit-transform: perspective(17px) rotateX(-10deg);
    transform: perspective(17px) rotateX(-10deg);
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    z-index: 2;
}
</style>

 

  • 2
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是使用Vue实现电影选座效果的示例代码: ```html <template> <div class="seat-selection"> <h2>请座位</h2> <div class="screen">屏幕</div> <div class="seats"> <div v-for="(row, rowIndex) in seats" :key="rowIndex" class="row"> <div v-for="(seat, seatIndex) in row" :key="seatIndex" :class="{'seat': true, 'selected': seat.selected, 'unavailable': !seat.available}" @click="selectSeat(rowIndex, seatIndex)"> {{ seatIndex + 1 }} </div> </div> </div> <div class="legend"> <div class="legend-item available">可选座位</div> <div class="legend-item selected">已选座位</div> <div class="legend-item unavailable">不可选座位</div> </div> <div class="buttons"> <button class="btn btn-primary" @click="confirmSelection">确认选座</button> <button class="btn btn-secondary" @click="cancelSelection">取消选座</button> </div> </div> </template> <script> export default { data() { return { seats: [ // 该电影座位的二维数组 // 每个元素代表一个座位,包含属性available和selected // available表示该座位是否可,selected表示该座位是否被中 // 例如:{ available: true, selected: false } ], selectedSeats: [] // 已选座位的数组 } }, methods: { selectSeat(rowIndex, seatIndex) { // 座位的方法 if (this.seats[rowIndex][seatIndex].available) { this.seats[rowIndex][seatIndex].selected = !this.seats[rowIndex][seatIndex].selected; if (this.seats[rowIndex][seatIndex].selected) { this.selectedSeats.push([rowIndex, seatIndex]); } else { const index = this.selectedSeats.findIndex((seat) => seat[0] === rowIndex && seat[1] === seatIndex); this.selectedSeats.splice(index, 1); } } }, confirmSelection() { // 确认选座的方法 // 处理选座逻辑 }, cancelSelection() { // 取消选座的方法 // 清空已选座位数组和每个座位的selected属性 this.selectedSeats.forEach((seat) => { this.seats[seat[0]][seat[1]].selected = false; }); this.selectedSeats = []; } } } </script> <style scoped> .seat-selection { display: flex; flex-direction: column; align-items: center; gap: 10px; font-size: 16px; } .screen { width: 500px; height: 30px; background-color: black; color: white; text-align: center; line-height: 30px; } .seats { display: flex; flex-direction: column; gap: 5px; margin-top: 10px; } .row { display:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值