-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jin-Yanhong
committed
Aug 3, 2023
1 parent
e1b0278
commit 4cdc481
Showing
4 changed files
with
211 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,187 @@ | ||
<script lang="jsx"> | ||
import { h } from 'vue'; | ||
import { ElButton, ElPagination, ElTable, ElTableColumn, ElIcon } from 'element-plus'; | ||
import { Search } from '@element-plus/icons-vue'; | ||
import { h, nextTick } from 'vue'; | ||
import { ElButton, ElPagination, ElTable, ElTableColumn, ElIcon, ElDialog, ElForm } from 'element-plus'; | ||
import { Search, Delete, Edit, Plus } from '@element-plus/icons-vue'; | ||
import { formRules } from './setFormRules'; | ||
const formStatus = { | ||
update: 'update', | ||
create: 'create', | ||
}; | ||
export default { | ||
name: 'Crud', | ||
props: ['api'], | ||
render() { | ||
const tableRowData = [ | ||
{ | ||
date: '2016-05-03', | ||
name: 'Tom', | ||
address: 'No. 189, Grove St, Los Angeles', | ||
}, | ||
{ | ||
date: '2016-05-02', | ||
name: 'Tom', | ||
address: 'No. 189, Grove St, Los Angeles', | ||
}, | ||
]; | ||
const defaultSlot = this.$slots.default().map(column => { | ||
return column; | ||
}); | ||
const { tableName, toCreate, toDelete, toUpdate, toQuery, pageCurrentChange, pageSizeChange, toSubmit } = this; | ||
let { status, pager, dialogVisible, tableRowData } = this; | ||
const columns = this.$slots?.column(); | ||
const { toCreate, toDelete, toUpdate, toQuery } = this; | ||
const columnsList = columns && columns instanceof Array ? columns : []; | ||
const columnSlots = columnsList.concat( | ||
<ElTableColumn label='操作'> | ||
{{ | ||
default: ({ row, $index }) => ( | ||
<div> | ||
<ElButton size='small' type='warning' icon={() => <ElIcon>{() => <Edit />}</ElIcon>} onClick={() => toUpdate(row, $index)}> | ||
{() => '更新'} | ||
</ElButton> | ||
<ElButton size='small' type='danger' icon={() => <ElIcon>{() => <Delete />}</ElIcon>} onClick={() => toDelete(row, $index)}> | ||
{() => '删除'} | ||
</ElButton> | ||
</div> | ||
), | ||
}} | ||
</ElTableColumn> | ||
); | ||
return h( | ||
'div', | ||
{ class: ['Crud'] }, | ||
<div> | ||
<div> | ||
<div class='marginT'> | ||
<ElButton size='small' type='' onClick={toCreate}> | ||
{{ default: () => '新增' }} | ||
</ElButton> | ||
<ElButton type='danger' onClick={toDelete}> | ||
{{ default: () => '删除' }} | ||
</ElButton> | ||
<ElButton type='' onClick={toUpdate}> | ||
{{ default: () => '更新' }} | ||
</ElButton> | ||
<ElButton type='danger' icon={{ default: () => <Search /> }} onClick={toQuery}> | ||
{{ default: () => '查询' }} | ||
</ElButton> | ||
</div> | ||
<ElButton type='success' icon={() => <ElIcon>{() => <Plus />}</ElIcon>} onClick={toCreate}> | ||
{() => '新增'} | ||
</ElButton> | ||
<ElButton type='danger' icon={() => <ElIcon>{() => <Delete />}</ElIcon>} onClick={toDelete}> | ||
{() => '删除'} | ||
</ElButton> | ||
<ElButton type='warning' icon={() => <ElIcon>{() => <Edit />}</ElIcon>} onClick={toUpdate}> | ||
{() => '更新'} | ||
</ElButton> | ||
<ElButton type='primary' icon={() => <ElIcon>{() => <Search />}</ElIcon>} onClick={toQuery}> | ||
{() => '查询'} | ||
</ElButton> | ||
</div> | ||
<div class='marginT'> | ||
<ElTable ref='table' data={tableRowData}> | ||
{{ | ||
default: defaultSlot, | ||
}} | ||
<div class='marginT'> | ||
<ElTable ref='table' data={tableRowData} border> | ||
{() => columnSlots} | ||
</ElTable> | ||
</div> | ||
<ElTableColumn> | ||
<ElButton type='' onClick={toUpdate}> | ||
{{ default: () => '修改' }} | ||
<div class='marginT flex'> | ||
<span style='flex: 1' /> | ||
<ElPagination small background layout='total, sizes, prev, pager, next, jumper' pageSize={pager.size} currentPage={pager.current} total={pager.total} onUpdate:current-page={pageCurrentChange} onUpdate:page-size={pageSizeChange} /> | ||
</div> | ||
<ElDialog | ||
appendToBody | ||
title={(status == formStatus.create ? '新增' : '编辑') + ' ' + tableName} | ||
modelValue={dialogVisible} | ||
onUpdate:modelValue={$event => { | ||
this.dialogVisible = $event; | ||
}} | ||
> | ||
{{ | ||
default: () => ( | ||
<ElForm ref='ruleForm' model={this.currentItem} rules={this.formRules}> | ||
{() => this.$slots.form(this.currentItem)} | ||
</ElForm> | ||
), | ||
footer: () => ( | ||
<span> | ||
<ElButton size='small' type='' onClick={() => (this.dialogVisible = false)}> | ||
{() => '取消'} | ||
</ElButton> | ||
<ElButton type='' onClick={toUpdate}> | ||
{{ default: () => '删除' }} | ||
<ElButton size='small' type='primary' onClick={toSubmit}> | ||
{() => '确认'} | ||
</ElButton> | ||
</ElTableColumn> | ||
</ElTable> | ||
</div> | ||
</div> | ||
<div class='marginT'> | ||
<ElPagination small layout='prev, pager, next' total={30} /> | ||
</div> | ||
</span> | ||
), | ||
}} | ||
</ElDialog> | ||
</div> | ||
); | ||
}, | ||
data() { | ||
return { | ||
// | ||
tableRowData: [ | ||
{ | ||
date: '2016-05-03', | ||
name: 'Tom', | ||
state: 'California', | ||
city: 'Los Angeles', | ||
address: 'No. 189, Grove St, Los Angeles', | ||
zip: 'CA 90036', | ||
tag: 'Home', | ||
}, | ||
{ | ||
date: '2016-05-04', | ||
name: 'Tom', | ||
state: 'California', | ||
city: 'Los Angeles', | ||
address: 'No. 189, Grove St, Los Angeles', | ||
zip: 'CA 90036', | ||
tag: 'Home', | ||
}, | ||
], | ||
query: {}, | ||
pager: { | ||
size: 10, | ||
total: 100, | ||
current: 1, | ||
}, | ||
dialogVisible: false, | ||
status: formStatus.create, | ||
currentItem: {}, | ||
formRules: {}, | ||
}; | ||
}, | ||
props: { | ||
tableName: { | ||
type: String, | ||
required: true, | ||
default: '', | ||
}, | ||
}, | ||
created() { | ||
this.setFormRules(); | ||
}, | ||
methods: { | ||
toCreate() {}, | ||
toDelete() {}, | ||
toUpdate() {}, | ||
async resetFormFields(callback) { | ||
await nextTick(() => { | ||
this.$refs.ruleForm.resetFields(); | ||
for (const key in this.currentItem) { | ||
if (Object.hasOwnProperty.call(this.currentItem, key)) { | ||
this.currentItem[key] = undefined; | ||
} | ||
} | ||
}) | ||
.then(() => { | ||
callback(); | ||
}) | ||
.catch(err => { | ||
console.log('Crud resetFormFields Error:', err.message); | ||
}); | ||
}, | ||
setFormRules() { | ||
const columns = this.$slots?.column(); | ||
const columnsList = columns && columns instanceof Array ? columns : []; | ||
columnsList.forEach(column => (this.currentItem[column.props.prop] = undefined)); | ||
this.formRules = formRules(this.currentItem); | ||
}, | ||
toCreate() { | ||
this.dialogVisible = true; | ||
this.resetFormFields(() => { | ||
this.status = formStatus.create; | ||
}); | ||
}, | ||
toDelete(row) {}, | ||
toUpdate(row) { | ||
this.dialogVisible = true; | ||
this.resetFormFields(() => { | ||
this.status = formStatus.update; | ||
this.currentItem = JSON.parse(JSON.stringify(row)); | ||
}); | ||
}, | ||
toQuery() {}, | ||
toReload() {}, | ||
pageCurrentChange() {}, | ||
pageSizeChange() {}, | ||
toSubmit() {}, | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const rules = { | ||
date: [{ required: true, message: 'Date不能为空', trigger: ['blur', 'change'] }], | ||
name: [{ required: true, message: 'Name不能为空', trigger: ['blur', 'change'] }], | ||
state: [{ required: true, message: 'State不能为空', trigger: ['blur', 'change'] }], | ||
city: [{ required: true, message: 'City不能为空', trigger: ['blur', 'change'] }], | ||
address: [{ required: true, message: 'Address不能为空', trigger: ['blur', 'change'] }], | ||
zip: [{ required: true, message: 'Zip不能为空', trigger: ['blur', 'change'] }], | ||
}; | ||
|
||
export function formRules(form) { | ||
let rule = {}; | ||
Object.keys(form).forEach(key => { | ||
rule[key] = rules[key]; | ||
}); | ||
return rule; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters