1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| <template> <div class="mix-table"> <div style="width: 100%; display: flex"> <div v-for="item in attach" style="display: flex; flex: 1" class="border"> <div style="margin-right: 10px">{{ item.title }}:</div> <div>{{ item.value }}</div> </div> </div> <t-table :columns="newColumns" :data="data" style="width: 100%" :loading="loading" :bordered="!isEmpty(data)" ></t-table> </div> </template>
<script lang="ts" setup> import { defineProps, watch, ref } from 'vue'; import { isEmpty, max, min } from 'lodash'; import { getTextWidth } from '@/utils/index'; const props = defineProps<{ loading: boolean; columns: { title: string; colKey: string }[]; attach: { title: string; value: string }[]; data: any[]; }>(); const newColumns = ref([]); watch( () => props.data, (newVal) => { if (!isEmpty(newVal)) { newVal.forEach((item) => { Object.entries(item).forEach(([key, value]) => { const index = newColumns.value.findIndex((citem) => citem.title === key); const width = max([getTextWidth(key, 14), getTextWidth((value || '') as string, 14)]) + 34; // 不存在 if (index === -1) { newColumns.value.push({ title: key, colKey: key, width: width, }); } // 存在则比较 else { if (width > newColumns.value[index].width) { // 最大取250 newColumns.value[index].width = min([width, 250]); } } }); }); newColumns.value.unshift(newColumns.value.pop()); newColumns.value.unshift({ colKey: 'serial-number', width: 60, title: '序号' }); } else { newColumns.value = []; } }, ); </script> <style> .mix-table .t-table ::-webkit-scrollbar { width: 10px !important; height: 10px !important; } </style> <style scoped> .border { border: 1px solid var(--td-component-border); color: var(--td-text-color-placeholder); padding: 10px; } </style>
|