123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <script setup lang="ts">
- import { ContentWrap } from '@/components/ContentWrap'
- import { Search } from '@/components/Search'
- import { Dialog } from '@/components/Dialog'
- import { useI18n } from '@/hooks/web/useI18n'
- import { ElButton, ElTag } from 'element-plus'
- import { Table } from '@/components/Table'
- import { getTableListApi, saveTableApi, delTableListApi } from '@/api/table'
- import { useTable } from '@/hooks/web/useTable'
- import { TableData } from '@/api/table/types'
- import { h, ref, unref, reactive } from 'vue'
- import Write from './components/Write.vue'
- import Detail from './components/Detail.vue'
- import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
- const { register, tableObject, methods } = useTable<TableData>({
- getListApi: getTableListApi,
- delListApi: delTableListApi,
- response: {
- list: 'list',
- total: 'total'
- }
- })
- const { getList, setSearchParams } = methods
- getList()
- const { t } = useI18n()
- const crudSchemas = reactive<CrudSchema[]>([
- {
- field: 'index',
- label: t('tableDemo.index'),
- type: 'index',
- form: {
- show: false
- },
- detail: {
- show: false
- }
- },
- {
- field: 'title',
- label: t('tableDemo.title'),
- search: {
- show: true
- },
- form: {
- colProps: {
- span: 24
- }
- },
- detail: {
- span: 24
- }
- },
- {
- field: 'author',
- label: t('tableDemo.author')
- },
- {
- field: 'display_time',
- label: t('tableDemo.displayTime'),
- form: {
- component: 'DatePicker',
- componentProps: {
- type: 'datetime',
- valueFormat: 'YYYY-MM-DD HH:mm:ss'
- }
- }
- },
- {
- field: 'importance',
- label: t('tableDemo.importance'),
- formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
- return h(
- ElTag,
- {
- type: cellValue === 1 ? 'success' : cellValue === 2 ? 'warning' : 'danger'
- },
- () =>
- cellValue === 1
- ? t('tableDemo.important')
- : cellValue === 2
- ? t('tableDemo.good')
- : t('tableDemo.commonly')
- )
- },
- form: {
- component: 'Select',
- componentProps: {
- options: [
- {
- label: '重要',
- value: 3
- },
- {
- label: '良好',
- value: 2
- },
- {
- label: '一般',
- value: 1
- }
- ]
- }
- }
- },
- {
- field: 'pageviews',
- label: t('tableDemo.pageviews'),
- form: {
- component: 'InputNumber',
- value: 0
- }
- },
- {
- field: 'content',
- label: t('exampleDemo.content'),
- table: {
- show: false
- },
- form: {
- component: 'Editor',
- colProps: {
- span: 24
- }
- },
- detail: {
- span: 24
- }
- },
- {
- field: 'action',
- width: '260px',
- label: t('tableDemo.action'),
- form: {
- show: false
- },
- detail: {
- show: false
- }
- }
- ])
- const { allSchemas } = useCrudSchemas(crudSchemas)
- const dialogVisible = ref(false)
- const dialogTitle = ref('')
- const AddAction = () => {
- dialogTitle.value = t('exampleDemo.add')
- tableObject.currentRow = null
- dialogVisible.value = true
- }
- const delLoading = ref(false)
- const delData = async (row: TableData | null, multiple: boolean) => {
- tableObject.currentRow = row
- const { delList, getSelections } = methods
- const selections = await getSelections()
- delLoading.value = true
- await delList(
- multiple ? selections.map((v) => v.id) : [tableObject.currentRow?.id as string],
- multiple
- ).finally(() => {
- delLoading.value = false
- })
- }
- const actionType = ref('')
- const action = (row: TableData, type: string) => {
- dialogTitle.value = t(type === 'edit' ? 'exampleDemo.edit' : 'exampleDemo.detail')
- actionType.value = type
- tableObject.currentRow = row
- dialogVisible.value = true
- }
- const writeRef = ref<ComponentRef<typeof Write>>()
- const loading = ref(false)
- const save = async () => {
- const write = unref(writeRef)
- await write?.elFormRef?.validate(async (isValid) => {
- if (isValid) {
- loading.value = true
- const data = (await write?.getFormData()) as TableData
- const res = await saveTableApi(data)
- .catch(() => {})
- .finally(() => {
- loading.value = false
- })
- if (res) {
- dialogVisible.value = false
- tableObject.currentPage = 1
- getList()
- }
- }
- })
- }
- </script>
- <template>
- <ContentWrap>
- <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
- <div class="mb-10px">
- <ElButton type="primary" @click="AddAction">{{ t('exampleDemo.add') }}</ElButton>
- <ElButton :loading="delLoading" type="danger" @click="delData(null, true)">
- {{ t('exampleDemo.del') }}
- </ElButton>
- </div>
- <Table
- v-model:pageSize="tableObject.pageSize"
- v-model:currentPage="tableObject.currentPage"
- :columns="allSchemas.tableColumns"
- :data="tableObject.tableList"
- :loading="tableObject.loading"
- :pagination="{
- total: tableObject.total
- }"
- @register="register"
- >
- <template #action="{ row }">
- <ElButton type="primary" @click="action(row, 'edit')">
- {{ t('exampleDemo.edit') }}
- </ElButton>
- <ElButton type="success" @click="action(row, 'detail')">
- {{ t('exampleDemo.detail') }}
- </ElButton>
- <ElButton type="danger" @click="delData(row, false)">
- {{ t('exampleDemo.del') }}
- </ElButton>
- </template>
- </Table>
- </ContentWrap>
- <Dialog v-model="dialogVisible" :title="dialogTitle">
- <Write
- v-if="actionType !== 'detail'"
- ref="writeRef"
- :form-schema="allSchemas.formSchema"
- :current-row="tableObject.currentRow"
- />
- <Detail
- v-if="actionType === 'detail'"
- :detail-schema="allSchemas.detailSchema"
- :current-row="tableObject.currentRow"
- />
- <template #footer>
- <ElButton v-if="actionType !== 'detail'" type="primary" :loading="loading" @click="save">
- {{ t('exampleDemo.save') }}
- </ElButton>
- <ElButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</ElButton>
- </template>
- </Dialog>
- </template>
|