table-view.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { computed, h } from 'vue'
  2. import { isObject, isArray } from '@cip/utils/util'
  3. import {fromInputEmits} from "@cip/components/cip-form-input/form-input-props";
  4. // 异步获取options由上层提供 不支持按条件搜索
  5. export default {
  6. props: {
  7. modelValue: [String, Number],
  8. config: Object,
  9. tableOptions: Array
  10. },
  11. emits: [...fromInputEmits],
  12. setup (props) {
  13. const options = computed(() => {
  14. return props.tableOptions || props.config.options || []
  15. })
  16. const optionIsObject = computed(() => {
  17. return isObject(options.value[0])
  18. })
  19. const optionProps = computed(() => {
  20. return Object.assign({
  21. label: 'label',
  22. value: 'value'
  23. }, props.config?.optionProps)
  24. })
  25. const marryOption = (value) => {
  26. const item = options.value.filter(v => v[optionProps.value.value] === value)[0]
  27. return item ? item[optionProps.value.label] : ''
  28. }
  29. const viewValue = computed(() => {
  30. if (isArray(props.modelValue)) {
  31. if (optionIsObject.value) {
  32. return props.modelValue.map(i => marryOption(i, i)).join(props.config.splitKey || ',')
  33. } else {
  34. return props.modelValue.join(props.config.splitKey || ',')
  35. }
  36. }
  37. if (options.value.length !== 0 && optionIsObject.value) {
  38. marryOption(props.modelValue)
  39. }
  40. return props.modelValue
  41. })
  42. return () => h('span', {}, [viewValue.value])
  43. }
  44. }