[Update] stash card

This commit is contained in:
ibuler
2020-04-26 08:59:17 +08:00
parent ec3bd29153
commit b55cc3ce9f
3 changed files with 92 additions and 38 deletions

View File

@@ -8,7 +8,7 @@
<table style="width: 100%"> <table style="width: 100%">
<tr> <tr>
<td colspan="2"> <td colspan="2">
<Select2 v-model="select2.value" v-bind="select2" @loadInitialOptionsDone="handleLoadInitialDone" /> <Select2 ref="select2" v-model="select2.value" v-bind="select2" />
</td> </td>
</tr> </tr>
<tr> <tr>
@@ -16,8 +16,8 @@
<el-button type="primary" size="small">{{ $tc('Add') }}</el-button> <el-button type="primary" size="small">{{ $tc('Add') }}</el-button>
</td> </td>
</tr> </tr>
<tr v-for="obj of validObjects" :key="obj.id" style="width: 100%" class="item"> <tr v-for="obj of iHasObjects" :key="obj.value" style="width: 100%" class="item">
<td><b>{{ obj.name }}</b></td> <td><b>{{ obj.label }}</b></td>
<td> <td>
<el-button size="mini" type="danger" style="float: right"> <el-button size="mini" type="danger" style="float: right">
<i class="fa fa-minus" /> <i class="fa fa-minus" />
@@ -31,6 +31,7 @@
<script> <script>
import Select2 from '@/components/Select2' import Select2 from '@/components/Select2'
import { createSourceIdCache } from '@/api/common'
export default { export default {
name: 'RelationCard', name: 'RelationCard',
@@ -46,7 +47,7 @@ export default {
type: String, type: String,
default: '' default: ''
}, },
// 地址发送给select2的查询所有的objects // 地址发送给select2的查询所有的objects, 和select2 ajax一样
objectsAjax: { objectsAjax: {
type: Object, type: Object,
default: () => ({}) default: () => ({})
@@ -55,35 +56,86 @@ export default {
type: [Array, null], type: [Array, null],
default: null default: null
}, },
// 已选择的objects Id, 会转换成select2的value, 作为默认选择项 // 已选择的objects Id, 会转换成select2的value, 作为默认选择项, 和objectsAjax类似
hasObjectsId: { hasObjectsId: {
type: Array, type: Array,
default: () => [] default: () => []
}, },
hasObjects: { hasObjects: {
type: [Array, null], type: Array,
default: null default: () => []
}, },
value: { value: {
type: [Array, Number, String], type: [Array, Number, String],
default: '' default: () => []
} }
}, },
data() { data() {
return { return {
validObjects: this.objects ? this.objects : [], iHasObjects: this.hasObjects || [],
params: {
page: 1,
hasMore: false,
pageSize: 10
},
select2: { select2: {
ajax: { url: '/api/v1/users/users/' }, ajax: this.objectsAjax,
options: this.objects,
value: this.value, value: this.value,
isSelectedValue: false isSelectedValue: false
} }
} }
}, },
computed: {
iAjax() {
this.$log.debug('iAjax', this.$refs.select2)
return this.$refs.select2.iAjax
}
},
mounted() {
if (this.hasObjectsId.length !== 0) {
setTimeout(() => {
this.getHasObjectsByIds()
}, 500)
}
},
methods: { methods: {
handleLoadInitialDone(initialOptions) { safeMakeParams(params) {
if (this.objects === null) { this.$log.debug('safeMakeParams', this.$refs.select2)
this.validObjects = initialOptions return this.$refs.select2.safeMakeParams(params)
},
async loadMore() {
if (this.loading) {
return
} }
if (!this.params.hasMore) {
return
}
this.params.page = this.params.page ? this.params.page + 1 : 1
try {
this.loading = true
await this.loadHasObjects()
} finally {
this.loading = false
}
},
async loadHasObjects() {
this.$log.debug('Start loadHasObject: ', this.params)
const params = this.safeMakeParams(this.params)
let data = await this.$axios.get(this.iAjax.url, { params: params })
data = this.iAjax.processResults.bind(this)(data)
data.results.forEach((v) => {
this.hasObjects.push(v)
})
// 如果还有其它页,继续获取, 如果没有就停止
if (!data.pagination) {
this.params.hasMore = false
}
},
async getHasObjectsByIds() {
const resp = await createSourceIdCache(this.hasObjectsId)
this.params.spm = resp.spm
await this.loadHasObjects()
} }
} }
} }

View File

@@ -160,6 +160,7 @@ export default {
this.params.search = query this.params.search = query
this.getOptions() this.getOptions()
}, },
async getInitialOptions() { async getInitialOptions() {
const params = this.safeMakeParams(this.params) const params = this.safeMakeParams(this.params)
this.$log.debug('Get initial options: ', params) this.$log.debug('Get initial options: ', params)

View File

@@ -7,7 +7,7 @@
<DetailCard :title="cardTitle" :items="detailItems" /> <DetailCard :title="cardTitle" :items="detailItems" />
</el-col> </el-col>
<el-col :md="10" :sm="24"> <el-col :md="10" :sm="24">
<RelationCard v-if="!relationConfig.loading" v-bind="relationConfig" /> <RelationCard v-bind="relationConfig" />
</el-col> </el-col>
</el-row> </el-row>
</div> </div>
@@ -16,7 +16,6 @@
</template> </template>
<script> <script>
import { getUserGroupMembers } from '@/api/user'
import { GenericDetailPage } from '@/layout/components' import { GenericDetailPage } from '@/layout/components'
import { DetailCard, RelationCard } from '@/components' import { DetailCard, RelationCard } from '@/components'
@@ -50,21 +49,23 @@ export default {
relationConfig: { relationConfig: {
icon: 'fa-user', icon: 'fa-user',
title: this.$tc('Members'), title: this.$tc('Members'),
url: '/api/v1/users/users/?fields_size=mini', objectsAjax: {
objectsId: [], url: '/api/v1/users/users/?fields_size=mini'
objects: [ },
{ hasObjectsId: ['938b47a3-db34-40be-b732-ee0125a4857c', '3e3b75a9-11fd-4b97-ad04-17a50a72507c'],
id: '1', hasObjects: [
name: '周杰伦' // {
}, // id: '1',
{ // name: '周杰伦'
id: '2', // },
name: '昆凌' // {
}, // id: '2',
{ // name: '昆凌'
id: '3', // },
name: '周杰伦' // {
} // id: '3',
// name: '周杰伦'
// }
], ],
loading: true loading: true
}, },
@@ -94,14 +95,14 @@ export default {
} }
}, },
mounted() { mounted() {
getUserGroupMembers(this.$route.params.id).then(data => { // getUserGroupMembers(this.$route.params.id).then(data => {
for (const i of data) { // for (const i of data) {
this.relationConfig.objectsId.push(i.user) // this.relationConfig.objectsId.push(i.user)
} // }
console.log(this.relationConfig.objectsId) // console.log(this.relationConfig.objectsId)
}).finally(() => { // }).finally(() => {
this.relationConfig.loading = false // this.relationConfig.loading = false
}) // })
}, },
methods: { methods: {
} }