refactor: 分页查询重构

environments/test/deployments/1
saatana 4 years ago
parent bd2695231a
commit d89c18590c

@ -5,32 +5,57 @@
import ElTable from './extra/ElTable.vue'; import ElTable from './extra/ElTable.vue';
import { ElTableColumn } from 'element-plus/es/components/table/index'; import { ElTableColumn } from 'element-plus/es/components/table/index';
const props = defineProps({ const props = defineProps({
/**
* 列表唯一标识
*/
code: { code: {
type: String, type: String,
default: '', required: true,
}, },
/**
* 列表标题
*/
title: { title: {
type: String, type: String,
default: '', default: '',
}, },
/**
* 是否支持拖拽排序
*/
sortable: { sortable: {
type: Boolean, type: Boolean,
default: false, default: false,
}, },
/**
* 列表配置
*/
config: { config: {
type: Object, type: Object,
required: true, required: true,
}, },
/**
* 列表操作
*/
operation: { operation: {
type: Array, type: Array,
default() { default() {
return ['create', 'remove', 'search']; return ['create', 'remove', 'search'];
}, },
}, },
/**
* 列表总数据量
*/
total: { total: {
type: Number, type: Number,
default: 0, default: 0,
}, },
/**
* 重置筛选条件支持异步函数
*/
reset: {
type: Function,
default: () => null,
},
}); });
const emits = defineEmits([ const emits = defineEmits([
'create', 'create',
@ -41,7 +66,6 @@
'selectAll', 'selectAll',
'selectionChange', 'selectionChange',
'currentChange', 'currentChange',
'reset',
'template', 'template',
'import', 'import',
'export', 'export',
@ -188,7 +212,7 @@
const pages = computed(() => store.state.local.listPage); const pages = computed(() => store.state.local.listPage);
const search = ref( const search = ref(
props.code props.code
? store.state.local.listPage[props.code] ? _.cloneDeep(unref(pages)[props.code])
: { : {
pageIndex: 1, pageIndex: 1,
length: 10, length: 10,
@ -200,10 +224,13 @@
length: 10, length: 10,
} }
) => { ) => {
if (props.config.page?.sizes && !props.config.page?.sizes?.includes(page.length)) {
page.length = props.config.page.sizes[0];
}
if (props.code) { if (props.code) {
store.commit('local/setListPage', { store.commit('local/setListPage', {
...unref(pages), ...unref(pages),
[props.code]: page, [props.code]: _.cloneDeep(page),
}); });
} }
search.value = page; search.value = page;
@ -223,10 +250,9 @@
const handleSearch = () => { const handleSearch = () => {
emits('search', unref(search)); emits('search', unref(search));
}; };
const handleReset = () => { const handleReset = async () => {
emits('reset'); await props.reset?.();
search.value = { pageIndex: 1, length: 10 }; search.value = { pageIndex: 1, length: 10 };
handleSearch();
}; };
const handleTemplate = () => { const handleTemplate = () => {
emits('template'); emits('template');
@ -418,7 +444,7 @@
) : ( ) : (
'' ''
)} )}
{props.code ? ( {props.config.setting !== false ? (
<ElButton class="setting-btn" icon="setting" type="text" onClick={() => handleSetting()}> <ElButton class="setting-btn" icon="setting" type="text" onClick={() => handleSetting()}>
<ElIcon name="Setting" /> <ElIcon name="Setting" />
<span>设置</span> <span>设置</span>

@ -4,7 +4,9 @@ const state = () => ({
listSettings: {}, listSettings: {},
listPage: {}, listPage: {},
}); });
const getters = {}; const getters = {
page: (state) => (code) => state.listPage[code] || { pageIndex: 1, length: 10 },
};
const mutations = { const mutations = {
sendMessage: (state) => (state.lastSendMessageTime = new Date().getTime()), sendMessage: (state) => (state.lastSendMessageTime = new Date().getTime()),
setToken: (state, data) => (state.token = data), setToken: (state, data) => (state.token = data),

@ -1,9 +1,10 @@
import * as api from '@/api/system/user.js'; import * as api from '@/api/system/user.js';
import { ElMessage, ElMessageBox } from '@/plugins/element-plus'; import { ElMessage, ElMessageBox } from '@/plugins/element-plus';
const state = () => ({ const state = () => ({
code: 'UserManagement',
condition: {},
list: [], list: [],
total: 0, total: 0,
page: { pageIndex: 1, length: 10 },
opts: { opts: {
init: false, init: false,
sex: [], sex: [],
@ -11,13 +12,15 @@ const state = () => ({
}); });
const getters = {}; const getters = {};
const mutations = { const mutations = {
setCode: (state, data) => (state.code = data),
setCondition: (state, data) => (state.condition = data),
setList: (state, data) => (state.list = data), setList: (state, data) => (state.list = data),
setTotal: (state, data) => (state.total = data), setTotal: (state, data) => (state.total = data),
setOpts: (state, data) => (state.opts = data), setOpts: (state, data) => (state.opts = data),
}; };
const actions = { const actions = {
search: async ({ state, commit }, data = {}) => { search: async ({ state, commit, rootGetters }) => {
let res = await api.findUserList({ ...state.page, ...data }); let res = await api.findUserList({ ...rootGetters['local/page'](state.code), ...state.condition });
if (res) { if (res) {
commit('setList', res.content); commit('setList', res.content);
commit('setTotal', res.totalElements); commit('setTotal', res.totalElements);

@ -1,9 +1,10 @@
<template> <template>
<TableList <TableList
v-loading="loading" v-loading="loading"
code="DemoList" :code="code"
:config="config" :config="config"
:data="list" :data="list"
:reset="handleReset"
title="用户" title="用户"
:total="total" :total="total"
@create="handleCreate" @create="handleCreate"
@ -24,6 +25,7 @@
const router = useRouter(); const router = useRouter();
const store = useStore(); const store = useStore();
const loading = ref(false); const loading = ref(false);
const code = computed(() => store.state.user.code);
const list = computed(() => store.state.user.list); const list = computed(() => store.state.user.list);
const total = computed(() => store.state.user.total); const total = computed(() => store.state.user.total);
const opts = computed(() => store.state.user.opts); const opts = computed(() => store.state.user.opts);
@ -32,9 +34,27 @@
username: null, username: null,
}, },
}); });
const handleSearch = async (page) => { watch(
() => state.condition,
(value) => {
store.commit('user/setCondition', _.cloneDeep(value));
},
{ immediate: true, deep: true }
);
const handleReset = () => {
//
return new Promise((resolve) => {
setTimeout(() => {
state.condition = {
username: null,
};
resolve();
}, 1000);
});
};
const handleSearch = async () => {
loading.value = true; loading.value = true;
await store.dispatch('user/search', { ...page, ...state.condition }); await store.dispatch('user/search');
loading.value = false; loading.value = false;
}; };
const handleCreate = () => { const handleCreate = () => {
@ -56,6 +76,13 @@
console.info((enabled ? 'enabled ' : 'disabled ') + row.id); console.info((enabled ? 'enabled ' : 'disabled ') + row.id);
}; };
const config = reactive({ const config = reactive({
//
setting: false,
// ElTable
table: {},
//
page: { sizes: [20, 100] },
//
columns: [ columns: [
{ {
type: 'selection', type: 'selection',

Loading…
Cancel
Save