当前位置:首页 > VUE

vue选座如何实现

2026-02-25 04:43:41VUE

Vue 选座功能实现方法

数据结构设计

选座功能的核心是座位数据的组织。通常使用二维数组或对象数组表示座位布局,例如:

seats: [
  { row: 'A', col: 1, status: 'available' }, // 可用
  { row: 'A', col: 2, status: 'selected' },  // 已选
  { row: 'B', col: 1, status: 'occupied' }   // 已占用
]

或影院式布局:

rows: [
  {
    name: 'A',
    cols: [
      { number: 1, type: 'standard', status: 'available' },
      { number: 2, type: 'vip', status: 'occupied' }
    ]
  }
]

可视化渲染

使用 v-for 循环渲染座位,通过动态 class 绑定不同状态样式:

<div v-for="row in rows" :key="row.name" class="seat-row">
  <div 
    v-for="col in row.cols" 
    :key="col.number"
    :class="['seat', col.status, col.type]"
    @click="selectSeat(row, col)"
  >
    {{ col.number }}
  </div>
</div>

CSS 示例:

.seat {
  width: 30px;
  height: 30px;
  margin: 5px;
  cursor: pointer;
}
.available { background-color: #ccc; }
.selected { background-color: #4CAF50; }
.occupied { background-color: #f44336; }
.vip { border: 2px solid gold; }

交互逻辑

实现座位选择/取消的点击事件:

methods: {
  selectSeat(row, col) {
    if (col.status === 'occupied') return;

    col.status = col.status === 'selected' 
      ? 'available' 
      : 'selected';

    this.updateSelectedSeats();
  },
  updateSelectedSeats() {
    this.selectedSeats = this.rows.flatMap(row => 
      row.cols.filter(col => col.status === 'selected')
    );
  }
}

进阶功能

  1. 连选限制:限制必须选择相邻座位

    validateAdjacent(selectedSeats) {
    const rows = new Set(selectedSeats.map(s => s.row));
    const cols = selectedSeats.map(s => s.col).sort();
    return cols.every((col, i) => i === 0 || col === cols[i-1] + 1);
    }
  2. 座位类型差异:不同座位类型显示不同价格

    computed: {
    totalPrice() {
     return this.selectedSeats.reduce((sum, seat) => 
       sum + (seat.type === 'vip' ? 50 : 30), 0);
    }
    }
  3. 屏幕标识:添加影院屏幕标识

    <div class="screen">SCREEN</div>
    <style>
    .screen {
    width: 80%;
    height: 20px;
    margin: 0 auto 30px;
    background: linear-gradient(to top, #555, #999);
    box-shadow: 0 0 10px rgba(0,0,0,0.5);
    }
    </style>

数据持久化

提交选座数据到后端:

async confirmSelection() {
  const res = await axios.post('/api/book', {
    seats: this.selectedSeats,
    sessionId: this.sessionId
  });
  if (res.data.success) {
    this.$router.push('/payment');
  }
}

注意事项

  • 使用 Vuex 管理复杂状态时,建议将座位数据存储在 store 中
  • 移动端适配需要增加触摸事件和响应式布局
  • 性能优化:大量座位渲染时建议使用虚拟滚动技术
  • 可访问性:为座位添加 ARIA 标签和键盘导航支持

vue选座如何实现

标签: 如何实现vue
分享给朋友:

相关文章

vue实现多选联动

vue实现多选联动

vue实现多选联动的方法 使用v-model绑定数组 在Vue中,可以通过v-model绑定一个数组来实现多选联动。当复选框被选中或取消选中时,数组会自动更新。 <template>…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-c…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的…

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…