SimpleTable.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <div class="borderless-table">
  3. <table v-if="data.length">
  4. <thead>
  5. <tr>
  6. <th v-for="(key, index) in keys" :key="index">
  7. {{ key }}
  8. </th>
  9. </tr>
  10. </thead>
  11. <tbody>
  12. <tr v-for="(item, rowIndex) in data" :key="rowIndex">
  13. <td v-for="(key, colIndex) in keys" :key="colIndex">
  14. {{ item[key] }}
  15. </td>
  16. </tr>
  17. </tbody>
  18. </table>
  19. <div v-else class="empty">暂无数据</div>
  20. </div>
  21. </template>
  22. <script setup lang="ts">
  23. import { computed } from 'vue'
  24. interface CellDataType {
  25. [title: string]: string
  26. }
  27. const props = defineProps({
  28. data: {
  29. type: Array as PropType<CellDataType[]>,
  30. default: () => []
  31. }
  32. })
  33. const keys = computed(() => {
  34. return props.data.length ? Object.keys(props.data[0]) : []
  35. })
  36. </script>
  37. <style scoped>
  38. .borderless-table table {
  39. width: 100%;
  40. border-collapse: collapse;
  41. }
  42. .borderless-table th,
  43. .borderless-table td {
  44. padding: 8px 12px;
  45. text-align: left;
  46. border: none;
  47. }
  48. .borderless-table thead {
  49. font-weight: bold;
  50. background-color: #f9f9f9;
  51. }
  52. .borderless-table .empty {
  53. text-align: center;
  54. color: #aaa;
  55. padding: 20px;
  56. }
  57. </style>