wip: crud table

This commit is contained in:
张传龙
2022-08-31 10:16:38 +08:00
parent af983d16b9
commit 9ea8ffd7fd
7 changed files with 168 additions and 18 deletions

View File

@@ -1,24 +1,44 @@
<template>
<CommonPage show-footer title="文章">
<n-data-table
mt-20
:loading="loading"
:scroll-x="1600"
:data="tableData"
:columns="columns"
:pagination="pagination"
:row-key="(row) => row.id"
@update:checked-row-keys="handleCheck"
/>
<template #action>
<n-button type="primary" @click="modalVisible = true">
<TheIcon icon="material-symbols:add" :size="18" class="mr-5" /> 新建文章
</n-button>
</template>
<CrudTable>
<template #queryBar>
<QueryBarItem label="标题" :label-width="50">
<n-input v-model:value="queryForm.title" type="text" placeholder="请输入标题" />
</QueryBarItem>
</template>
<n-data-table
:loading="loading"
:scroll-x="1600"
:data="tableData.filter((item) => item.title.includes(queryForm.title))"
:columns="columns"
:pagination="pagination"
:row-key="(row) => row.id"
@update:checked-row-keys="handleCheck"
/>
<!-- 新增/编辑/查看 -->
<CrudModal v-model:visible="modalVisible" :title="modalTitle" @on-save="handleSave"> 内容 </CrudModal>
</CrudTable>
</CommonPage>
</template>
<script setup>
import { usePostTable } from './usePostTable'
const router = useRouter()
const queryForm = reactive({
title: '',
})
const modalVisible = ref(false)
const modalTitle = ref('新增文章')
const pagination = ref({ pageSize: 10 })
const pagination = reactive({ pageSize: 10 })
const { loading, columns, tableData, initColumns, initTableData } = usePostTable()
onBeforeMount(() => {
@@ -26,8 +46,8 @@ onBeforeMount(() => {
initTableData()
})
function handleCreate() {
router.push('/example/table/post-create')
function handleSave() {
modalVisible.value = false
}
function handleCheck(rowKeys) {

View File

@@ -1,8 +1,8 @@
import { h } from 'vue'
import { NButton, NSwitch } from 'naive-ui'
import { formatDateTime } from '@/utils'
import api from './api'
import { renderIcon } from '@/utils/icon'
import api from './api'
export const usePostTable = () => {
// refs
@@ -31,6 +31,21 @@ export const usePostTable = () => {
}
}
function handleEdit(row) {
if (row && row.id) {
$dialog.confirm({
content: '确定删除?',
confirm() {
$message.success('删除成功')
initTableData()
},
cancel() {
$message.success('已取消')
},
})
}
}
async function handleRecommend(row) {
if (row && row.id) {
row.recommending = true
@@ -84,7 +99,7 @@ export const usePostTable = () => {
{
title: '推荐',
key: 'isRecommend',
width: 100,
width: 120,
align: 'center',
fixed: 'right',
render(row) {
@@ -100,7 +115,7 @@ export const usePostTable = () => {
{
title: '发布',
key: 'isPublish',
width: 100,
width: 120,
align: 'center',
fixed: 'right',
render(row) {
@@ -116,11 +131,20 @@ export const usePostTable = () => {
{
title: '操作',
key: 'actions',
width: 120,
width: 200,
align: 'center',
fixed: 'right',
render(row) {
return [
h(
NButton,
{
size: 'small',
type: 'primary',
onClick: () => handleEdit(row),
},
{ default: () => '编辑', icon: renderIcon('material-symbols:edit-outline', { size: 14 }) }
),
h(
NButton,
{