SdTableBody.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <div v-bind="containerProps" style="height: 200px" class="sd-body">
  3. <div v-bind="wrapperProps">
  4. <div v-for="item in list"
  5. :key="item.index"
  6. class="row"
  7. :class="rowClass(item)"
  8. @mouseenter="isHoverIndex = item.index"
  9. @mouseleave="isHoverIndex = -1"
  10. @click="rowClick(item)"
  11. style="height: 23px">
  12. <template v-for="(he,heindex) in tempColumns">
  13. <div class="cell"
  14. :style="he.fixedStyle"
  15. :title="getTitle(item,he)">
  16. <template v-if="he.type">
  17. <template v-if="he.type === 'selected'">
  18. <el-checkbox v-model="item.data.$selected"/>
  19. </template>
  20. <template v-else-if="he.type === 'index'">
  21. {{ item.index + 1 }}
  22. </template>
  23. </template>
  24. <template v-else>
  25. <component :is="createTd(item,he)"/>
  26. </template>
  27. </div>
  28. </template>
  29. </div>
  30. </div>
  31. </div>
  32. </template>
  33. <script setup name='SdTableBody' lang="tsx">
  34. import {defineProps, ref, onMounted, nextTick} from 'vue'
  35. import {useVirtualList} from "@vueuse/core";
  36. const {data, tempColumns, columns, sdMitt} = defineProps({
  37. data: {
  38. type: Array,
  39. default: []
  40. },
  41. tempColumns: {
  42. type: Array,
  43. default: []
  44. },
  45. columns: {
  46. type: Array,
  47. default: []
  48. },
  49. sdMitt: {
  50. type: Object
  51. },
  52. scrollConfig: {
  53. type: Object,
  54. default: {
  55. x: '9px',
  56. y: '9px'
  57. }
  58. }
  59. })
  60. const {list, containerProps, wrapperProps, scrollTo} = useVirtualList(
  61. data,
  62. {
  63. itemHeight: 23,
  64. },
  65. )
  66. const createTd = (row, columnRow): any => {
  67. let {data, index} = row
  68. if (columnRow.editCell) {
  69. try {
  70. return <div>{columnRow.renderCell(data, index)}</div>;
  71. } catch {
  72. return <div>{data[columnRow.code]}</div>
  73. }
  74. } else {
  75. return <div>{data[columnRow.code]}</div>
  76. }
  77. }
  78. const getTitle = ({data, index}, columnRow) => {
  79. try {
  80. return data[columnRow.code];
  81. } catch {
  82. return '';
  83. }
  84. }
  85. let oldRow = {
  86. data: {$edit: false}
  87. }
  88. const rowClick = (item) => {
  89. oldRow.data.$edit = false
  90. item.data.$edit = true
  91. oldRow = item
  92. }
  93. const isHoverIndex = ref(0)
  94. const rowClass = ({data, index}) => {
  95. if (isHoverIndex.value === index) {
  96. return 'is-hovered'
  97. }
  98. }
  99. let scrollTop = 0;
  100. onMounted(async () => {
  101. await nextTick()
  102. sdMitt.on('setScrollTop', () => {
  103. containerProps.ref.value.scrollTop = scrollTop
  104. })
  105. containerProps.ref.value.addEventListener('scroll', (val) => {
  106. scrollTop = containerProps.ref.value.scrollTop
  107. sdMitt.emit('changeScrollX', containerProps.ref.value.scrollLeft)
  108. })
  109. })
  110. const changeData = async () => {
  111. await nextTick()
  112. sdMitt.emit('changeScrollY', containerProps.ref.value.scrollHeight > containerProps.ref.value.clientHeight)
  113. }
  114. defineExpose({
  115. changeData
  116. })
  117. </script>
  118. <style lang="scss">
  119. .sd-body {
  120. background-color: white;
  121. &::-webkit-scrollbar {
  122. height: v-bind('scrollConfig.x');
  123. width: v-bind('scrollConfig.y');
  124. cursor: pointer;
  125. }
  126. .is-hovered {
  127. .cell {
  128. background-color: #f5f7fa !important;
  129. }
  130. }
  131. .row {
  132. height: 22px;
  133. display: flex;
  134. align-items: center;
  135. .cell {
  136. box-sizing: border-box;
  137. overflow: hidden;
  138. display: flex;
  139. align-items: center;
  140. text-overflow: ellipsis;
  141. white-space: nowrap;
  142. height: 100%;
  143. flex-grow: 0;
  144. flex-shrink: 0;
  145. background-color: inherit;
  146. color: inherit;
  147. border-bottom: 1px solid #ebeef5;
  148. padding: 0 5px;
  149. }
  150. }
  151. }
  152. </style>