Skip to content

Commit f4c751d

Browse files
author
xuqingkai
committed
fix: 🐛 修复IndexBar组件更新数据时显示异常的问题
Closes: #545
1 parent 8680e77 commit f4c751d

5 files changed

Lines changed: 236 additions & 38 deletions

File tree

docs/component/index-bar.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,170 @@ const data = ref([
125125
</style>
126126
```
127127

128+
## 更新列表数据
129+
130+
列表数据如果需要更新,可以参考此示例
131+
132+
::: details 查看更新列表数据示例
133+
::: code-group
134+
135+
```html [vue]
136+
<template>
137+
<wd-search hide-cancel placeholder="我要去哪里?" v-model="keyword" @search="handleSearch" @clear="handleClear" />
138+
<view class="wraper">
139+
<wd-index-bar sticky v-if="showList.length">
140+
<view v-for="item in showList" :key="item.index">
141+
<wd-index-anchor :index="item.index" />
142+
<wd-cell border clickable v-for="city in item.data" :key="city" :title="city" @click="handleClick(item.index, city)"></wd-cell>
143+
</view>
144+
</wd-index-bar>
145+
</view>
146+
</template>
147+
```
148+
149+
```typescript [typescript]
150+
<script lang="ts" setup>
151+
import { useToast } from '@/uni_modules/wot-design-uni'
152+
import { nextTick, onMounted, ref } from 'vue'
153+
const { show: showToast } = useToast()
154+
155+
onMounted(() => {
156+
handleSearch()
157+
})
158+
159+
const keyword = ref('')
160+
161+
const showList = ref<any>([])
162+
163+
const indexList = [
164+
{
165+
index: 'A',
166+
data: ['阿坝', '阿拉善', '阿里', '安康', '安庆', '鞍山', '安顺', '安阳', '澳门']
167+
},
168+
{
169+
index: 'B',
170+
data: ['北京', '白银', '保定', '宝鸡', '保山', '包头', '巴中', '北海', '蚌埠', '本溪', '毕节', '滨州', '百色', '亳州']
171+
},
172+
{
173+
index: 'C',
174+
data: [
175+
'重庆',
176+
'成都',
177+
'长沙',
178+
'长春',
179+
'沧州',
180+
'常德',
181+
'昌都',
182+
'长治',
183+
'常州',
184+
'巢湖',
185+
'潮州',
186+
'承德',
187+
'郴州',
188+
'赤峰',
189+
'池州',
190+
'崇左',
191+
'楚雄',
192+
'滁州',
193+
'朝阳'
194+
]
195+
},
196+
{
197+
index: 'D',
198+
data: ['大连', '东莞', '大理', '丹东', '大庆', '大同', '大兴安岭', '德宏', '德阳', '德州', '定西', '迪庆', '东营']
199+
},
200+
{
201+
index: 'E',
202+
data: ['鄂尔多斯', '恩施', '鄂州']
203+
},
204+
{
205+
index: 'F',
206+
data: ['福州', '防城港', '佛山', '抚顺', '抚州', '阜新', '阜阳']
207+
},
208+
{
209+
index: 'G',
210+
data: ['广州', '桂林', '贵阳', '甘南', '赣州', '甘孜', '广安', '广元', '贵港', '果洛']
211+
},
212+
{
213+
index: 'H',
214+
data: [
215+
'杭州',
216+
'哈尔滨',
217+
'合肥',
218+
'海口',
219+
'呼和浩特',
220+
'海北',
221+
'海东',
222+
'海南',
223+
'海西',
224+
'邯郸',
225+
'汉中',
226+
'鹤壁',
227+
'河池',
228+
'鹤岗',
229+
'黑河',
230+
'衡水',
231+
'衡阳',
232+
'河源',
233+
'贺州',
234+
'红河',
235+
'淮安',
236+
'淮北',
237+
'怀化',
238+
'淮南',
239+
'黄冈',
240+
'黄南',
241+
'黄山',
242+
'黄石',
243+
'惠州',
244+
'葫芦岛',
245+
'呼伦贝尔',
246+
'湖州',
247+
'菏泽'
248+
]
249+
}
250+
]
251+
252+
function handleClick(index: string, city: string) {
253+
showToast(`当前点击项:${index},城市:${city}`)
254+
}
255+
256+
function handleSearch() {
257+
showList.value = []
258+
nextTick(() => {
259+
if (keyword.value) {
260+
showList.value = indexList.filter((item) => {
261+
return item.data.some((city) => {
262+
return city.includes(keyword.value)
263+
})
264+
})
265+
} else {
266+
showList.value = indexList
267+
}
268+
})
269+
270+
// 筛选indexList项中data包含keyword的项
271+
}
272+
273+
function handleClear() {
274+
keyword.value = ''
275+
handleSearch()
276+
}
277+
</script>
278+
```
279+
280+
```css [css]
281+
.wraper {
282+
height: calc(100vh - var(--window-top));
283+
height: calc(100vh - var(--window-top) - constant(safe-area-inset-bottom));
284+
height: calc(100vh - var(--window-top) - env(safe-area-inset-bottom));
285+
}
286+
```
287+
288+
:::
289+
290+
291+
128292
## Attributes
129293

130294
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |

src/pages/indexBar/Index.vue

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<template>
22
<page-wraper>
3+
<wd-search hide-cancel placeholder="我要去哪里?" v-model="keyword" @search="handleSearch" @clear="handleClear" />
34
<view class="wraper">
4-
<wd-index-bar sticky>
5-
<view v-for="item in data" :key="item.index">
5+
<wd-index-bar sticky v-if="showList.length">
6+
<view v-for="item in showList" :key="item.index">
67
<wd-index-anchor :index="item.index" />
78
<wd-cell border clickable v-for="city in item.data" :key="city" :title="city" @click="handleClick(item.index, city)"></wd-cell>
89
</view>
@@ -13,10 +14,18 @@
1314

1415
<script lang="ts" setup>
1516
import { useToast } from '@/uni_modules/wot-design-uni'
16-
import { ref } from 'vue'
17+
import { nextTick, onMounted, ref } from 'vue'
1718
const { show: showToast } = useToast()
1819
19-
const data = ref([
20+
onMounted(() => {
21+
handleSearch()
22+
})
23+
24+
const keyword = ref('')
25+
26+
const showList = ref<any>([])
27+
28+
const indexList = [
2029
{
2130
index: 'A',
2231
data: ['阿坝', '阿拉善', '阿里', '安康', '安庆', '鞍山', '安顺', '安阳', '澳门']
@@ -103,11 +112,33 @@ const data = ref([
103112
'菏泽'
104113
]
105114
}
106-
])
115+
]
107116
108117
function handleClick(index: string, city: string) {
109118
showToast(`当前点击项:${index},城市:${city}`)
110119
}
120+
121+
function handleSearch() {
122+
showList.value = []
123+
nextTick(() => {
124+
if (keyword.value) {
125+
showList.value = indexList.filter((item) => {
126+
return item.data.some((city) => {
127+
return city.includes(keyword.value)
128+
})
129+
})
130+
} else {
131+
showList.value = indexList
132+
}
133+
})
134+
135+
// 筛选indexList项中data包含keyword的项
136+
}
137+
138+
function handleClear() {
139+
keyword.value = ''
140+
handleSearch()
141+
}
111142
</script>
112143

113144
<style lang="scss">

src/uni_modules/wot-design-uni/components/wd-index-anchor/wd-index-anchor.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,27 @@ const indexAnchorId = ref<string>(`indexBar${uuid()}`)
2727
2828
const { proxy } = getCurrentInstance()!
2929
30+
const top = ref<number>(0)
31+
3032
const isSticky = computed(() => {
3133
return indexBar && indexBar.props.sticky && indexBar.anchorState.activeIndex === props.index
3234
})
3335
3436
function getInfo() {
3537
getRect(`#${indexAnchorId.value}`, false, proxy).then((res) => {
3638
if (isDef(indexBar)) {
37-
const anchor = indexBar.anchorState.anchorList.find((v) => v.index === props.index)!
38-
anchor.top = Math.floor(res.top!)
39+
top.value = Math.floor(res.top!)
3940
}
4041
})
4142
}
4243
4344
onMounted(() => {
44-
if (isDef(indexBar)) {
45-
indexBar.anchorState.anchorList.push({ top: 0, index: props.index })
46-
}
4745
getInfo()
4846
})
47+
48+
defineExpose({
49+
top
50+
})
4951
</script>
5052

5153
<style lang="scss" scoped>

src/uni_modules/wot-design-uni/components/wd-index-bar/type.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import type { Ref } from 'vue'
21
import type { InjectionKey } from 'vue'
32
import type { ExtractPropTypes } from 'vue'
43
import { makeBooleanProp } from '../common/props'
54

65
export type AnchorIndex = number | string
76

8-
export interface AnchorItem {
9-
top: number
10-
index: AnchorIndex
11-
}
12-
137
export const indexBarProps = {
148
/**
159
* @description 索引是否吸顶
@@ -22,7 +16,6 @@ export type IndexBarProps = ExtractPropTypes<typeof indexBarProps>
2216
export type InderBarProvide = {
2317
props: { sticky?: boolean }
2418
anchorState: {
25-
anchorList: AnchorItem[] // 锚点列表
2619
activeIndex: AnchorIndex | null // 当前激活的索引
2720
}
2821
}

0 commit comments

Comments
 (0)