1、组件定义
<template>
<el-dialog @open="handleOpenDialog"
@opened="handleOpenedDialog"
@close="handleCloseDialog"
@closed="handleClosedDialog"
:top="top"
:title="title"
:width="width"
:modal="modal"
:center="center"
:lock-scroll="true"
:show-close="showClose"
:visible.sync="visible"
:fullscreen="fullscreen"
:custom-class="customClass"
:modal-append-to-body="true"
:close-on-click-modal="false"
:append-to-body="appendToBody"
:close-on-press-escape="false">
<!-- body start -->
<div class="dialog-layout"
:style="style">
<div ref="dialogBody">
<slot></slot>
</div>
</div>
<!-- body end -->
<!-- footer start -->
<div slot="footer"
class="dialog-footer"
v-if="isBtnGroup">
<template v-for="item in btnGroups">
<el-button :key="item.label"
:type="item.type"
:size="item.size || ''"
:loading="item.loading"
v-if="item.show == null ? true : item.show"
:disabled="item.disabled == null ? false : item.disabled"
@click="handleClick(item.fn)">
{{item.label}}
</el-button>
</template>
</div>
<!-- footer end -->
</el-dialog>
</template>
<script>
export default {
name: 'GovDialog',
data () {
return {
visible: false,
clientHeight: 250,
style: {},
btnLoading: false,
}
},
computed: {
btnGroups () {
let btnGroups = []
if (this.btnGroup.length > 0) {
btnGroups = this.btnGroup
} else {
btnGroups = [
{
label: '保 存',
name: 'saveButton',
type: 'primary',
show: true,
disabled: false,
loading: this.btnLoading,
fn: 'handleSubmit',
},
{
label: '取 消',
name: 'cancalButton',
show: true,
type: 'default',
disabled: false,
fn: 'handleCancel',
},
]
}
return btnGroups
},
},
props: {
lockScroll: {
type: Boolean,
default: true,
},
title: {
type: String,
default: '无标题弹窗',
},
width: {
type: String,
default: '50%',
},
btnGroup: {
type: Array,
default: () => [],
},
fullscreen: {
type: Boolean,
default: false,
},
top: {
type: String,
default: '15vh',
},
modal: {
type: Boolean,
default: true,
},
appendToBody: {
type: Boolean,
default: true,
},
customClass: {
type: String,
default: '',
},
showClose: {
type: Boolean,
default: true,
},
center: {
type: Boolean,
default: false,
},
isBtnGroup: {
type: Boolean,
default: true,
},
},
methods: {
open () {
this.visible = true
this.btnLoading = false
},
close () {
this.visible = false
this.btnLoading = false
},
changeLoading (value) {
this.btnLoading = value
},
handleOpenDialog () {
this.$nextTick(() => {
this.resetDocumentClientHeight()
})
this.$emit('open')
},
handleOpenedDialog () {
this.$nextTick(() => {
this.resetDocumentClientHeight()
})
this.$emit('opened')
},
handleCloseDialog () {
this.$emit('close')
},
handleClosedDialog () {
this.$emit('closed')
},
handleClick (fn) {
if (fn === 'handleCancel') {
this.$emit(fn)
this.close()
} else {
this.$emit(fn)
}
},
setDialogHeight () {
if (!this.$refs.dialogBody) {
return
}
const documentClientHeight = document.documentElement['clientHeight']
const otherHeight = 150 + this.top.replace('vh', '') * documentClientHeight / 100
const bodyHeight = this.$refs.dialogBody.clientHeight
if (documentClientHeight - otherHeight <= bodyHeight) {
this.style = {
height: documentClientHeight - otherHeight + 'px',
overflow: 'auto',
}
} else {
this.style = {}
}
},
resetDocumentClientHeight () {
this.setDialogHeight()
window.onresize = () => {
this.setDialogHeight()
}
},
toggleButtonStatus ({ name = 'saveButton', status = false, type = 'loading' }) {
let btnGroups = this.btnGroups
for (let i = 0, len = btnGroups.length; i < len; i++) {
if (btnGroups[i].name === name) {
this.$set(btnGroups[i], type, status)
break
}
}
},
},
}
</script>
<style lang="scss" scoped>
.dialog-layout {
margin: -30px -20px;
padding: 0px 20px 30px 20px;
}
</style>
全局引入
Vue.component("gdsDialog", gdsDialog)
全局使用
<template>
<gds-dialog
ref="dialog"
@closed="handleClosed"
title="选择事项目录"
@handleSubmit="handleSubmit"
width="70%"
>
<multiply-table
v-if="isShow"
ref="multiply"
:selected-list="submitSelectedIds"
:column-map="infoOption"
:draw="getElementPage"
@giveSelectedObjs="handleChangeSelectedIds"
></multiply-table>
</gds-dialog>
</template>