152 lines
3.5 KiB
Vue
Raw Normal View History

2025-07-03 20:19:09 +08:00
<template>
2025-07-09 17:43:57 +08:00
<div class="main__container table">
<header class="table-header">
<n-input-group style="width:440px">
2025-07-11 21:15:05 +08:00
<n-input :style="{ width: '50%' }" placeholder="请输入用户名称" v-model:value="roleName" />
2025-07-09 17:43:57 +08:00
<n-button strong secondary type="primary">
搜索
<template #icon>
<n-icon>
<SearchOutline />
</n-icon>
</template>
</n-button>
</n-input-group>
</header>
<main class="table-main" ref="tableMainRef">
<n-data-table v-if="height" :columns="columns" :data="data" :max-height="`${height}px`"
:min-height="`${height}px`" />
<div class="table-main__pagination">
2025-07-11 23:22:08 +08:00
<n-pagination v-model:page="pagination.page" v-model:page-size="pagination.pageSize" :item-count="data.length"
show-size-picker :page-sizes="[10, 20, 30, 40]">
<template #prefix="{ itemCount, startIndex }">
{{ itemCount }}
</template>
</n-pagination>
2025-07-09 17:43:57 +08:00
</div>
</main>
</div>
2025-07-03 20:19:09 +08:00
</template>
<script setup lang='ts'>
2025-07-09 17:43:57 +08:00
import { SearchOutline } from '@vicons/ionicons5';
2025-07-10 20:05:35 +08:00
import { getUserList } from '@/api/roleApi';
2025-07-10 22:27:33 +08:00
import { DataTableColumns, NButton, useMessage } from 'naive-ui';
2025-07-09 17:43:57 +08:00
2025-07-10 22:27:33 +08:00
const message = useMessage();
2025-07-09 17:43:57 +08:00
const tableMainRef = ref<HTMLElement | null>(null);
2025-07-11 23:22:08 +08:00
const roleName = ref('');
const total = ref(0);
2025-07-10 20:05:35 +08:00
const pagination = reactive<PageController>({
2025-07-09 17:43:57 +08:00
pageSize: 10,
page: 1,
});
2025-07-11 23:22:08 +08:00
const data = ref([]);
const height = ref(null);
2025-07-09 17:43:57 +08:00
2025-07-10 22:27:33 +08:00
// 类似render - 函数式组件渲染
function createColumns({
sendMail
}: {
2025-07-11 23:22:08 +08:00
sendMail: (rowData: UserRaw) => void
}): DataTableColumns<UserRaw> {
2025-07-10 22:27:33 +08:00
return [
{
title: '用户名',
2025-07-11 23:22:08 +08:00
key: 'userName'
2025-07-10 22:27:33 +08:00
},
{
title: '性别',
2025-07-11 23:22:08 +08:00
key: 'sex',
render(row: UserRaw) {
return h('span', {}, {
default: () => `${row.sex || '未知'}`
})
}
2025-07-10 22:27:33 +08:00
},
{
2025-07-11 23:22:08 +08:00
title: '职位',
key: 'jobCode',
render(row: UserRaw) {
return h('span', {}, {
default: () => `${row.jobCode || '无职位'}`
})
}
2025-07-10 22:27:33 +08:00
},
{
2025-07-11 23:22:08 +08:00
title: '菜单权限',
key: 'menuName',
render(row: UserRaw) {
return h('span', {}, {
default: () => `${row.menuName || '无权限'}`
})
}
},
{
title: '操作',
2025-07-10 22:27:33 +08:00
key: 'actions',
2025-07-11 23:22:08 +08:00
render(row: UserRaw) {
2025-07-10 22:27:33 +08:00
return h(
NButton,
{
size: 'small',
onClick: () => sendMail(row)
},
2025-07-11 23:22:08 +08:00
{ default: () => '点击用户' }
2025-07-10 22:27:33 +08:00
)
}
}
]
}
// 生成表格列
const columns = createColumns({
sendMail(rowData) {
2025-07-11 23:22:08 +08:00
message.error(`我点击了${rowData.userName}`);
2025-07-09 17:43:57 +08:00
}
2025-07-10 22:27:33 +08:00
})
2025-07-11 23:22:08 +08:00
2025-07-10 20:05:35 +08:00
const init = async () => {
try {
2025-07-11 23:22:08 +08:00
const { users, totalCount } = await getUserList({
2025-07-11 21:15:05 +08:00
...pagination,
roleName: roleName.value
});
2025-07-11 23:22:08 +08:00
total.value = totalCount;
data.value = users;
2025-07-10 20:05:35 +08:00
} catch (error) {
2025-07-11 21:15:05 +08:00
message.error(error.message);
2025-07-10 20:05:35 +08:00
}
}
2025-07-09 17:43:57 +08:00
onMounted(() => {
if (tableMainRef.value) {
height.value = tableMainRef.value.clientHeight - 64 - 50; //减去两次内边距 - 分页的大小
2025-07-10 20:05:35 +08:00
};
init();
2025-07-09 17:43:57 +08:00
});
2025-07-03 20:19:09 +08:00
</script>
<style scoped lang='scss'>
2025-07-09 17:43:57 +08:00
.table-header {
background: #fff;
border-radius: 4px;
height: 100%;
display: flex;
align-items: center;
padding: 0 $normolGap;
}
.table-main {
background: #fff;
padding: $normolGap;
border-radius: 4px;
height: calc(100% - 2 * $normolGap);
&__pagination {
height: 50px;
display: flex;
align-items: center;
justify-content: flex-end;
}
}
2025-07-03 20:19:09 +08:00
</style>