lg_frontend/components/smallCommon/Curd.vue

335 lines
9.9 KiB
Vue
Raw Normal View History

2024-06-24 16:23:39 +00:00
<template>
<div style="text-align: left">
2024-06-26 16:52:06 +00:00
<div class="table-search" v-if="searchItems.length > 0">
<a-form class="ant-advanced-search-form" :form="form" @submit="handleSearch" layout="inline"
@change="handleSearchFormChange">
2024-06-24 16:23:39 +00:00
<a-form-item v-for="menuItem in searchItems" :key="menuItem.key" :label="menuItem.label">
<a-input
v-decorator="[menuItem.key]"
:placeholder="menuItem.placeholder || ''"
v-if="menuItem.type === 'input'"
/>
<a-select
v-decorator="[menuItem.key]"
:placeholder="menuItem.placeholder || ''"
v-else-if="menuItem.type === 'select'"
>
<a-select-option :value="cItem.value" v-for="cItem in menuItem.children || []" :key="cItem.value">
{{ cItem.label }}
</a-select-option>
2024-06-24 16:23:39 +00:00
</a-select>
<a-range-picker
:placeholder="menuItem.placeholder || ''" v-decorator="[menuItem.key]"
v-else-if="menuItem.type === 'dateRange'" format="YYYY-MM-DD"/>
</a-form-item>
<a-form-item>
<a-button type="primary" html-type="submit" v-if="!hideSearch">查询</a-button>
<a-button :style="{ marginLeft: '8px' }" @click="reset" v-if="!hideSearch">重置</a-button>
<slot name="search-button" v-bind:search="form.getFieldsValue()"></slot>
2024-06-24 16:23:39 +00:00
</a-form-item>
</a-form>
</div>
<a-divider v-if="!hideButton && searchItems.length > 0"/>
2024-06-26 16:52:06 +00:00
<div class="table-operations" v-if="!hideButton">
2024-06-24 16:23:39 +00:00
<a-button @click="addRow" type="primary">
添加
</a-button>
</div>
<a-table v-bind="tableProps" :pagination="page" :row-key="primaryKey" :columns="realColumns"
:data-source="tableDataSource" @change="handleChange">
2024-06-24 16:23:39 +00:00
<span slot="action" slot-scope="text, record">
<a-button type="danger" size="small" @click="deleteRow(record)">删除</a-button>
<a-divider type="vertical"/>
2024-06-24 16:23:39 +00:00
<a-button type="primary" size="small" @click="editRow(record)">编辑</a-button>
</span>
<span slot="details" slot-scope="text, record">
<!-- <a-button type="danger" size="small" @click="deleteRow(record)">删除</a-button>-->
<!-- <a-divider type="vertical"/>-->
<a-button type="primary" size="small" @click="viewDetails(record)">详情</a-button>
</span>
2024-06-24 16:23:39 +00:00
</a-table>
<a-modal
:title="dialog.title"
:visible="dialog.visible"
:confirm-loading="dialog.loading"
@ok="handleOk"
@cancel="handleCancel"
>
<a-form class="ant-advanced-search-form" :form="editForm" @submit="handleSearch" @change="handleSearchFormChange">
<a-form-item v-for="menuItem in formItems" :key="menuItem.key" :label="menuItem.label" v-if="!menuItem.hide">
2024-06-24 16:23:39 +00:00
<a-input
2024-06-26 00:19:02 +00:00
v-bind="menuItem.props || {}"
v-decorator="gtDecorator(menuItem)"
2024-06-24 16:23:39 +00:00
:placeholder="menuItem.placeholder || ''"
v-if="menuItem.type === 'input'"
/>
<a-select
2024-06-26 00:19:02 +00:00
v-decorator="gtDecorator(menuItem)"
2024-06-24 16:23:39 +00:00
:placeholder="menuItem.placeholder || ''"
v-else-if="menuItem.type === 'select'"
2024-06-26 00:19:02 +00:00
v-bind="menuItem.props || {}"
2024-06-24 16:23:39 +00:00
>
<a-select-option :value="cItem.value" v-for="cItem in menuItem.children || []" :key="cItem.value">
{{ cItem.label }}
</a-select-option>
2024-06-24 16:23:39 +00:00
</a-select>
2024-06-26 00:19:02 +00:00
<a-tree-select
v-bind="menuItem.props || {}"
v-decorator="gtDecorator(menuItem)"
:placeholder="menuItem.placeholder || ''"
allow-clear
:tree-data="menuItem.children"
tree-default-expand-all
style="width: 100%"
:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
v-else-if="menuItem.type === 'treeSelect'"
>
<a-select-option :value="cItem.value" v-for="cItem in menuItem.children || []" :key="cItem.value">
{{ cItem.label }}
</a-select-option>
2024-06-26 00:19:02 +00:00
</a-tree-select>
<a-range-picker v-bind="menuItem.props || {}" v-decorator="gtDecorator(menuItem)"
v-else-if="menuItem.type === 'dateRange'" format="YYYY-MM-DD"/>
2024-06-24 16:23:39 +00:00
</a-form-item>
</a-form>
</a-modal>
</div>
</template>
<script>
export default {
name: "user",
data() {
2024-06-24 16:23:39 +00:00
return {
2024-06-26 00:19:02 +00:00
isValidate: false,
2024-06-24 16:23:39 +00:00
tableDataSource: [],
form: this.$form.createForm(this, {name: `form_${new Date().valueOf()}`}),
editForm: this.$form.createForm(this, {name: `edit_form_${new Date().valueOf()}`}),
2024-06-24 16:23:39 +00:00
dialog: {
title: '',
visible: false,
isEdit: '',
loading: false
},
selectRow: null,
page: {
pageSize: 10,
2024-06-26 16:52:06 +00:00
current: 1,
total: 0
2024-06-24 16:23:39 +00:00
},
searchFormModel: {}
}
},
props: {
2024-06-26 16:52:06 +00:00
tableProps: {
type: Object,
default: () => ({})
},
hideSearch: {
type: Boolean,
default: false
},
hideAction: {
type: Boolean,
default: false
},
hideButton: {
type: Boolean,
default: false
},
2024-06-24 16:23:39 +00:00
// 表单内容配置
formItems: {
type: Array,
default: () => []
2024-06-24 16:23:39 +00:00
},
// 列表列配置
columns: {
type: Array,
default: () => []
2024-06-24 16:23:39 +00:00
},
primaryKey: {
type: String,
default: 'id'
},
apiConf: {
type: Object,
default: () => ({})
2024-06-26 00:19:02 +00:00
},
2024-06-24 16:23:39 +00:00
},
computed: {
searchItems() {
2024-06-24 16:23:39 +00:00
return this.formItems.filter(item => !!item.isSearch)
},
realColumns() {
if (this.hideAction) return [...this.columns]
2024-06-24 16:23:39 +00:00
return [
...this.columns,
{
title: '操作',
dataIndex: 'action',
scopedSlots: {customRender: 'action'},
2024-06-24 16:23:39 +00:00
fixed: 'right',
width: 155
},
]
}
},
watch: {},
2024-06-24 16:23:39 +00:00
mounted() {
this.handleSearch()
},
methods: {
viewDetails(data){
this.$emit('viewDetails',data)
},
gtDecorator(menuItem) {
2024-06-26 00:19:02 +00:00
if (menuItem.rules) {
this.isValidate = true
return [
menuItem.key,
{rules: menuItem.rules}
2024-06-26 00:19:02 +00:00
]
}
return [
menuItem.key
]
},
2024-06-24 16:23:39 +00:00
handleSearchFormChange(changedFields) {
this.searchFormModel = {...this.searchFormModel, ...changedFields}
2024-06-24 16:23:39 +00:00
},
addRow() {
2024-06-24 16:23:39 +00:00
this.handleCancel()
2024-06-26 00:19:02 +00:00
this.$emit('edit', false)
2024-06-24 16:23:39 +00:00
this.dialog.visible = true
},
2024-06-26 00:19:02 +00:00
async editRow(row) {
if (this.apiConf.detailApi) {
const {api, method, detailHandler} = this.apiConf.detailApi
const {data} = await this[`$${method}`](api, {[this.primaryKey]: row[this.primaryKey]})
2024-06-26 00:19:02 +00:00
this.selectRow = detailHandler && detailHandler(data) || data
} else {
this.selectRow = row
}
this.dialog.title = '编辑'
2024-06-24 16:23:39 +00:00
this.dialog.visible = true
this.dialog.isEdit = true
this.dialog.loading = false
2024-06-26 00:19:02 +00:00
this.$emit('edit', true)
setTimeout(() => {
this.editForm.setFieldsValue(this.selectRow)
}, 200)
2024-06-24 16:23:39 +00:00
},
deleteRow(row) {
2024-06-26 00:19:02 +00:00
const self = this
2024-06-26 16:52:06 +00:00
if (!self.apiConf.deleteApi) return
2024-06-24 16:23:39 +00:00
this.$confirm({
title: '确认要删除么?',
okText: '确认',
okType: 'danger',
cancelText: '再想想',
onOk() {
2024-06-26 16:52:06 +00:00
const {api, deleteKey, paramsType, method} = self.apiConf.deleteApi
2024-06-26 00:19:02 +00:00
let params = {}
if (paramsType === 'Array') {
params = [row[self.primaryKey]]
} else {
params[deleteKey] = row[self.primaryKey]
}
self[`$${method}`](api, params, {params: params}).then(res => {
self.handleSearch()
2024-06-24 16:23:39 +00:00
})
}
});
},
handleChange({pageSize, current}) {
2024-06-24 16:23:39 +00:00
this.page.pageSize = pageSize
2024-06-26 16:52:06 +00:00
this.page.current = current
2024-06-24 16:23:39 +00:00
this.handleSearch()
},
reset() {
this.form.resetFields();
},
2024-06-24 16:23:39 +00:00
async handleSearch(e) {
e && e.preventDefault();
const values = this.form.getFieldsValue()
const {api, noPage, method} = this.apiConf.listApi
const {data} = await this[`$${method}`](api, {
2024-06-24 16:23:39 +00:00
...values,
pageSize: this.page.pageSize,
2024-06-26 16:52:06 +00:00
current: this.page.current,
2024-06-24 16:23:39 +00:00
})
2024-06-26 00:19:02 +00:00
if (noPage) {
2024-06-26 16:52:06 +00:00
this.page = false
2024-06-26 00:19:02 +00:00
this.tableDataSource = data
} else {
2024-06-26 16:52:06 +00:00
this.page.total = data.total || 0
2024-06-26 00:19:02 +00:00
this.tableDataSource = data.items
}
2024-06-24 16:23:39 +00:00
},
async handleOk() {
2024-06-26 00:19:02 +00:00
if (this.isValidate) {
this.editForm.validateFields(async (err, values) => {
if (!err) {
if (this.dialog.isEdit) {
const {api, method, updateKey} = this.apiConf.editApi
2024-06-26 00:19:02 +00:00
await this[`$${method}`](api, {
[updateKey || this.primaryKey]: this.selectRow[this.primaryKey],
...values
})
} else {
const {api, method} = this.apiConf.addApi
2024-06-26 00:19:02 +00:00
await this[`$${method}`](api, values)
}
this.handleCancel()
this.handleSearch()
}
});
2024-06-24 16:23:39 +00:00
} else {
2024-06-26 00:19:02 +00:00
const values = this.editForm.getFieldsValue()
if (this.dialog.isEdit) {
const {api, method, updateKey} = this.apiConf.editApi
2024-06-26 00:19:02 +00:00
await this[`$${method}`](api, {
[updateKey || this.primaryKey]: this.selectRow[this.primaryKey],
...values
})
} else {
const {api, method} = this.apiConf.addApi
2024-06-26 00:19:02 +00:00
await this[`$${method}`](api, values)
}
this.handleCancel()
this.handleSearch()
2024-06-24 16:23:39 +00:00
}
2024-06-26 00:19:02 +00:00
2024-06-24 16:23:39 +00:00
},
handleCancel() {
2024-06-26 00:19:02 +00:00
this.selectRow = null
2024-06-24 16:23:39 +00:00
this.dialog.title = '添加'
this.dialog.visible = false
this.dialog.isEdit = false
this.dialog.loading = false
2024-06-26 00:19:02 +00:00
this.editForm.resetFields()
2024-06-24 16:23:39 +00:00
},
}
}
</script>
<style scoped lang="less">
.table-operations {
margin-bottom: 16px;
}
2024-06-24 16:23:39 +00:00
.ant-form-item {
margin-bottom: 0;
}
2024-06-24 16:23:39 +00:00
.ant-divider-horizontal {
margin: 16px 0;
}
.table-operations > button {
margin-right: 8px;
}
</style>