123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <template>
- <view class="jnpf-relation-form">
- <selectBox v-model="innerValue" :placeholder="placeholder" @openSelect="openSelect"></selectBox>
- <DisplayList :extraObj="extraObj" :extraOptions="extraOptions"
- v-if="Object.keys(extraObj).length && innerValue">
- </DisplayList>
- </view>
- </template>
- <script>
- import {
- getRelationFormDetail
- } from '@/api/common.js'
- import DisplayList from '@/components/displayList'
- import selectBox from '@/components/selectBox'
- import {
- useBaseStore
- } from '@/store/modules/base'
- const baseStore = useBaseStore()
- export default {
- name: 'jnpf-relation-form',
- components: {
- DisplayList,
- selectBox
- },
- props: {
- modelValue: {
- default: ''
- },
- placeholder: {
- type: String,
- default: '请选择'
- },
- formType: {
- type: String,
- default: 'select'
- },
- disabled: {
- type: Boolean,
- default: false
- },
- columnOptions: {
- type: Array,
- default: []
- },
- extraOptions: {
- type: Array,
- default: () => []
- },
- relationField: {
- type: String,
- default: ''
- },
- propsValue: {
- type: String,
- default: ''
- },
- modelId: {
- type: String,
- default: ''
- },
- hasPage: {
- type: Boolean,
- default: false
- },
- pageSize: {
- type: Number,
- default: 10000
- },
- vModel: {
- type: String,
- default: ''
- },
- popupTitle: {
- type: String,
- default: ''
- },
- queryType: {
- type: Number,
- default: 1
- }
- },
- data() {
- return {
- selectShow: false,
- innerValue: '',
- defaultValue: '',
- current: null,
- defaultOptions: [],
- firstVal: '',
- firstId: 0,
- extraObj: {}
- }
- },
- watch: {
- modelValue(val) {
- this.setDefault()
- },
- },
- created() {
- uni.$on('relationConfirm', (subVal, innerValue, list, selectRow) => {
- this.confirm(subVal, innerValue, list, selectRow)
- })
- this.setDefault()
- },
- methods: {
- setDefault() {
- if (!this.modelId || !this.vModel) return
- if (!this.modelValue) {
- this.innerValue = ''
- let relationData = baseStore.relationData
- relationData[this.vModel] = {}
- baseStore.updateRelationData(relationData)
- return
- }
- let query = {
- id: this.modelValue,
- };
- if (this.propsValue) query = {
- ...query,
- propsValue: this.propsValue
- };
- getRelationFormDetail(this.modelId, query).then(res => {
- if (!res.data || !res.data.data) return
- let data = JSON.parse(res.data.data)
- this.extraObj = data
- this.innerValue = data[this.relationField] ? data[this.relationField] : this
- .modelValue
- let relationData = baseStore.relationData
- relationData[this.vModel] = data
- baseStore.updateRelationData(relationData)
- })
- },
- openSelect() {
- if (this.disabled) {
- if (!this.modelValue) return
- let config = {
- modelId: this.modelId,
- id: this.modelValue,
- formTitle: '详情',
- noShowBtn: 1,
- noDataLog: 1
- }
- this.$nextTick(() => {
- const url =
- '/pages/apply/dynamicModel/detail?config=' +
- this.jnpf.base64.encode(JSON.stringify(config))
- uni.navigateTo({
- url: url
- })
- })
- return
- }
- let data = {
- columnOptions: this.columnOptions,
- relationField: this.relationField,
- type: 'relation',
- propsValue: this.propsValue,
- modelId: this.modelId,
- hasPage: this.hasPage,
- pageSize: this.pageSize,
- id: this.modelValue,
- vModel: this.vModel,
- popupTitle: this.popupTitle || '选择数据',
- innerValue: this.innerValue,
- queryType: this.queryType
- }
- uni.navigateTo({
- url: '/pages/apply/popSelect/index?data=' + encodeURIComponent(JSON.stringify(
- data))
- })
- },
- confirm(subVal, innerValue, vModel, selectRow) {
- if (vModel !== this.vModel) return; // 早期返回,如果条件不满足,则直接退出
- this.firstVal = innerValue;
- this.firstId = subVal;
- // 确定 innerValue 的值
- let innerValueStr = !this.propsValue ? innerValue + '' : selectRow[this.propsValue];
- this.innerValue = selectRow[this.relationField] || "";
- // 触发 update:modelValue 事件
- this.$emit('update:modelValue', selectRow[this.propsValue]);
- // 触发 change 事件
- this.$emit('change', subVal, selectRow);
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .jnpf-relation-form {
- width: 100%;
- height: 100%;
- }
- </style>
|