DataTable.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <table>
  3. <thead>
  4. <tr>
  5. <th v-for="field in fields">{{ field.displayName }}</th>
  6. </tr>
  7. </thead>
  8. <tbody>
  9. <tr v-for="data in computedData">
  10. <td v-for="field in fields">
  11. <span v-if="isFieldSlot(field.name)">
  12. <slot :name="field.name" :data="data"></slot>
  13. </span>
  14. <span v-else>
  15. {{ data[field.name] }}
  16. </span>
  17. </td>
  18. </tr>
  19. </tbody>
  20. </table>
  21. </template>
  22. <script>
  23. export default {
  24. components: {},
  25. data: () => {
  26. return {
  27. }
  28. },
  29. computed: {
  30. computedData: function() {
  31. let data = this.data;
  32. this.sortOrder.reverse().forEach(orderItem => {
  33. data = data.sort((a, b) => {
  34. if (orderItem.order === "desc") return a[orderItem.field] < b[orderItem.field];
  35. else return a[orderItem.field] > b[orderItem.field];
  36. });
  37. });
  38. return data;
  39. }
  40. },
  41. props: {
  42. data: Array,
  43. fields: Array,
  44. sortOrder: Array
  45. },
  46. methods: {
  47. isFieldSlot(name) {
  48. return !!this.$scopedSlots[name];
  49. }
  50. },
  51. mounted() {
  52. }
  53. };
  54. </script>
  55. <style lang="scss" scoped>
  56. table {
  57. background-color: white;
  58. }
  59. th, td {
  60. padding: 8px;
  61. }
  62. th {
  63. background-color: #eee;
  64. &:hover {
  65. background-color: #ddd;
  66. }
  67. }
  68. td {
  69. background-color: white;
  70. &:hover {
  71. background-color: #eee;
  72. }
  73. }
  74. table, th, td {
  75. border: 1px solid black;
  76. border-collapse: collapse;
  77. }
  78. </style>