feat: add vxe-table

This commit is contained in:
xingyu 2022-10-19 18:08:42 +08:00
parent 4e415486eb
commit aa3aa56140
2 changed files with 163 additions and 13 deletions

View File

@ -1,35 +1,185 @@
<script setup lang="ts"> <script setup lang="ts">
import { PropType, reactive, ref } from 'vue' import { PropType, reactive, ref } from 'vue'
import { VxeGrid, VxeGridProps, VxeTableInstance } from 'vxe-table' import { propTypes } from '@/utils/propTypes'
import { VxeGrid, VxeGridProps, VXETable, VxeTableInstance } from 'vxe-table'
import XEUtils from 'xe-utils'
const props = defineProps({ const props = defineProps({
options: { columns: {
type: Object as PropType<VxeGridProps>, type: Array as PropType<any[]>,
default: () => ({}) default: () => []
} },
form: {
type: Array as PropType<any[]>,
default: () => []
},
api: propTypes.string.def('')
}) })
const xGrid = ref<VxeTableInstance>() const xGrid = ref<VxeTableInstance>()
const gridOptions = reactive<VxeGridProps>({ const gridOptions = reactive<VxeGridProps>({
height: 300, id: 'crud',
height: 600,
showHeaderOverflow: true,
showOverflow: true,
align: null, align: null,
loading: true,
columnConfig: { columnConfig: {
resizable: true resizable: true
}, },
formConfig: {
titleWidth: 100,
titleAlign: 'right',
items: []
},
pagerConfig: {
pageSize: 10,
pageSizes: [5, 10, 15, 20, 50, 100, 200, 500]
},
columns: [], columns: [],
toolbarConfig: {}, toolbarConfig: {
data: [] buttons: [
{ code: 'insert', name: '新增' },
{ code: 'delete', name: '删除' },
{ code: 'save', name: '保存', status: 'success' }
],
refresh: true,
import: true,
export: true,
print: true,
zoom: true,
custom: true
},
importConfig: {
remote: true,
types: ['xlsx'],
modes: ['insert'],
//
async importMethod({ file }) {
const formBody = new FormData()
formBody.append('file', file)
try {
const response = await fetch(`/api/pub/import`, {
method: 'POST',
body: formBody
})
const data = await response.json()
VXETable.modal.message({
content: `成功导入 ${data.result.insertRows} 条记录!`,
status: 'success'
})
//
xGrid.value.commitProxy('query')
} catch {
VXETable.modal.message({ content: '导入失败,请检查数据是否正确!', status: 'error' })
}
}
},
proxyConfig: {
seq: true, //
sort: true, // query
filter: true, // query
form: true, // reload
// { result: [], page: { total: 100 } }
props: {
result: 'result', //
total: 'page.total' //
},
// Promise
ajax: {
// queryreload
query: ({ page, sorts, filters, form }) => {
const queryParams: any = Object.assign({}, form)
//
const firstSort = sorts[0]
if (firstSort) {
queryParams.sort = firstSort.field
queryParams.order = firstSort.order
}
//
filters.forEach(({ field, values }) => {
queryParams[field] = values.join(',')
})
return fetch(
props.api + `/list${page.pageSize}/${page.currentPage}?${XEUtils.serialize(queryParams)}`
).then((response) => response.json())
},
// delete
delete: ({ body }) => {
return fetch(props.api + `/save`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
}).then((response) => response.json())
},
// save
save: ({ body }) => {
return fetch(props.api + `/save`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
}).then((response) => response.json())
}
}
},
exportConfig: {
remote: true,
types: ['xlsx'],
modes: ['current', 'selected', 'all'],
//
async exportMethod({ options }) {
const $grid = xGrid.value
const proxyInfo = $grid.getProxyInfo()
//
const body = {
filename: options.filename,
sheetName: options.sheetName,
isHeader: options.isHeader,
original: options.original,
mode: options.mode,
pager: proxyInfo ? proxyInfo.pager : null,
ids: options.mode === 'selected' ? options.data.map((item) => item.id) : [],
fields: options.columns.map((column) => {
return {
field: column.field,
title: column.title
}
})
}
//
try {
const response = await fetch(`/api/pub/export`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
})
const data = await response.json()
if (data.id) {
VXETable.modal.message({ content: '导出成功,开始下载', status: 'success' })
//
fetch(`/api/pub/export/download/${data.id}`).then((response_1) => {
response_1.blob().then((blob) => {
//
VXETable.saveFile({ filename: '导出数据', type: 'xlsx', content: blob })
})
})
}
} catch {
VXETable.modal.message({ content: '导出失败!', status: 'error' })
}
}
}
}) })
const init = () => { const init = () => {
console.log(props.options) gridOptions.columns = props.columns
gridOptions.formConfig.items = props.form
gridOptions.loading = false
} }
init() init()
</script> </script>
<template> <template>
<ContentWrap> <vxe-grid ref="xGrid" v-bind="gridOptions" class="pro-table-scrollbar" />
<vxe-grid ref="xGrid" v-bind="gridOptions" show-footer class="pro-table-scrollbar" />
</ContentWrap>
</template> </template>
<style scoped> <style scoped>

View File

@ -72,7 +72,7 @@ VXETable.setup({
clearable: true clearable: true
}, },
i18n: (key, args) => { i18n: (key, args) => {
return unref(i18n.global.locale) === 'zh' return unref(i18n.global.locale) === 'zh-CN'
? XEUtils.toFormatString(XEUtils.get(zhCN, key), args) ? XEUtils.toFormatString(XEUtils.get(zhCN, key), args)
: XEUtils.toFormatString(XEUtils.get(enUS, key), args) : XEUtils.toFormatString(XEUtils.get(enUS, key), args)
} }