Allen's blog Allen's blog
首页
面经
算法 (opens new window)
分类

Allen

前端CV工程师
首页
面经
算法 (opens new window)
分类
  • Javascript

  • TypeScript

  • CSS

  • Vue

  • React

  • 框架和构建工具

  • 工具库

  • 常见业务场景

    • 事件委托
    • 滚动条闪动解决方案
    • 列表拖拽排序
    • linear-gradient背景渐变色
    • 表格组件数据加载,排序等
    • 时间业务
    • 滚动加载
    • 文件下载
  • Bug

  • 项目实战

  • 前端
  • 常见业务场景
Allen
2023-09-18

表格组件数据加载,排序等

待总结...

<script setup lang="tsx">
const props = defineProps<{
  fundTypeCode: number | ''
  keyword: string
  selectedList: Fund[]
  handleCheck: (fund: Fund) => void
}>()

const page = ref(1)
const order = ref('chg_rate_nav')
const orderType = ref(-1)
const loading = ref(true)
const originTableData = ref([])
const initInfiniteScroll = ref(false)
const requestKey = ref(0)
const isLoadMore = ref(true)

const defaultSorter = computed(() => {
  let innerOrder
  if (orderType.value === -1) {
    innerOrder = 'desc'
  } else if (orderType.value === 1) {
    innerOrder = 'asc'
  }
  return { field: order.value, order: innerOrder }
})

watch([() => props.fundTypeCode, () => props.keyword], () => {
  order.value = undefined
  orderType.value = undefined
  isLoadMore.value = false
  loadData(requestKey.value++)
})

loadData(requestKey.value++)

// 排序
const handleSortChange = (el: TableChangeEvent) => {
  order.value = el.sorter.field
  if (el.sorter.order === 'desc') {
    orderType.value = -1
  } else if (el.sorter.order === 'asc') {
    orderType.value = 1
  } else {
    orderType.value = undefined
  }
  page.value = 1
  isLoadMore.value = false
  loadData(requestKey.value++)
}
const goToDetail = (fundId: number) => {
  const path = `/#/xxx?fundId=${fundId}`
  if (isApp()) {
    invoke('comprehensiveBridge', {
      type: 'openNewWebView',
      url: `${window.location.origin}${path}`,
    })
  } else {
    // router.push(path)
  }
}

const columns = [...]

function loadData(key: number) {
  loading.value = true
  getFunds({
    rows: 20,
    page: page.value,
    order: order.value,
    order_type: orderType.value,
    fund_type_level1_zyyx_code: props.fundTypeCode,
    keyword: props.keyword,
  })
    .then((res) => {
      if (key + 1 !== requestKey.value) return
      console.log({ '请求到公募列表数据:': res })
      if (!res?.data || !Array.isArray(res.data)) {
        if (
          res?.code === 1001 &&
          res?.message === '此次请求没有查询到相应内容'
        ) {
          console.log('没有搜索到相关内容')
          originTableData.value = []
        }
        return Promise.reject(new Error('数据结构不对'))
      }
      if (isLoadMore.value) {
        originTableData.value = [...originTableData.value, ...res.data]
      } else {
        const el = document.querySelector('.st-table-scroll-col')
        el && el?.scrollTo(0, 0)
        originTableData.value = res.data
        isLoadMore.value = true
      }
      if (!initInfiniteScroll.value) {
        initInfiniteScroll.value = true
        nextTick(() => {
          const el = document.querySelector(
            '.st-table-scroll-col',
          ) as HTMLElement
          useInfiniteScroll(
            el,
            () => {
              if (loading.value || el.clientHeight === el.scrollHeight) return

              page.value++
              loadData(requestKey.value++)
            },
            { distance: 10 },
          )
        })
      }
    })
    .catch((e) => {
      console.log(e)
    })
    .finally(() => {
      loading.value = false
    })
}
</script>
<template>
    <div class="flex flex-1 flex-col overflow-auto">
        <Spin
            v-if="loading"
            type="normal"
            class="fixed left-0 top-0 z-[110] flex h-full w-full items-center justify-center overflow-hidden"
        />
        <GDMTable
            v-if="originTableData.length !== 0"
            class="flex flex-1 flex-col overflow-auto"
            :data-source="originTableData"
            :columns="columns"
            :default-sorter="defaultSorter"
            @change="handleSortChange"
        />
        <GDMEmpty
            v-if="!loading && originTableData.length === 0"
            :image="emptyImg"
            :image-style="{
                marginTop: '88px',
                width: '212px',
                height: '140px'
            }"
        >
            <template #description>
                <template v-if="keyword"
                    >未找到包含“{{ keyword }}”的内容</template
                >
                <template v-else>暂无数据</template>
            </template>
            <template #prompt>
                <template v-if="keyword">换个词试试吧</template>
            </template>
        </GDMEmpty>
    </div>
</template>
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
上次更新: 2023/12/16, 09:22:46
linear-gradient背景渐变色
时间业务

← linear-gradient背景渐变色 时间业务→

最近更新
01
rollup使用配置文件rollup.config.ts打包
12-08
02
package.json导出类型
12-08
03
关键问题方案
11-17
更多文章>
Theme by Vdoing | Copyright © 2023-2023 Allen | Github
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式