当前位置:首页 > VUE

vue实现表格刷新

2026-01-16 22:17:44VUE

Vue 实现表格刷新的方法

使用 v-if 强制重新渲染

通过 v-if 控制表格的显示与隐藏,切换时 Vue 会重新渲染组件。

<template>
  <button @click="refreshTable">刷新表格</button>
  <table v-if="showTable">
    <!-- 表格内容 -->
  </table>
</template>

<script>
export default {
  data() {
    return {
      showTable: true
    }
  },
  methods: {
    refreshTable() {
      this.showTable = false
      this.$nextTick(() => {
        this.showTable = true
      })
    }
  }
}
</script>

修改 key 强制更新

为表格绑定动态 key,改变 key 值会触发组件重新渲染。

<template>
  <button @click="refreshTable">刷新表格</button>
  <table :key="tableKey">
    <!-- 表格内容 -->
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableKey: 0
    }
  },
  methods: {
    refreshTable() {
      this.tableKey += 1
    }
  }
}
</script>

通过数据更新驱动刷新

直接更新表格绑定的数据源,Vue 的响应式系统会自动更新视图。

<template>
  <button @click="refreshTable">刷新表格</button>
  <table>
    <tr v-for="item in tableData" :key="item.id">
      <td>{{ item.name }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    async refreshTable() {
      const res = await fetch('/api/data')
      this.tableData = await res.json()
    }
  }
}
</script>

使用 $forceUpdate 方法

强制组件重新渲染,适用于数据更新但视图未响应的情况。

<template>
  <button @click="refreshTable">刷新表格</button>
  <table>
    <!-- 表格内容 -->
  </table>
</template>

<script>
export default {
  methods: {
    refreshTable() {
      this.$forceUpdate()
    }
  }
}
</script>

结合 Vuex 状态管理

通过 Vuex 管理表格数据,提交 mutation 或 action 更新数据。

// store.js
export default new Vuex.Store({
  state: {
    tableData: []
  },
  mutations: {
    updateTableData(state, payload) {
      state.tableData = payload
    }
  },
  actions: {
    async fetchTableData({ commit }) {
      const res = await fetch('/api/data')
      commit('updateTableData', await res.json())
    }
  }
})
<template>
  <button @click="refreshTable">刷新表格</button>
  <table>
    <tr v-for="item in $store.state.tableData" :key="item.id">
      <td>{{ item.name }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  methods: {
    refreshTable() {
      this.$store.dispatch('fetchTableData')
    }
  }
}
</script>

注意事项

  • 数据驱动更新是 Vue 推荐的方式,优先考虑通过修改数据实现刷新
  • v-ifkey 方法会触发组件完全重新渲染,可能影响性能
  • $forceUpdate 不会更新子组件,仅强制当前组件重新渲染
  • 大型项目建议使用 Vuex 或 Pinia 集中管理状态

vue实现表格刷新

标签: 表格vue
分享给朋友:

相关文章

vue实现文档

vue实现文档

Vue 实现文档的方法 在 Vue 中实现文档功能可以通过多种方式,包括使用富文本编辑器、Markdown 解析或直接渲染 HTML。以下是几种常见的实现方法。 使用富文本编辑器 富文本编辑器如…

vue实现单词反转

vue实现单词反转

实现思路 在Vue中实现单词反转功能,可以通过多种方式完成。核心思路是利用JavaScript的字符串处理方法,将输入的单词或句子拆分为数组,反转数组后再拼接成字符串。 方法一:使用计算属性 通过…

vue循环数组实现

vue循环数组实现

vue循环数组的实现方法 在Vue中循环数组主要通过v-for指令实现,以下是几种常见用法: 基本数组循环 使用v-for指令遍历数组,可以通过(item, index)的语法获取当前项和索引值:…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件: Vu…

国外vue实现表格控件

国外vue实现表格控件

Vue 表格控件实现方案 使用开源组件库(推荐) Element UI、Vuetify、Ant Design Vue 等主流库提供成熟的表格组件,支持排序、分页、筛选等功能。 Element U…

vue实现字母添加排序

vue实现字母添加排序

实现字母添加排序的方法 在Vue中实现字母添加排序功能,可以通过以下步骤完成: 创建Vue实例并定义数据 new Vue({ el: '#app', data: { letters…