当前位置:首页 > 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实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提交…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

javascript实现vue

javascript实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,主要包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化的实现示例。 数据响应式系统 通过 Object.de…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue实现vr

vue实现vr

Vue 实现 VR 的方法 Vue 可以与 WebVR 或 WebXR API 结合使用,通过 Three.js、A-Frame 等库实现 VR 效果。以下是几种常见方法: 使用 A-Frame 框…