当前位置:首页 > VUE

vue实现选座位

2026-02-11 09:35:09VUE

Vue 实现选座位功能

数据准备

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

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 类动态控制样式。关键代码示例:

vue实现选座位

<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):

.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

前端多线程实现vue

前端多线程与Vue的结合 在Vue中实现多线程通常通过Web Worker技术完成。Web Worker允许在后台线程中运行脚本,避免阻塞主线程,适合处理计算密集型任务。 使用Web Worker的…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现上划

vue实现上划

Vue 实现上划功能 在Vue中实现上划功能,可以通过监听触摸事件(touchstart、touchmove、touchend)来判断用户的手势方向。以下是实现上划功能的几种方法: 监听触摸事件 通…

vue 水印 实现

vue 水印 实现

Vue 水印实现方法 使用 canvas 动态生成水印 在 Vue 组件中通过 canvas 绘制水印,并将其作为背景图片插入到目标元素中。这种方法灵活性高,支持动态调整水印内容、角度和透明度。 &…

vue实现左右布局

vue实现左右布局

实现左右布局的方法 在Vue中实现左右布局可以通过多种方式完成,以下是几种常见的方法: 使用Flexbox布局 Flexbox是一种现代的CSS布局方式,可以轻松实现左右布局。 <templ…

vue实现登录权限

vue实现登录权限

Vue 实现登录权限方案 路由守卫控制访问权限 安装vue-router并配置路由守卫,在全局前置守卫中检查用户登录状态和权限。未登录用户访问受限路由时重定向到登录页。 // router.js r…