Dataset.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <div class="dataset-component">
  3. <div class="form-item mt-8">
  4. <label>数据集名称</label>
  5. <t-input v-model="modelValue.name" placeholder="名称" />
  6. </div>
  7. <div class="form-item mt-8">
  8. <label>数据方式</label>
  9. <t-radio-group v-model="modelValue.mode">
  10. <t-radio value="api">HTTP请求</t-radio>
  11. <t-radio value="">自定义</t-radio>
  12. </t-radio-group>
  13. </div>
  14. <div v-if="modelValue.mode === 'api'" class="form-item mt-8">
  15. <label>URL地址</label>
  16. <t-input v-model="modelValue.url" @blur="getDatas" @enter="getDatas" />
  17. </div>
  18. <template v-else>
  19. <div class="form-item mt-8">
  20. <label>从Excel导入</label>
  21. <div>
  22. <t-button
  23. class="shrink-0"
  24. style="width: 90px; height: 30px"
  25. @click="importDataset"
  26. >
  27. 导入Excel
  28. </t-button>
  29. <a href="/data.xlsx" download class="ml-12 mt-4 nowrap">
  30. 下载Excel示例
  31. </a>
  32. </div>
  33. </div>
  34. </template>
  35. <t-table
  36. class="mt-16"
  37. row-key="id"
  38. :data="modelValue.data"
  39. :columns="datasetColumns"
  40. size="small"
  41. >
  42. <template #type="{ row }">
  43. {{ row.type || 'string' }}
  44. </template>
  45. <template v-if="!modelValue.mode" #actions="{ row, rowIndex }">
  46. <t-icon name="edit" class="hover" @click="showAddData(row)" />
  47. <t-icon
  48. name="delete"
  49. class="ml-12 hover"
  50. @click="modelValue.data.splice(rowIndex, 1)"
  51. />
  52. </template>
  53. </t-table>
  54. <t-dialog
  55. v-if="addDataDialog.show"
  56. :visible="true"
  57. class="data-dialog"
  58. :header="addDataDialog.header"
  59. @close="addDataDialog.show = false"
  60. @confirm="onOkAddData"
  61. >
  62. <div class="form-item mt-16">
  63. <label>数据点名称</label>
  64. <t-input v-model="addDataDialog.data.label" placeholder="简短描述" />
  65. </div>
  66. <div class="form-item mt-16">
  67. <label>数据点ID</label>
  68. <t-input v-model="addDataDialog.data.id" placeholder="数据点ID" />
  69. </div>
  70. <div class="form-item mt-16">
  71. <label>类型</label>
  72. <t-select
  73. class="w-full"
  74. :options="typeOptions"
  75. v-model="addDataDialog.data.type"
  76. placeholder="字符串"
  77. @change="addDataDialog.data.value = null"
  78. />
  79. </div>
  80. </t-dialog>
  81. </div>
  82. </template>
  83. <script lang="ts" setup>
  84. import { onBeforeMount, reactive, ref } from 'vue';
  85. import axios from 'axios';
  86. import { MessagePlugin } from 'tdesign-vue-next';
  87. import { importExcel } from '@/services/excel';
  88. import { typeOptions } from '@/services/common';
  89. const { modelValue } = defineProps<{
  90. modelValue: any;
  91. }>();
  92. const emit = defineEmits(['update:modelValue', 'change']);
  93. const datasetColumns = ref([
  94. {
  95. colKey: 'label',
  96. title: '数据点名称',
  97. ellipsis: true,
  98. },
  99. {
  100. colKey: 'id',
  101. title: '数据点ID',
  102. ellipsis: true,
  103. },
  104. {
  105. colKey: 'type',
  106. title: '类型',
  107. ellipsis: true,
  108. },
  109. {
  110. colKey: 'actions',
  111. title: '操作',
  112. width: 100,
  113. },
  114. ]);
  115. const addDataDialog = reactive<any>({});
  116. onBeforeMount(() => {
  117. if (!modelValue.data) {
  118. modelValue.data = [];
  119. }
  120. getDatas();
  121. });
  122. const getDatas = async () => {
  123. if (!modelValue.url) {
  124. return;
  125. }
  126. const ret = await axios.get(modelValue.url);
  127. if (ret) {
  128. modelValue.data = ret;
  129. }
  130. };
  131. const importDataset = async () => {
  132. let columns: any = [
  133. {
  134. header: '数据点名称',
  135. key: 'label',
  136. },
  137. {
  138. header: '数据点ID',
  139. key: 'id',
  140. },
  141. {
  142. header: '类型',
  143. key: 'type',
  144. },
  145. ];
  146. const data: any = await importExcel(columns);
  147. modelValue.data = data;
  148. emit('update:modelValue', modelValue);
  149. emit('change', modelValue);
  150. };
  151. const showAddData = (row?: any) => {
  152. if (row) {
  153. addDataDialog.header = '编辑数据';
  154. addDataDialog.data = row;
  155. } else {
  156. addDataDialog.header = '添加数据';
  157. addDataDialog.data = { type: 'string' };
  158. }
  159. addDataDialog.show = true;
  160. };
  161. const onOkAddData = () => {
  162. if (!addDataDialog.data.label) {
  163. MessagePlugin.error('请填写名称');
  164. return;
  165. }
  166. if (!addDataDialog.data.key) {
  167. MessagePlugin.error('请填写数据ID');
  168. return;
  169. }
  170. modelValue.data.push(addDataDialog.data);
  171. addDataDialog.show = false;
  172. };
  173. </script>
  174. <style lang="postcss" scoped>
  175. .dataset-component {
  176. padding-right: 16px;
  177. }
  178. </style>