feat: dict vxe

This commit is contained in:
xingyu4j 2022-11-22 14:34:21 +08:00
parent 22748c5aa8
commit 42eb550858
3 changed files with 217 additions and 316 deletions

View File

@ -2,131 +2,100 @@ import { reactive } from 'vue'
import { DICT_TYPE } from '@/utils/dict' import { DICT_TYPE } from '@/utils/dict'
import { required } from '@/utils/formRules' import { required } from '@/utils/formRules'
import { useI18n } from '@/hooks/web/useI18n' import { useI18n } from '@/hooks/web/useI18n'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas' import { VxeCrudSchema, useVxeCrudSchemas } from '@/hooks/web/useVxeCrudSchemas'
// 国际化 // 国际化
const { t } = useI18n() const { t } = useI18n()
// 表单校验 // 表单校验
export const dictDataRules = reactive({ export const dictDataRules = reactive({
label: [required], title: [required],
value: [required], value: [required],
sort: [required] sort: [required]
}) })
// crudSchemas // crudSchemas
export const crudSchemas = reactive<CrudSchema[]>([ export const crudSchemas = reactive<VxeCrudSchema>({
{ primaryKey: 'id',
label: t('common.index'), primaryType: 'seq',
field: 'id', action: true,
type: 'index', actionWidth: '140px',
form: { columns: [
show: false {
title: '字典类型',
field: 'dictType',
isTable: false,
isForm: false
}, },
detail: { {
show: false title: '数据标签',
} field: 'label',
}, isSearch: true
{
label: '字典类型',
field: 'dictType',
table: {
show: false
}, },
form: { {
show: false title: '数据键值',
} field: 'value'
},
{
label: '数据标签',
field: 'label',
search: {
show: true
}
},
{
label: '数据键值',
field: 'value'
},
{
label: '颜色类型',
field: 'colorType',
form: {
component: 'Select',
componentProps: {
options: [
{
label: 'default',
value: ''
},
{
label: 'success',
value: 'success'
},
{
label: 'info',
value: 'info'
},
{
label: 'warning',
value: 'warning'
},
{
label: 'danger',
value: 'danger'
}
]
}
}, },
table: { {
show: false title: '颜色类型',
} field: 'colorType',
}, form: {
{ component: 'Select',
label: 'CSS Class', componentProps: {
field: 'cssClass', options: [
table: { {
show: false title: 'default',
} value: ''
}, },
{ {
label: '显示排序', title: 'success',
field: 'sort', value: 'success'
table: { },
show: false {
} title: 'info',
}, value: 'info'
{ },
label: t('common.status'), {
field: 'status', title: 'warning',
dictType: DICT_TYPE.COMMON_STATUS, value: 'warning'
dictClass: 'number' },
}, {
{ title: 'danger',
label: t('form.remark'), value: 'danger'
field: 'remark', }
form: { ]
component: 'Input', }
componentProps: {
type: 'textarea',
rows: 4
}, },
colProps: { isTable: false
span: 24
}
}, },
table: { {
show: false title: 'CSS Class',
} field: 'cssClass',
}, isTable: false
{
label: t('table.action'),
field: 'action',
width: '180px',
form: {
show: false
}, },
detail: { {
show: false title: '显示排序',
field: 'sort',
isTable: false
},
{
title: t('common.status'),
field: 'status',
dictType: DICT_TYPE.COMMON_STATUS,
dictClass: 'number'
},
{
title: t('form.remark'),
field: 'remark',
form: {
component: 'Input',
componentProps: {
type: 'textarea',
rows: 4
},
colProps: {
span: 24
}
},
isTable: false
} }
} ]
]) })
export const { allSchemas } = useVxeCrudSchemas(crudSchemas)
export const { allSchemas } = useCrudSchemas(crudSchemas)

View File

@ -2,96 +2,63 @@ import { reactive } from 'vue'
import { DICT_TYPE } from '@/utils/dict' import { DICT_TYPE } from '@/utils/dict'
import { required } from '@/utils/formRules' import { required } from '@/utils/formRules'
import { useI18n } from '@/hooks/web/useI18n' import { useI18n } from '@/hooks/web/useI18n'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas' import { VxeCrudSchema, useVxeCrudSchemas } from '@/hooks/web/useVxeCrudSchemas'
// 国际化 const { t } = useI18n() // 国际化
const { t } = useI18n()
// 表单校验 // 表单校验
export const dictTypeRules = reactive({ export const dictTypeRules = reactive({
name: [required], name: [required],
type: [required] type: [required]
}) })
// 新增 + 修改 // 新增 + 修改
const crudSchemas = reactive<CrudSchema[]>([ const crudSchemas = reactive<VxeCrudSchema>({
{ primaryKey: 'id',
label: t('common.index'), primaryType: 'seq',
field: 'id', action: true,
type: 'index', actionWidth: '140px',
form: { columns: [
show: false {
title: '字典名称',
field: 'name',
isSearch: true
}, },
detail: { {
show: false title: '字典类型',
} field: 'type',
}, isSearch: true
{
label: '字典名称',
field: 'name',
search: {
show: true
}
},
{
label: '字典类型',
field: 'type',
search: {
show: true
}
},
{
label: t('common.status'),
field: 'status',
dictType: DICT_TYPE.COMMON_STATUS,
dictClass: 'number'
},
{
label: t('common.createTime'),
field: 'createTime',
table: {
show: false
}, },
form: { {
show: false title: t('common.status'),
field: 'status',
dictType: DICT_TYPE.COMMON_STATUS,
dictClass: 'number',
table: {
width: 70
}
}, },
detail: { {
show: false title: t('common.createTime'),
field: 'createTime',
formatter: 'formatDate',
isForm: false,
table: {
width: 150
}
}, },
search: { {
show: true, title: t('form.remark'),
component: 'DatePicker', field: 'remark',
componentProps: { isTable: false,
type: 'datetimerange', form: {
valueFormat: 'YYYY-MM-DD HH:mm:ss', componentProps: {
defaultTime: [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)] type: 'textarea',
rows: 4
},
colProps: {
span: 24
}
} }
} }
}, ]
{ })
label: t('form.remark'), export const { allSchemas } = useVxeCrudSchemas(crudSchemas)
field: 'remark',
table: {
show: false
},
form: {
componentProps: {
type: 'textarea',
rows: 4
},
colProps: {
span: 24
}
}
},
{
field: 'action',
width: '180px',
label: t('table.action'),
form: {
show: false
},
detail: {
show: false
}
}
])
export const { allSchemas } = useCrudSchemas(crudSchemas)

View File

@ -6,54 +6,36 @@
<span>字典分类</span> <span>字典分类</span>
</div> </div>
</template> </template>
<Search <vxe-grid
:schema="DictTypeSchemas.allSchemas.searchSchema" ref="xTypeGrid"
@search="setTypeSearchParams" v-bind="typeGridOptions"
@reset="setTypeSearchParams" @cell-click="cellClickEvent"
/> class="xtable-scrollbar"
<!-- 操作工具栏 -->
<div class="mb-10px">
<XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['system:dict:create']"
@click="handleTypeCreate()"
/>
</div>
<!-- 列表 -->
<Table
@row-click="onClickType"
:columns="DictTypeSchemas.allSchemas.tableColumns"
:selection="false"
:data="typeTableObject.tableList"
:loading="typeTableObject.loading"
:pagination="{
total: typeTableObject.total
}"
:highlight-current-row="true"
v-model:pageSize="typeTableObject.pageSize"
v-model:currentPage="typeTableObject.currentPage"
@register="typeRegister"
> >
<template #status="{ row }"> <template #toolbar_buttons>
<DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" /> <XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['system:dict:create']"
@click="handleTypeCreate()"
/>
</template> </template>
<template #action="{ row }"> <template #actionbtns_default="{ row }">
<XTextButton <XTextButton
preIcon="ep:edit" preIcon="ep:edit"
:title="t('action.edit')" :title="t('action.edit')"
v-hasPermi="['system:dict:update']" v-hasPermi="['system:dict:update']"
@click="handleTypeUpdate(row)" @click="handleTypeUpdate(row.id)"
/> />
<XTextButton <XTextButton
preIcon="ep:delete" preIcon="ep:delete"
:title="t('action.del')" :title="t('action.del')"
v-hasPermi="['system:dict:delete']" v-hasPermi="['system:dict:delete']"
@click="delTypeList(row.id, false)" @click="handleTypeDelete(row.id)"
/> />
</template> </template>
</Table> </vxe-grid>
</el-card> </el-card>
<el-card class="w-1/2 dict" style="margin-left: 10px" :gutter="12" shadow="hover"> <el-card class="w-1/2 dict" style="margin-left: 10px" :gutter="12" shadow="hover">
<template #header> <template #header>
@ -66,51 +48,32 @@
<span>请从左侧选择</span> <span>请从左侧选择</span>
</div> </div>
<div v-if="tableTypeSelect"> <div v-if="tableTypeSelect">
<Search <!-- 列表 -->
:schema="DictDataSchemas.allSchemas.searchSchema" <vxe-grid ref="xDataGrid" v-bind="dataGridOptions" class="xtable-scrollbar">
@search="setDataSearchParams" <template #toolbar_buttons>
@reset="setDataSearchParams" <XButton
/> type="primary"
<!-- 操作工具栏 --> preIcon="ep:zoom-in"
<div class="mb-10px"> :title="t('action.add')"
<XButton v-hasPermi="['system:dict:create']"
type="primary" @click="handleDataCreate()"
preIcon="ep:zoom-in" />
:title="t('action.add')"
v-hasPermi="['system:dict:create']"
@click="handleDataCreate()"
/>
</div>
<Table
:columns="DictDataSchemas.allSchemas.tableColumns"
:selection="false"
:data="dataTableObject.tableList"
:loading="dataTableObject.loading"
:pagination="{
total: dataTableObject.total
}"
v-model:pageSize="dataTableObject.pageSize"
v-model:currentPage="dataTableObject.currentPage"
@register="dataRegister"
>
<template #status="{ row }">
<DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
</template> </template>
<template #action="{ row }"> <template #actionbtns_default="{ row }">
<XTextButton <XTextButton
v-hasPermi="['system:dict:update']" v-hasPermi="['system:dict:update']"
preIcon="ep:edit" preIcon="ep:edit"
:title="t('action.edit')" :title="t('action.edit')"
@click="handleDataUpdate(row)" @click="handleDataUpdate(row.id)"
/> />
<XTextButton <XTextButton
v-hasPermi="['system:dict:delete']" v-hasPermi="['system:dict:delete']"
preIcon="ep:delete" preIcon="ep:delete"
:title="t('action.del')" :title="t('action.del')"
@click="delDataList(row.id, false)" @click="handleDataDelete(row.id)"
/> />
</template> </template>
</Table> </vxe-grid>
</div> </div>
</el-card> </el-card>
<XModal id="dictModel" v-model="dialogVisible" :title="dialogTitle"> <XModal id="dictModel" v-model="dialogVisible" :title="dialogTitle">
@ -155,82 +118,81 @@
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, unref, onMounted } from 'vue' import { ref, unref, reactive } from 'vue'
import { DICT_TYPE } from '@/utils/dict'
import { useI18n } from '@/hooks/web/useI18n' import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
import { VxeGridInstance, VxeTableEvents } from 'vxe-table'
import { FormExpose } from '@/components/Form' import { FormExpose } from '@/components/Form'
import { ElInput, ElTag } from 'element-plus' import { ElInput, ElTag, ElCard } from 'element-plus'
import * as DictTypeSchemas from './dict.type' import * as DictTypeSchemas from './dict.type'
import * as DictDataSchemas from './dict.data' import * as DictDataSchemas from './dict.data'
import { useTable } from '@/hooks/web/useTable'
import { ElCard, ElMessage } from 'element-plus'
import * as DictTypeApi from '@/api/system/dict/dict.type' import * as DictTypeApi from '@/api/system/dict/dict.type'
import * as DictDataApi from '@/api/system/dict/dict.data' import * as DictDataApi from '@/api/system/dict/dict.data'
import { DictDataVO, DictTypeVO } from '@/api/system/dict/types' import { DictDataVO, DictTypeVO } from '@/api/system/dict/types'
const { t } = useI18n() // const { t } = useI18n() //
// ========== ========== const message = useMessage() //
const xTypeGrid = ref<VxeGridInstance>() // Grid Ref
const { const {
register: typeRegister, gridOptions: typeGridOptions,
tableObject: typeTableObject, getList: typeGetList,
methods: typeMethods deleteData: typeDeleteData
} = useTable<DictTypeVO>({ } = useVxeGrid<DictTypeVO>({
allSchemas: DictTypeSchemas.allSchemas,
getListApi: DictTypeApi.getDictTypePageApi, getListApi: DictTypeApi.getDictTypePageApi,
delListApi: DictTypeApi.deleteDictTypeApi deleteApi: DictTypeApi.deleteDictTypeApi
})
const xDataGrid = ref<VxeGridInstance>() // Grid Ref
const queryParams = reactive({
dictType: null
}) })
const { const {
getList: getTypeList, gridOptions: dataGridOptions,
setSearchParams: setTypeSearchParams, getList: dataGetList,
delList: delTypeList deleteData: dataDeleteData
} = typeMethods } = useVxeGrid<DictDataVO>({
allSchemas: DictDataSchemas.allSchemas,
queryParams: queryParams,
getListApi: DictDataApi.getDictDataPageApi,
deleteApi: DictDataApi.deleteDictDataApi
})
// ========== ==========
const dictTypeValue = ref('') const dictTypeValue = ref('')
// //
const handleTypeCreate = () => { const handleTypeCreate = () => {
dictTypeValue.value = '' dictTypeValue.value = ''
setDialogTile('typeCreate') setDialogTile('typeCreate')
} }
const handleTypeUpdate = async (row: DictTypeVO) => { const handleTypeUpdate = async (rowId: number) => {
setDialogTile('typeUpdate') setDialogTile('typeUpdate')
// //
const res = await DictTypeApi.getDictTypeApi(row.id) const res = await DictTypeApi.getDictTypeApi(rowId)
dictTypeValue.value = res.type dictTypeValue.value = res.type
unref(typeFormRef)?.setValues(res) unref(typeFormRef)?.setValues(res)
} }
// ========== ==========
const tableTypeSelect = ref(false)
const {
register: dataRegister,
tableObject: dataTableObject,
methods: dataMethods
} = useTable<DictDataVO>({
getListApi: DictDataApi.getDictDataPageApi,
delListApi: DictDataApi.deleteDictDataApi
})
const {
getList: getDataList,
setSearchParams: setDataSearchParams,
delList: delDataList
} = dataMethods
// //
const handleDataCreate = () => { const handleDataCreate = () => {
setDialogTile('dataCreate') setDialogTile('dataCreate')
} }
const handleDataUpdate = async (row: DictDataVO) => { const handleDataUpdate = async (rowId: number) => {
setDialogTile('dataUpdate') setDialogTile('dataUpdate')
// //
const res = await DictDataApi.getDictDataApi(row.id) const res = await DictDataApi.getDictDataApi(rowId)
unref(dataFormRef)?.setValues(res) unref(dataFormRef)?.setValues(res)
} }
// //
const parentType = ref('') const parentType = ref('')
const onClickType = async (data: { [key: string]: any }) => { const tableTypeSelect = ref(false)
const cellClickEvent: VxeTableEvents.CellClick = async ({ row }) => {
console.info(row['type'])
tableTypeSelect.value = true tableTypeSelect.value = true
dataTableObject.params = { queryParams.dictType = row['type']
dictType: data.type await dataGetList(xDataGrid)
} parentType.value = row['type']
getDataList()
parentType.value = data.type
} }
// //
const dialogVisible = ref(false) // const dialogVisible = ref(false) //
@ -246,6 +208,16 @@ const setDialogTile = (type: string) => {
actionType.value = type actionType.value = type
dialogVisible.value = true dialogVisible.value = true
} }
//
const handleTypeDelete = async (rowId: number) => {
await typeDeleteData(xTypeGrid, rowId)
}
const handleDataDelete = async (rowId: number) => {
await dataDeleteData(xDataGrid, rowId)
}
// //
const submitTypeForm = async () => { const submitTypeForm = async () => {
const elForm = unref(typeFormRef)?.getElFormRef() const elForm = unref(typeFormRef)?.getElFormRef()
@ -259,16 +231,15 @@ const submitTypeForm = async () => {
if (actionType.value === 'typeCreate') { if (actionType.value === 'typeCreate') {
data.type = dictTypeValue.value data.type = dictTypeValue.value
await DictTypeApi.createDictTypeApi(data) await DictTypeApi.createDictTypeApi(data)
ElMessage.success(t('common.createSuccess')) message.success(t('common.createSuccess'))
} else if (actionType.value === 'typeUpdate') { } else if (actionType.value === 'typeUpdate') {
await DictTypeApi.updateDictTypeApi(data) await DictTypeApi.updateDictTypeApi(data)
ElMessage.success(t('common.updateSuccess')) message.success(t('common.updateSuccess'))
} }
//
await getTypeList()
dialogVisible.value = false dialogVisible.value = false
} finally { } finally {
actionLoading.value = false actionLoading.value = false
typeGetList(xTypeGrid)
} }
} }
}) })
@ -285,23 +256,17 @@ const submitDataForm = async () => {
if (actionType.value === 'dataCreate') { if (actionType.value === 'dataCreate') {
data.dictType = parentType.value data.dictType = parentType.value
await DictDataApi.createDictDataApi(data) await DictDataApi.createDictDataApi(data)
ElMessage.success(t('common.createSuccess')) message.success(t('common.createSuccess'))
} else if (actionType.value === 'dataUpdate') { } else if (actionType.value === 'dataUpdate') {
await DictDataApi.updateDictDataApi(data) await DictDataApi.updateDictDataApi(data)
ElMessage.success(t('common.updateSuccess')) message.success(t('common.updateSuccess'))
} }
await getDataList()
//
dialogVisible.value = false dialogVisible.value = false
} finally { } finally {
actionLoading.value = false actionLoading.value = false
dataGetList(xDataGrid)
} }
} }
}) })
} }
//
onMounted(async () => {
await getTypeList()
typeTableObject.tableList[0] && onClickType(typeTableObject.tableList[0])
})
</script> </script>