当前位置:首页 > VUE

vue实现赛程表

2026-01-22 02:09:34VUE

Vue 实现赛程表

数据结构设计

赛程表的核心是赛事数据的组织,通常需要包含比赛时间、对阵双方、比分等信息。可以使用数组对象存储数据,每个对象代表一场比赛。

data() {
  return {
    matches: [
      { id: 1, date: '2023-10-01', teamA: 'Team A', teamB: 'Team B', scoreA: 2, scoreB: 1 },
      { id: 2, date: '2023-10-02', teamA: 'Team C', teamB: 'Team D', scoreA: 0, scoreB: 0 },
      // 更多比赛数据...
    ]
  }
}

基础表格渲染

使用 v-for 指令循环渲染比赛数据,构建基础的赛程表格。

vue实现赛程表

<table class="schedule-table">
  <thead>
    <tr>
      <th>日期</th>
      <th>主队</th>
      <th>比分</th>
      <th>客队</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="match in matches" :key="match.id">
      <td>{{ match.date }}</td>
      <td>{{ match.teamA }}</td>
      <td>{{ match.scoreA }} - {{ match.scoreB }}</td>
      <td>{{ match.teamB }}</td>
    </tr>
  </tbody>
</table>

按日期分组

对于多日赛事,可以按日期分组显示比赛。使用计算属性对数据进行分组处理。

computed: {
  groupedMatches() {
    const groups = {};
    this.matches.forEach(match => {
      if (!groups[match.date]) {
        groups[match.date] = [];
      }
      groups[match.date].push(match);
    });
    return groups;
  }
}
<div v-for="(matches, date) in groupedMatches" :key="date">
  <h3>{{ date }}</h3>
  <table>
    <!-- 表格内容同上 -->
  </table>
</div>

交互功能实现

添加点击事件处理比赛详情展示,使用模态框或展开行显示更多信息。

vue实现赛程表

<tr v-for="match in matches" @click="showDetail(match)">
  <!-- 表格内容 -->
</tr>

<!-- 详情模态框 -->
<dialog v-if="selectedMatch" @click.self="selectedMatch = null">
  <h4>{{ selectedMatch.teamA }} vs {{ selectedMatch.teamB }}</h4>
  <p>日期: {{ selectedMatch.date }}</p>
  <p>比分: {{ selectedMatch.scoreA }} - {{ selectedMatch.scoreB }}</p>
  <button @click="selectedMatch = null">关闭</button>
</dialog>
methods: {
  showDetail(match) {
    this.selectedMatch = match;
  }
}

样式优化

使用 CSS 增强表格的可读性和美观性,添加悬停效果和响应式设计。

.schedule-table {
  width: 100%;
  border-collapse: collapse;
}

.schedule-table th, .schedule-table td {
  padding: 12px;
  text-align: center;
  border: 1px solid #ddd;
}

.schedule-table tr:hover {
  background-color: #f5f5f5;
}

/* 响应式设计 */
@media (max-width: 600px) {
  .schedule-table thead {
    display: none;
  }

  .schedule-table tr {
    display: block;
    margin-bottom: 16px;
  }

  .schedule-table td {
    display: block;
    text-align: right;
  }

  .schedule-table td::before {
    content: attr(data-label);
    float: left;
    font-weight: bold;
  }
}

动态数据加载

对于大型赛事,可以从 API 异步加载数据,并添加加载状态。

data() {
  return {
    matches: [],
    loading: false,
    error: null
  }
},
methods: {
  async fetchMatches() {
    this.loading = true;
    try {
      const response = await fetch('api/matches');
      this.matches = await response.json();
    } catch (err) {
      this.error = err.message;
    } finally {
      this.loading = false;
    }
  }
},
created() {
  this.fetchMatches();
}
<div v-if="loading">加载中...</div>
<div v-else-if="error">错误: {{ error }}</div>
<!-- 正常显示表格 -->

通过以上步骤,可以构建一个功能完整、样式美观的赛程表组件。根据实际需求,还可以添加更多功能如筛选、排序等。

标签: 赛程表vue
分享给朋友:

相关文章

vue设计与实现 书

vue设计与实现 书

vue设计与实现 书 《Vue.js设计与实现》是一本深入探讨Vue.js框架内部原理和设计思想的书籍,适合希望深入理解Vue.js的开发者。以下是关于这本书的详细信息: 书籍内容 《Vue.js…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <template…

vue实现语音

vue实现语音

Vue 实现语音功能的方法 使用 Web Speech API Vue 可以结合 Web Speech API 实现语音识别和合成功能。Web Speech API 包含 SpeechRecognit…

vue实现triger

vue实现triger

在Vue中实现触发器(trigger)功能通常涉及自定义事件或DOM事件触发。以下是几种常见场景的实现方法: 自定义事件触发 通过$emit方法触发父组件中监听的自定义事件: // 子组件 t…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…