当前位置:首页 > VUE

vue点击实现升降排序

2026-01-22 23:47:16VUE

实现点击升降序排序

在Vue中实现点击表格头切换升降序排序,可以通过以下方法完成:

数据准备

data() {
  return {
    items: [
      { id: 1, name: 'Apple', price: 5 },
      { id: 2, name: 'Banana', price: 3 },
      { id: 3, name: 'Orange', price: 4 }
    ],
    sortKey: '',
    sortOrder: 1 // 1升序,-1降序
  }
}

排序方法实现

methods: {
  sortBy(key) {
    if (this.sortKey === key) {
      this.sortOrder *= -1
    } else {
      this.sortKey = key
      this.sortOrder = 1
    }

    this.items.sort((a, b) => {
      if (a[key] < b[key]) return -1 * this.sortOrder
      if (a[key] > b[key]) return 1 * this.sortOrder
      return 0
    })
  }
}

模板中使用

<table>
  <thead>
    <tr>
      <th @click="sortBy('name')">名称</th>
      <th @click="sortBy('price')">价格</th>
    </tr>
  </thead>
  <tbody>
    <tr v-for="item in items" :key="item.id">
      <td>{{ item.name }}</td>
      <td>{{ item.price }}</td>
    </tr>
  </tbody>
</table>

添加排序状态指示

可以添加箭头图标显示当前排序状态

<th @click="sortBy('name')">
  名称
  <span v-if="sortKey === 'name'">
    {{ sortOrder === 1 ? '↑' : '↓' }}
  </span>
</th>

使用计算属性优化

将排序逻辑移至计算属性,避免直接修改原始数据

computed: {
  sortedItems() {
    return [...this.items].sort((a, b) => {
      if (a[this.sortKey] < b[this.sortKey]) return -1 * this.sortOrder
      if (a[this.sortKey] > b[this.sortKey]) return 1 * this.sortOrder
      return 0
    })
  }
}

模板中改为使用sortedItems替代items

封装为可复用组件

可以创建一个可复用的排序表格组件

Vue.component('sortable-table', {
  props: ['data', 'columns'],
  data() {
    return {
      sortKey: '',
      sortOrder: 1
    }
  },
  methods: {
    sortBy(key) {
      if (this.sortKey === key) {
        this.sortOrder *= -1
      } else {
        this.sortKey = key
        this.sortOrder = 1
      }
    }
  },
  computed: {
    sortedData() {
      return [...this.data].sort((a, b) => {
        if (a[this.sortKey] < b[this.sortKey]) return -1 * this.sortOrder
        if (a[this.sortKey] > b[this.sortKey]) return 1 * this.sortOrder
        return 0
      })
    }
  }
})

处理数字和字符串排序

对于数字类型字段,需要特殊处理以确保正确排序

sortBy(key) {
  // 判断字段是否为数字类型
  const isNumeric = this.items.every(item => typeof item[key] === 'number')

  if (this.sortKey === key) {
    this.sortOrder *= -1
  } else {
    this.sortKey = key
    this.sortOrder = 1
  }

  this.items.sort((a, b) => {
    if (isNumeric) {
      return (a[key] - b[key]) * this.sortOrder
    }
    return String(a[key]).localeCompare(String(b[key])) * this.sortOrder
  })
}

vue点击实现升降排序

标签: vue
分享给朋友:

相关文章

vue实现前端分离

vue实现前端分离

Vue 实现前后端分离 前后端分离是一种架构模式,前端负责页面渲染和交互逻辑,后端提供数据接口。Vue.js 作为前端框架,非常适合实现前后端分离的开发模式。 核心实现步骤 创建 Vue 项目 使…

vue实现冰墩墩

vue实现冰墩墩

使用 Vue 实现冰墩墩动画 通过 Vue 的动画特性和 SVG 技术,可以动态渲染冰墩墩的形象。以下是具体实现方法: 准备 SVG 素材 冰墩墩的形象通常由多个 SVG 路径组成,包括身体轮廓…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue实现分屏

vue实现分屏

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