当前位置:首页 > VUE

vue实现选座位

2026-02-11 09:35:09VUE

Vue 实现选座位功能

数据准备

创建一个座位数组,每个座位对象包含唯一标识符、行列位置和选中状态。示例数据结构如下:

data() {
  return {
    seats: [
      { id: 1, row: 1, col: 1, selected: false, booked: false },
      { id: 2, row: 1, col: 2, selected: false, booked: true },
      // 更多座位...
    ]
  }
}

渲染座位布局

使用 v-for 循环渲染座位,通过 CSS 类动态控制样式。关键代码示例:

<div class="seat-map">
  <div 
    v-for="seat in seats" 
    :key="seat.id"
    :class="['seat', 
             { 'selected': seat.selected, 
               'booked': seat.booked }]"
    @click="toggleSeat(seat)"
  >
    {{ seat.row }}-{{ seat.col }}
  </div>
</div>

处理点击事件

实现座位选择/取消选择逻辑,注意已预订座位不可选:

methods: {
  toggleSeat(seat) {
    if (!seat.booked) {
      seat.selected = !seat.selected
      this.$emit('seat-selected', seat)
    }
  }
}

样式设计

基础样式示例(SCSS):

vue实现选座位

.seat-map {
  display: grid;
  grid-template-columns: repeat(8, 40px);
  gap: 10px;
}

.seat {
  width: 40px;
  height: 40px;
  border: 1px solid #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;

  &.selected {
    background-color: #42b983;
    color: white;
  }

  &.booked {
    background-color: #ff4444;
    cursor: not-allowed;
  }
}

高级功能扩展

  • 添加屏幕区域标识
  • 实现连选功能(按住Shift多选)
  • 添加座位类型区分(VIP/普通)
  • 与后端API集成保存选择状态
  • 使用SVG实现更复杂的座位图形

完整组件示例

<template>
  <div>
    <h3>屏幕</h3>
    <div class="screen"></div>
    <div class="seat-map">
      <div 
        v-for="seat in seats" 
        :key="seat.id"
        :class="['seat', 
                { 'selected': seat.selected, 
                  'booked': seat.booked }]"
        @click="toggleSeat(seat)"
      >
        {{ seat.row }}-{{ seat.col }}
      </div>
    </div>
    <div>已选座位: {{ selectedSeats }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      seats: this.generateSeats(5, 8)
    }
  },
  computed: {
    selectedSeats() {
      return this.seats.filter(s => s.selected).map(s => `${s.row}排${s.col}座`)
    }
  },
  methods: {
    generateSeats(rows, cols) {
      // 生成座位数据的实现
    },
    toggleSeat(seat) {
      // 选择逻辑实现
    }
  }
}
</script>

标签: 座位vue
分享给朋友:

相关文章

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…

vue登录实现

vue登录实现

Vue 登录实现 实现登录功能通常需要前端与后端配合,Vue 作为前端框架,主要负责表单处理、请求发送和状态管理。以下是常见的实现方式: 表单设计与数据绑定 使用 Vue 的 v-model 实现表…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…