当前位置:首页 > 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):

.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
分享给朋友:

相关文章

django vue实现

django vue实现

Django与Vue.js整合实现方案 Django作为后端框架与Vue.js作为前端框架的整合,可以通过以下方式实现: 分离式开发 前后端完全分离,Django仅提供API接口,Vue.js通过a…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

实现vue cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue轮播实现

vue轮播实现

Vue 轮播实现方法 使用第三方库(推荐) 安装 swiper 或 vue-awesome-swiper 库,快速实现功能丰富的轮播效果。 以 vue-awesome-swiper 为例: n…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可…

vue实现页面

vue实现页面

Vue 实现页面的基本方法 创建 Vue 项目 使用 Vue CLI 快速初始化项目,运行以下命令安装并创建项目: npm install -g @vue/cli vue create my-pro…