StdSelector.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <script setup lang="ts">
  2. import {onMounted, reactive, ref, watch} from 'vue'
  3. import StdTable from '@/components/StdDataDisplay/StdTable.vue'
  4. import gettext from '@/gettext'
  5. const {$gettext} = gettext
  6. const props = defineProps(['selectedKey', 'value', 'recordValueIndex',
  7. 'selectionType', 'api', 'columns', 'data_key',
  8. 'disable_search', 'get_params', 'description'])
  9. const emit = defineEmits(['update:selectedKey', 'changeSelect'])
  10. const visible = ref(false)
  11. const M_value = ref('')
  12. onMounted(() => {
  13. init()
  14. })
  15. const selected = ref([])
  16. const record: any = reactive({})
  17. function init() {
  18. if (props.selectedKey && !props.value && props.selectionType === 'radio') {
  19. props.api.get(props.selectedKey).then((r: any) => {
  20. Object.assign(record, r)
  21. M_value.value = r[props.recordValueIndex]
  22. })
  23. }
  24. }
  25. function show() {
  26. visible.value = true
  27. }
  28. function onSelect(_selected: any) {
  29. selected.value = _selected
  30. }
  31. function onSelectedRecord(r: any) {
  32. Object.assign(record, r)
  33. }
  34. function ok() {
  35. visible.value = false
  36. if (props.selectionType == 'radio') {
  37. emit('update:selectedKey', selected.value[0])
  38. } else {
  39. emit('update:selectedKey', selected.value)
  40. }
  41. M_value.value = record[props.recordValueIndex]
  42. emit('changeSelect', record)
  43. }
  44. watch(props, () => {
  45. if (!props?.selectedKey) {
  46. M_value.value = ''
  47. } else if (props.value) {
  48. M_value.value = props.value
  49. } else {
  50. init()
  51. }
  52. })
  53. </script>
  54. <template>
  55. <div class="std-selector-container">
  56. <div class="std-selector" @click="show()">
  57. <a-input v-model="selectedKey" disabled hidden/>
  58. <div class="value">
  59. {{ M_value }}
  60. </div>
  61. <a-modal
  62. :mask="false"
  63. :visible="visible"
  64. :cancel-text="$gettext('Cancel')"
  65. :ok-text="$gettext('OK')"
  66. :title="$gettext('Selector')"
  67. @cancel="visible=false"
  68. @ok="ok()"
  69. :width="800"
  70. destroyOnClose
  71. >
  72. {{ description }}
  73. <std-table
  74. :api="api"
  75. :columns="columns"
  76. :data_key="data_key"
  77. :disable_search="disable_search"
  78. :pithy="true"
  79. :get_params="get_params"
  80. :selectionType="selectionType"
  81. :disable_query_params="true"
  82. @onSelected="onSelect"
  83. @onSelectedRecord="onSelectedRecord"
  84. />
  85. </a-modal>
  86. </div>
  87. </div>
  88. </template>
  89. <style lang="less" scoped>
  90. .std-selector-container {
  91. height: 39.9px;
  92. display: flex;
  93. align-items: flex-start;
  94. .std-selector {
  95. box-sizing: border-box;
  96. font-variant: tabular-nums;
  97. list-style: none;
  98. font-feature-settings: 'tnum';
  99. height: 32px;
  100. padding: 4px 11px;
  101. color: rgba(0, 0, 0, 0.85);
  102. font-size: 14px;
  103. line-height: 1.5;
  104. background-color: #fff;
  105. background-image: none;
  106. border: 1px solid #d9d9d9;
  107. border-radius: 4px;
  108. transition: all 0.3s;
  109. margin: 0 10px 0 0;
  110. cursor: pointer;
  111. min-width: 180px;
  112. @media (prefers-color-scheme: dark) {
  113. background-color: #1e1f20;
  114. border: 1px solid #666666;
  115. color: rgba(255, 255, 255, 0.99);
  116. }
  117. .value {
  118. }
  119. }
  120. }
  121. </style>