后台管理-模型关联 关系删除&&筛选

This commit is contained in:
wang-liang0615 2023-08-15 15:02:46 +08:00
parent ff061d4d2e
commit 522991e23b
2 changed files with 90 additions and 105 deletions

View File

@ -1,65 +1,63 @@
<template> <template>
<div> <div class="model-relation">
<a-card :bordered="false"> <a-button @click="handleCreate" type="primary" style="margin-bottom: 15px;" icon="plus">新增关系</a-button>
<a-button @click="handleCreate" type="primary" style="margin-bottom: 15px;" icon="plus">新增关系</a-button> <model-relation-table ref="table"></model-relation-table>
<model-relation-table ref="table"></model-relation-table> <a-modal
<a-modal :closable="false"
:closable="false" :title="drawerTitle"
:title="drawerTitle" :visible="visible"
:visible="visible" @cancel="onClose"
@cancel="onClose" @ok="handleSubmit"
@ok="handleSubmit" width="500px"
width="500px" >
> <a-form :form="form" @submit="handleSubmit" :label-col="{ span: 6 }" :wrapper-col="{ span: 14 }">
<a-form :form="form" @submit="handleSubmit" :label-col="{ span: 6 }" :wrapper-col="{ span: 14 }"> <a-form-item label="源模型">
<a-form-item label="源模型"> <a-select
<a-select showSearch
showSearch name="source_ci_type_id"
name="source_ci_type_id" v-decorator="['source_ci_type_id', { rules: [{ required: true, message: '请选择源模型' }] }]"
v-decorator="['source_ci_type_id', { rules: [{ required: true, message: '请选择源模型' }] }]" @change="handleSourceTypeChange"
@change="handleSourceTypeChange" :filterOption="filterOption"
:filterOption="filterOption" >
> <a-select-option :value="CIType.id" :key="CIType.id" v-for="CIType in displayCITypes">{{
<a-select-option :value="CIType.id" :key="CIType.id" v-for="CIType in displayCITypes">{{ CIType.alias || CIType.name
CIType.alias || CIType.name }}</a-select-option>
}}</a-select-option> </a-select>
</a-select> </a-form-item>
</a-form-item> <a-form-item label="目标模型">
<a-form-item label="目标模型"> <a-select
<a-select showSearch
showSearch name="ci_type_id"
name="ci_type_id" v-decorator="['ci_type_id', { rules: [{ required: true, message: '请选择目标模型' }] }]"
v-decorator="['ci_type_id', { rules: [{ required: true, message: '请选择目标模型' }] }]" @change="handleTargetTypeChange"
@change="handleTargetTypeChange" :filterOption="filterOption"
:filterOption="filterOption" >
> <a-select-option :value="CIType.id" :key="CIType.id" v-for="CIType in displayTargetCITypes">
<a-select-option :value="CIType.id" :key="CIType.id" v-for="CIType in displayTargetCITypes"> {{ CIType.alias || CIType.name }}
{{ CIType.alias || CIType.name }} </a-select-option>
</a-select-option> </a-select>
</a-select> </a-form-item>
</a-form-item>
<a-form-item label="关联关系"> <a-form-item label="关联关系">
<a-select <a-select
name="relation_type_id" name="relation_type_id"
v-decorator="['relation_type_id', { rules: [{ required: true, message: '请选择关联关系' }] }]" v-decorator="['relation_type_id', { rules: [{ required: true, message: '请选择关联关系' }] }]"
> >
<a-select-option :value="relationType.id" :key="relationType.id" v-for="relationType in relationTypes">{{ <a-select-option :value="relationType.id" :key="relationType.id" v-for="relationType in relationTypes">{{
relationType.name relationType.name
}}</a-select-option> }}</a-select-option>
</a-select> </a-select>
</a-form-item> </a-form-item>
<a-form-item label="关联约束"> <a-form-item label="关联约束">
<a-select v-decorator="['constraint', { rules: [{ required: true, message: '请选择关联约束' }] }]"> <a-select v-decorator="['constraint', { rules: [{ required: true, message: '请选择关联约束' }] }]">
<a-select-option value="0">一对多</a-select-option> <a-select-option value="0">一对多</a-select-option>
<a-select-option value="1">一对一</a-select-option> <a-select-option value="1">一对一</a-select-option>
<a-select-option value="2">多对多</a-select-option> <a-select-option value="2">多对多</a-select-option>
</a-select> </a-select>
</a-form-item> </a-form-item>
</a-form> </a-form>
</a-modal> </a-modal>
</a-card>
</div> </div>
</template> </template>
@ -110,16 +108,13 @@ export default {
}, },
mounted() { mounted() {
const _currentId = localStorage.getItem('ops_cityps_currentId') const _currentId = localStorage.getItem('ops_cityps_currentId')
console.log(_currentId)
if (_currentId) { if (_currentId) {
this.currentId = _currentId this.currentId = _currentId
} }
searchResourceType({ page_size: 9999, app_id: 'cmdb' }).then((res) => { searchResourceType({ page_size: 9999, app_id: 'cmdb' }).then((res) => {
console.log('searchResourceType', res)
this.resource_type = { groups: res.groups, id2perms: res.id2perms } this.resource_type = { groups: res.groups, id2perms: res.id2perms }
}) })
this.loadCITypes(!_currentId) this.loadCITypes(!_currentId)
console.log(this.CITypeId)
}, },
computed: { computed: {
currentCId() { currentCId() {
@ -168,7 +163,6 @@ export default {
}, },
getCITypes() { getCITypes() {
getCITypes().then((res) => { getCITypes().then((res) => {
console.log('getCITypes', res.ci_types)
this.CITypes = res.ci_types this.CITypes = res.ci_types
}) })
}, },
@ -196,9 +190,6 @@ export default {
e.preventDefault() e.preventDefault()
this.form.validateFields((err, values) => { this.form.validateFields((err, values) => {
if (!err) { if (!err) {
// eslint-disable-next-line no-console
console.log('Received values of form: ', values)
createRelation(values.source_ci_type_id, values.ci_type_id, values.relation_type_id, values.constraint).then( createRelation(values.source_ci_type_id, values.ci_type_id, values.relation_type_id, values.constraint).then(
(res) => { (res) => {
this.$message.success(`添加成功`) this.$message.success(`添加成功`)
@ -215,8 +206,6 @@ export default {
this.$refs.table.refresh() this.$refs.table.refresh()
}, },
handleDelete(record) { handleDelete(record) {
console.log(record)
deleteRelation(record.source_ci_type_id, record.id).then((res) => { deleteRelation(record.source_ci_type_id, record.id).then((res) => {
this.$message.success(`删除成功`) this.$message.success(`删除成功`)
@ -236,4 +225,12 @@ export default {
} }
</script> </script>
<style></style> <style lang="less" scoped>
.model-relation {
background-color: #fff;
border-radius: 15px;
padding: 24px;
height: calc(100vh - 64px);
margin-bottom: -24px;
}
</style>

View File

@ -1,12 +1,13 @@
<template> <template>
<div> <div>
<vxe-table <vxe-table
ref="xTable"
stripe stripe
class="ops-stripe-table" class="ops-stripe-table"
show-header-overflow show-header-overflow
show-overflow show-overflow
resizable resizable
:max-height="`${windowHeight - 183}px`" :height="`${windowHeight - 160}px`"
:data="tableData" :data="tableData"
:sort-config="{ defaultSort: { field: 'created_at', order: 'desc' } }" :sort-config="{ defaultSort: { field: 'created_at', order: 'desc' } }"
> >
@ -16,21 +17,8 @@
field="relation_type_id" field="relation_type_id"
title="关系" title="关系"
:filters="[{ data: '' }]" :filters="[{ data: '' }]"
:filter-method="filterRelationMethod" :filter-multiple="false"
:filter-recover-method="filterRelationRecoverMethod"
> >
<template #filter="{ $panel, column }">
<template v-for="(option, index) in column.filters">
<input
type="type"
:key="index"
v-model="option.data"
@input="$panel.changeOption($event, !!option.data, option)"
@keyup.enter="$panel.confirmFilter()"
placeholder="按回车确认筛选"
/>
</template>
</template>
<template #default="{ row }"> <template #default="{ row }">
<a-tag color="cyan"> <a-tag color="cyan">
{{ row.relation_type.name }} {{ row.relation_type.name }}
@ -39,9 +27,14 @@
</vxe-column> </vxe-column>
<vxe-column field="child.alias" title="目标模型"></vxe-column> <vxe-column field="child.alias" title="目标模型"></vxe-column>
<vxe-column field="constraint" title="关联约束"></vxe-column> <vxe-column field="constraint" title="关联约束"></vxe-column>
<vxe-column field="authorization" title="授权" width="89px"> <vxe-column field="authorization" title="操作" width="89px">
<template #default="{ row }"> <template #default="{ row }">
<a @click="handleOpenGrant(row)"><a-icon type="user-add"/></a> <a-space>
<a @click="handleOpenGrant(row)"><a-icon type="user-add"/></a>
<a-popconfirm title="确认删除?" @confirm="deleteRelation(row)">
<a :style="{ color: 'red' }"><ops-icon type="icon-xianxing-delete"/></a>
</a-popconfirm>
</a-space>
</template> </template>
</vxe-column> </vxe-column>
</vxe-table> </vxe-table>
@ -50,7 +43,7 @@
</template> </template>
<script> <script>
import { getCITypeRelations } from '@/modules/cmdb/api/CITypeRelation' import { getCITypeRelations, deleteRelation } from '@/modules/cmdb/api/CITypeRelation'
import { getRelationTypes } from '@/modules/cmdb/api/relationType' import { getRelationTypes } from '@/modules/cmdb/api/relationType'
import CMDBGrant from '../../../components/cmdbGrant' import CMDBGrant from '../../../components/cmdbGrant'
@ -86,40 +79,35 @@ export default {
item.constraint = this.handleConstraint(item.constraint) item.constraint = this.handleConstraint(item.constraint)
}) })
this.tableData = res this.tableData = res
console.log('MainData', res)
}, },
// 获取关系 // 获取关系
async getRelationTypes() { async getRelationTypes() {
const res = await getRelationTypes() const res = await getRelationTypes()
const relationTypeMap = new Map() this.relationTypeList = res.map((item) => ({ value: item.id, label: item.name }))
res.forEach((item) => { const $table = this.$refs.xTable
relationTypeMap.set(item.id, item.name) if ($table) {
}) const nameColumn = $table.getColumnByField('relation_type_id')
this.relationTypeList = relationTypeMap if (nameColumn) {
console.log('relationTypeList', this.relationTypeList) $table.setFilter(nameColumn, this.relationTypeList)
}
}
}, },
// 转换关联关系 // 转换关联关系
handleConstraint(constraintId) { handleConstraint(constraintId) {
return this.constraintMap[constraintId] return this.constraintMap[constraintId]
}, },
handleOpenGrant(record) { handleOpenGrant(record) {
console.log('record', record)
console.log(`${record.parent.name} -> ${record.child.name}`)
// this.$refs.grantDrawer.open({ name: `${record.parent.name} -> ${record.child.name}` })
this.$refs.cmdbGrant.open({ this.$refs.cmdbGrant.open({
name: `${record.parent.name} -> ${record.child.name}`, name: `${record.parent.name} -> ${record.child.name}`,
typeRelationIds: [record.parent_id, record.child_id], typeRelationIds: [record.parent_id, record.child_id],
cmdbGrantType: 'type_relation', cmdbGrantType: 'type_relation',
}) })
}, },
filterRelationMethod({ option, row }) { deleteRelation(row) {
return row.relation_type.name.includes(String(option.data)) deleteRelation(row.parent_id, row.child_id).then((res) => {
}, this.$message.success(`删除成功`)
filterRelationRecoverMethod({ option }) { this.getRelationTypes()
option.data = '' })
},
refresh() {
this.getMainData()
}, },
}, },
} }