136 lines
3.1 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">
<n-pagination v-model:page="pagination.page" v-model:page-size="pagination.pageSize" :page-count="data.length"
show-size-picker :page-sizes="[10, 20, 30, 40]" />
</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 21:15:05 +08:00
const roleName = ref('admin');
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-10 22:27:33 +08:00
// 列接口数据
2025-07-11 21:15:05 +08:00
interface RowData {
name: string
sex: 'female' | 'male' | 'unknown',
address: string
2025-07-10 22:27:33 +08:00
}
// 类似render - 函数式组件渲染
function createColumns({
sendMail
}: {
sendMail: (rowData: RowData) => void
}): DataTableColumns<RowData> {
return [
{
title: '用户名',
key: 'name'
},
{
title: '性别',
key: 'sex'
},
{
title: 'Address',
key: 'address'
},
{
title: 'Action',
key: 'actions',
render(row) {
return h(
NButton,
{
size: 'small',
onClick: () => sendMail(row)
},
{ default: () => 'Send Email' }
)
}
}
]
}
// 生成表格列
const columns = createColumns({
sendMail(rowData) {
message.error(`我点击了${rowData.name}`);
2025-07-09 17:43:57 +08:00
}
2025-07-10 22:27:33 +08:00
})
2025-07-09 17:43:57 +08:00
const height = ref(null);
2025-07-10 22:27:33 +08:00
const data = Array.from({ length: 10 }).map((_, index) => ({
2025-07-09 17:43:57 +08:00
key: index,
name: `Edward King ${index}`,
2025-07-10 22:27:33 +08:00
sex: 'female',
2025-07-09 17:43:57 +08:00
address: `London, Park Lane no. ${index}`
}));
2025-07-10 20:05:35 +08:00
const init = async () => {
try {
2025-07-11 21:15:05 +08:00
const result = await getUserList({
...pagination,
roleName: roleName.value
});
2025-07-10 22:27:33 +08:00
console.log(result, 'result');
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>