Skip to content

Commit 98c5fb3

Browse files
authored
Merge pull request #92 from HC200ok/Fix/expand-loading
fix expand row after pageItems updates
2 parents 83cd96c + 007c027 commit 98c5fb3

File tree

7 files changed

+90
-1
lines changed

7 files changed

+90
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"author": "HC200ok",
44
"description": "A customizable and easy-to-use data table component made with Vue.js 3.x.",
55
"private": false,
6-
"version": "1.4.5",
6+
"version": "1.4.11",
77
"types": "./types/main.d.ts",
88
"license": "MIT",
99
"files": [

src/App.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
<ServerSideMode />
33
<br><br>
44
<ClientMode />
5+
<br><br>
6+
<ExpandLoading/>
57
</template>
68

79
<script setup lang="ts">
810
import ServerSideMode from './modes/ServerSide.vue';
911
import ClientMode from './modes/Client.vue';
12+
import ExpandLoading from './modes/ExpandLoading.vue';
1013
</script>

src/components/DataTable.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ const {
421421
const {
422422
expandingItemIndexList,
423423
updateExpandingItemIndexList,
424+
clearExpandingItemIndexList,
424425
} = useExpandableRow(
425426
items,
426427
emits,
@@ -463,6 +464,7 @@ watch(loading, (newVal, oldVal) => {
463464
// in server-side mode, turn to next page when api request finished.
464465
if (newVal === false && oldVal === true) {
465466
updateCurrentPaginationNumber(serverOptionsComputed.value.page);
467+
clearExpandingItemIndexList();
466468
}
467469
}
468470
});
@@ -479,6 +481,10 @@ watch(rowsPerPageRef, (value) => {
479481
}
480482
});
481483
484+
watch([currentPaginationNumber, clientSortOptions, searchField, searchValue, filterOptions], () => {
485+
clearExpandingItemIndexList();
486+
}, { deep: true });
487+
482488
defineExpose({
483489
currentPageFirstIndex,
484490
currentPageLastIndex,
@@ -549,3 +555,4 @@ defineExpose({
549555
height: v-bind(tableHeightPx);
550556
}
551557
</style>
558+

src/hooks/useExpandableRow.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ export default function useExpandableRow(
1919
}
2020
};
2121

22+
const clearExpandingItemIndexList = () => {
23+
expandingItemIndexList.value = [];
24+
};
25+
2226
return {
2327
expandingItemIndexList,
2428
updateExpandingItemIndexList,
29+
clearExpandingItemIndexList,
2530
};
2631
}

src/modes/Client.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
<template>
2+
<span>search field:</span>
3+
<select v-model="searchField">
4+
<option>name</option>
5+
<option>height</option>
6+
</select>
7+
8+
<br/>
9+
10+
<span>search value: </span>
11+
<input type="text" v-model="searchValue">
212
<div>
313
<DataTable
414
ref="dataTable"

src/modes/ExpandLoading.vue

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<template>
2+
<DataTable
3+
:headers="headers"
4+
:items="items"
5+
@expand-row="loadIntroduction"
6+
>
7+
<template #expand="item">
8+
<div
9+
v-if="item.introduction"
10+
style="padding: 15px"
11+
>
12+
{{ item.introduction }}
13+
</div>
14+
</template>
15+
</DataTable>
16+
</template>
17+
18+
<script lang="ts" setup>
19+
import {
20+
computed, ref, reactive, toRefs,
21+
} from 'vue';
22+
import type {
23+
Header, Item, FilterOption, ClickRowArgument, HeaderItemClassNameFunction, BodyItemClassNameFunction, BodyRowClassNameFunction,
24+
} from '../types/main';
25+
import DataTable from '../components/DataTable.vue';
26+
27+
import { mockClientNestedItems, mockClientItems, mockDuplicateClientNestedItems } from '../mock';
28+
const headers: Header[] = [
29+
{ text: 'PLAYER', value: 'player' },
30+
{ text: 'TEAM', value: 'team', sortable: true },
31+
];
32+
33+
const items = ref<Item[]>([
34+
{ player: 'Stephen Curry', team: 'GSW' },
35+
{ player: 'Lebron James', team: 'LAL' },
36+
{ player: 'Kevin Durant', team: 'BKN' },
37+
{ player: 'Giannis Antetokounmpo', team: 'MIL' },
38+
]);
39+
40+
const mockItemIntroduction = async (name: string): Promise<string> => {
41+
await new Promise((s) => setTimeout(s, 2000));
42+
const introduction: Record<string, string> = {
43+
'Stephen Curry': 'Wardell Stephen Curry II is an American professional basketball player for the Golden State Warriors of the National Basketball Association (NBA).',
44+
'Lebron James': 'LeBron Raymone James Sr is an American professional basketball player for the Los Angeles Lakers of the National Basketball Association (NBA).',
45+
'Kevin Durant': 'Kevin Wayne Durant also known by his initials KD, is an American professional basketball player for the Brooklyn Nets of the National Basketball Association (NBA).',
46+
'Giannis Antetokounmpo': 'Giannis Sina Ugo Antetokounmpo (né Adetokunbo; December 6, 1994) is a Greek-Nigerian professional basketball player for the Milwaukee Bucks of the National Basketball Association (NBA).',
47+
};
48+
return introduction[name];
49+
};
50+
51+
const loadIntroduction = async (index: number): Promise<void> => {
52+
const expandedItem = items.value[index];
53+
if (!expandedItem.introduction) {
54+
expandedItem.expandLoading = true;
55+
expandedItem.introduction = await mockItemIntroduction(expandedItem.player);
56+
expandedItem.expandLoading = false;
57+
}
58+
};
59+
</script>

src/modes/ServerSide.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
theme-color="#1d90ff"
2626
border-cell
2727
>
28+
<template #expand="item">
29+
<div style="padding: 15px">
30+
{{ item.name }} won championships
31+
</div>
32+
</template>
2833
<template #address="{ address }">
2934
<a :href="address">{{ address }}</a>
3035
</template>

0 commit comments

Comments
 (0)