当前位置:首页 > VUE

vue实现选座

2026-03-09 00:07:52VUE

Vue 实现选座功能

选座功能常见于影院、演出、交通等场景,以下是基于 Vue 的实现方案:

数据结构设计

使用二维数组表示座位布局,每个座位包含状态属性(如可选、已选、已售):

vue实现选座

data() {
  return {
    seats: [
      [{status: 'available'}, {status: 'sold'}],
      [{status: 'available'}, {status: 'available'}]
    ],
    selectedSeats: []
  }
}

座位渲染

通过嵌套 v-for 渲染座位矩阵,动态绑定样式类:

vue实现选座

<div v-for="(row, rowIndex) in seats" :key="rowIndex" class="seat-row">
  <div 
    v-for="(seat, colIndex) in row" 
    :key="colIndex"
    :class="['seat', seat.status]"
    @click="selectSeat(rowIndex, colIndex)"
  >
    {{ rowIndex + 1 }}-{{ colIndex + 1 }}
  </div>
</div>

选择逻辑实现

处理座位点击事件,更新状态和选中列表:

methods: {
  selectSeat(row, col) {
    if (this.seats[row][col].status === 'sold') return;

    const seatKey = `${row}-${col}`;
    if (this.selectedSeats.includes(seatKey)) {
      this.seats[row][col].status = 'available';
      this.selectedSeats = this.selectedSeats.filter(k => k !== seatKey);
    } else {
      this.seats[row][col].status = 'selected';
      this.selectedSeats.push(seatKey);
    }
  }
}

样式设计

通过 CSS 区分不同状态:

.seat {
  width: 30px;
  height: 30px;
  margin: 5px;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.available { background: #4CAF50; }
.selected { background: #FFC107; }
.sold { background: #F44336; cursor: not-allowed; }

优化建议

  1. 添加屏幕区域标识(如荧幕位置)
  2. 实现最大可选数量限制
  3. 加入座位类型区分(如VIP座)
  4. 使用 Vuex 管理复杂状态
  5. 增加选座确认和反选功能

完整组件示例

<template>
  <div class="seat-map">
    <div class="screen">荧幕</div>
    <div v-for="(row, rowIndex) in seats" :key="rowIndex" class="seat-row">
      <div 
        v-for="(seat, colIndex) in row" 
        :key="colIndex"
        :class="['seat', seat.status, seat.type]"
        @click="handleSeatClick(rowIndex, colIndex)"
      >
        {{ rowIndex + 1 }}-{{ colIndex + 1 }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      maxSelection: 5,
      seats: [
        [
          {status: 'available', type: 'standard'},
          {status: 'available', type: 'vip'}
        ],
        [
          {status: 'sold', type: 'standard'},
          {status: 'available', type: 'standard'}
        ]
      ]
    }
  },
  methods: {
    handleSeatClick(row, col) {
      const seat = this.seats[row][col];
      if (seat.status === 'sold') return;

      seat.status = seat.status === 'selected' ? 'available' : 'selected';
      this.$emit('selection-change', this.getSelectedSeats());
    },
    getSelectedSeats() {
      return this.seats.flatMap((row, rowIndex) => 
        row.map((seat, colIndex) => 
          seat.status === 'selected' ? `${rowIndex}-${colIndex}` : null
        ).filter(Boolean)
      );
    }
  }
}
</script>

标签: vue
分享给朋友:

相关文章

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过 @…

vue实现分屏

vue实现分屏

Vue 实现分屏的方法 分屏功能通常用于展示多个独立内容区域,以下是几种常见的实现方式: 使用 CSS Grid 布局 通过 CSS Grid 可以轻松创建分屏布局,在 Vue 组件中直接应用样式:…

vue 实现豆瓣

vue 实现豆瓣

以下是基于 Vue 实现豆瓣电影类功能的实现方案,涵盖核心模块和技术要点: 数据获取与 API 调用 使用豆瓣开放 API(需注意调用频率限制)或第三方代理接口 推荐 axios 进行异步请求,配合…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

vue实现切换

vue实现切换

Vue 实现切换功能的方法 在 Vue 中实现切换功能可以通过多种方式实现,以下是一些常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染实现切换功能,适用于简单的显示/隐藏场景。…