[Update] 修改generic detail

This commit is contained in:
ibuler 2020-04-21 14:36:49 +08:00
parent 668ac84e8a
commit b4aac49eb1
2 changed files with 71 additions and 57 deletions

View File

@ -1,8 +1,10 @@
<template> <template>
<Page> <Page v-loading="loading">
<span slot="title"> <template #title>
{{ title }} <span>
</span> {{ validTitle }}
</span>
</template>
<template #headingRightSide> <template #headingRightSide>
<span v-if="hasRightSide"> <span v-if="hasRightSide">
@ -35,6 +37,10 @@ export default {
ActionsGroup ActionsGroup
}, },
props: { props: {
object: {
type: Object,
required: true
},
title: { title: {
type: String, type: String,
default: '' default: ''
@ -51,71 +57,77 @@ export default {
type: Boolean, type: Boolean,
default: true default: true
}, },
canDelete: { actions: {
type: [Boolean, Function], type: Object, // defaultActions
default: true default: () => ({})
}, },
deleteCallback: { getObjectName: {
type: Function, type: Function,
default: function(item) { default: function(obj) {
return this.defaultDelete(item) return obj.name
} }
}, },
deleteUrl: { getTitle: {
type: String,
default: function() {
return getApiPath(this)
}
},
deleteSuccessRoute: {
type: String,
default: function() {
return this.$route.name.replace('Detail', 'List')
}
},
canUpdate: {
type: [Boolean, Function],
default: true
},
updateCallback: {
type: Function, type: Function,
default: function(item) { default: function(obj) {
return this.defaultUpdate(item) const objectType = this.$tr(this.$route.meta.title)
} .replace('Detail', '')
}, .replace('详情', '')
updateRoute: { console.log('Object is: ', this.obj)
type: String, const objectName = this.getObjectName(obj)
default: function() { return `${objectType}: ${objectName}`
return this.$route.name.replace('Detail', 'Update')
} }
} }
}, },
data() { data() {
const defaultActions = {
canDelete: true,
deleteCallback: function(item) { this.defaultDelete(item) },
deleteApiUrl: getApiPath(this),
deleteSuccessRoute: this.$route.name.replace('Detail', 'List'),
canUpdate: true,
updateCallback: function(item) { this.defaultUpdate(item) },
updateRoute: this.$route.name.replace('Detail', 'Update'),
detailApiUrl: getApiPath(this)
}
return { return {
activeName: this.activeMenu || null, loading: false,
pageActions: [ activeName: this.activeMenu || 'info',
validActions: Object.assign(defaultActions, this.actions)
}
},
computed: {
pageActions() {
return [
{ {
name: 'update', name: 'update',
title: this.$tc('Update'), title: this.$tc('Update'),
can: this.canUpdate, can: this.validActions.canUpdate,
callback: this.updateCallback.bind(this) callback: this.validActions.updateCallback.bind(this)
}, },
{ {
name: 'delete', name: 'delete',
title: this.$tc('Delete'), title: this.$tc('Delete'),
can: this.canDelete, can: this.validActions.canDelete,
callback: this.deleteCallback.bind(this) callback: this.validActions.deleteCallback.bind(this)
} }
] ]
},
validTitle() {
return this.title || this.getTitle(this.object)
} }
}, },
mounted() {
this.getObject()
},
methods: { methods: {
defaultDelete() { defaultDelete() {
const msg = this.$tc('Are you sure to delete') + ' ?' const msg = this.$tc('Are you sure to delete') + ' ?'
const title = this.$tc('Info') const title = this.$tc('Info')
const performDelete = async function() { const performDelete = async function() {
this.$log.debug('Start perform delete: ', this.deleteUrl) const url = this.validActions.deleteApiUrl
return this.$axios.delete(this.deleteUrl) this.$log.debug('Start perform delete: ', url)
return this.$axios.delete(url)
} }
this.$alert(msg, title, { this.$alert(msg, title, {
type: 'warning', type: 'warning',
@ -128,7 +140,7 @@ export default {
await performDelete.bind(this)() await performDelete.bind(this)()
done() done()
this.$message.success(this.$tc('Delete success')) this.$message.success(this.$tc('Delete success'))
this.$router.push({ name: this.deleteSuccessRoute }) this.$router.push({ name: this.validActions.deleteSuccessRoute })
} catch (error) { } catch (error) {
this.$message.error(this.$tc('Delete failed' + ' ' + error)) this.$message.error(this.$tc('Delete failed' + ' ' + error))
} finally { } finally {
@ -141,9 +153,17 @@ export default {
}, },
defaultUpdate() { defaultUpdate() {
const id = this.$route.params.id const id = this.$route.params.id
this.$router.push({ name: this.updateRoute, params: { id: id }}) const routeName = this.validActions.updateRoute
this.$router.push({ name: routeName, params: { id: id }})
}, },
getObject() { getObject() {
this.loading = true
const url = this.validActions.detailApiUrl
this.$axios.get(url).then(data => {
this.$emit('update:object', data)
}).finally(() => {
this.loading = false
})
} }
} }
} }

View File

@ -1,6 +1,6 @@
<template> <template>
<GenericDetailPage v-loading="loading" :title="title" v-bind="config"> <GenericDetailPage :object.sync="group" v-bind="config">
<template v-slot:info> <template #info>
<div> <div>
<el-row :gutter="20"> <el-row :gutter="20">
<el-col :span="14"> <el-col :span="14">
@ -24,7 +24,7 @@
</template> </template>
<script> <script>
import { getUserGroupDetail, getUserGroupMembers } from '@/api/user' import { getUserGroupMembers } from '@/api/user'
import { GenericDetailPage } from '@/layout/components' import { GenericDetailPage } from '@/layout/components'
import DetailCard from '@/components/DetailCard' import DetailCard from '@/components/DetailCard'
import Select2 from '@/components/Select2' import Select2 from '@/components/Select2'
@ -64,9 +64,9 @@ export default {
} }
}, },
computed: { computed: {
title() { // title() {
return this.$t('users.userGroup') + ': ' + this.group.name // return this.$t('users.userGroup') + ': ' + this.group.name
}, // },
detailItems() { detailItems() {
return [ return [
{ {
@ -89,12 +89,6 @@ export default {
} }
}, },
mounted() { mounted() {
getUserGroupDetail(this.$route.params.id).then(data => {
this.group = data
}).finally(() => {
this.loading = false
})
getUserGroupMembers(this.$route.params.id).then(data => { getUserGroupMembers(this.$route.params.id).then(data => {
this.groupMembers = data.map(v => { this.groupMembers = data.map(v => {
const member = {} const member = {}