Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xnPageSelector分页问题修复 #9

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions snowy-admin-web/src/components/XnPageSelect/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
</a-spin>
</template>

<script setup name="xnPageSelector">
import { cloneDeep } from 'lodash-es'
<script setup>
import { watch } from 'vue'

const current = ref(1) // 当前页数
Expand Down Expand Up @@ -60,24 +59,23 @@
// 请求数据
const onPage = (param = {}) => {
if (props.pageFunction) {
initParams.value = param
initParams.value.size = props.pageSize
initParams.value = { ...initParams.value, ...param, size: props.pageSize };
// 加载API
spinning.value = true
props
.pageFunction(initParams.value)
spinning.value = true;
props.pageFunction(initParams.value)
.then((data) => {
initParams.value.current = data.current
// 更新当前页码
initParams.value.current = data.current;
// 加载完后设置总数
total.value = data.total
options.value = data.records
queryEcho()
total.value = data.total;
options.value = data.records;
queryEcho();
})
.finally(() => {
spinning.value = false
})
spinning.value = false;
});
}
}
};
const queryEcho = () => {
// 如果带有查询的方法,那么我们去给查询回显
if (props.echoFunction) {
Expand All @@ -90,7 +88,7 @@
}
props.echoFunction(param).then((data) => {
if (data[0]){
options.value.push(data[0])
options.value.unshift(data[0])
}
})
}
Expand Down Expand Up @@ -119,27 +117,29 @@
const handlePagination = () => {
// 判断已有数量是否小于总量
if (options.value.length < total.value) {
const param = cloneDeep(initParams.value)
param.current = initParams.value.current + 1
spinning.value = true
props
.pageFunction(param)
const param = { ...initParams.value, current: initParams.value.current + 1 };
spinning.value = true;
props.pageFunction(param)
.then((data) => {
if (data.records.length > 0) {
options.value = [...cloneDeep(options.value), ...data.records].filter((item, index, self) => {
return (
self.findIndex((f) => {
return f.id === item.id
}) === index
)
})
// 更新当前页码
initParams.value.current = data.current;
// 合并新旧数据
const newOptions = [...options.value, ...data.records];
// 使用 id 去重
const uniqueOptions = newOptions.reduce((acc, cur) => {
acc[cur.id] = cur;
return acc;
}, {});
options.value = Object.values(uniqueOptions);
}
})
.finally(() => {
spinning.value = false
})
spinning.value = false;
});
}
}
};

defineExpose({
onPage
})
Expand Down