table-view.js 1.3 KB

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