wangrong
2026-08-30 4c949c0dc9bcae785c281c9d8125295f09810b15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<script setup lang="ts">
import { SpreadSheet, Node } from '@antv/s2'
import { PropType } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { S2Event, SortFuncParam } from '@antv/s2'
import { SortUp, SortDown, Sort } from '@element-plus/icons-vue'
import { cloneDeep } from 'lodash-es'
 
const { t } = useI18n()
const props = defineProps({
  table: {
    type: Object as PropType<SpreadSheet>,
    required: true
  },
  meta: {
    type: Object as PropType<Node>,
    required: true
  }
})
const sortFunc = (sortParams: SortFuncParam) => {
  if (!sortParams.sortMethod) {
    return sortParams.data
  }
  const data = cloneDeep(sortParams.data)
  return data.sort((a, b) => {
    // 总计行放最后
    if (a['SUMMARY']) {
      return 1
    }
    const field = sortParams.sortFieldId
    const aValue = a[field]
    const bValue = b[field]
    if (aValue === bValue) {
      return 0
    }
    if (sortParams.sortMethod === 'asc') {
      if (typeof aValue === 'number') {
        return aValue - bValue
      }
      return aValue < bValue ? -1 : 1
    }
    if (typeof aValue === 'number') {
      return bValue - aValue
    }
    return aValue > bValue ? -1 : 1
  })
}
const sort = (type?) => {
  props.table.updateSortMethodMap(props.meta.field, type, true)
  props.table.emit(S2Event.RANGE_SORT, [
    {
      sortFieldId: props.meta.field,
      sortMethod: type,
      sortFunc
    }
  ])
}
</script>
<template>
  <el-col style="color: black">
    <el-row align="middle">
      <el-icon><SortUp /></el-icon>
      <span class="sort-btn" @click="() => sort('asc')">{{ t('chart.asc') }}</span>
    </el-row>
    <el-row align="middle">
      <el-icon><SortDown /></el-icon>
      <span class="sort-btn" @click="() => sort('desc')">{{ t('chart.desc') }}</span>
    </el-row>
    <el-row align="middle">
      <el-icon><Sort /></el-icon>
      <span class="sort-btn" @click="() => sort()">{{ t('chart.default') }}</span>
    </el-row>
  </el-col>
</template>
<style scoped>
.sort-btn {
  font-size: 14px;
  cursor: pointer;
}
</style>