visual-table.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!-- -->
  2. <template>
  3. <HcTable
  4. ref="tableRef"
  5. :column="tableColumn" :datas="tableData" :loading="tableLoaing"
  6. is-new :index-style="{ width: 60 }" :is-check="isCheck" :check-style="{ width: 29 }"
  7. @selection-change="tableSelection"
  8. />
  9. </template>
  10. <script setup>
  11. import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
  12. const props = defineProps({
  13. tableData: {
  14. type: Array,
  15. default: () => ([]),
  16. },
  17. isCheck:{
  18. type:Boolean,
  19. default:true,
  20. },
  21. })
  22. //事件
  23. const emit = defineEmits(['getTableKeys'])
  24. const isCheck = ref(props.isCheck)
  25. const allData = ref(props.tableData)
  26. const needle = ref(10)//数据指针 默认19
  27. const tableData = ref([])
  28. const tableLoaing = ref(false)
  29. //多选
  30. const tableKeys = ref([])
  31. const tableSelection = (rows) => {
  32. tableKeys.value = rows
  33. emit('getTableKeys', rows)
  34. }
  35. const tableSelectAll = (val)=>{
  36. if (val) {
  37. // tableData.value.forEach((ele1)=>{
  38. // nextTick(()=>{
  39. // tableRef.value.toggleRowSelection( ele1, true )
  40. // })
  41. // })
  42. tableKeys.value = allData.value
  43. emit('getTableKeys', allData.value )
  44. } else {
  45. tableRef.value.clearSelection()
  46. tableKeys.value = []
  47. emit('getTableKeys', tableKeys.value )
  48. }
  49. }
  50. //表头
  51. const tableRef = ref(null)
  52. const tableColumn = ref([
  53. { key:'fileNumber', name: '档号', width: 180 },
  54. { key:'name', name: '案卷题名' },
  55. { key:'pageN', name: '总页数', width: 120 },
  56. { key:'storageTimeValue', name: '保管期限', width: 120 },
  57. { key:'remark', name: '备注' },
  58. ])
  59. //监听
  60. watch(() => [
  61. props.tableData,
  62. props.isCheck,
  63. ], ([Data, Check]) => {
  64. allData.value = Data
  65. isCheck.value = Check
  66. tableData.value = []
  67. //判断数据长度有没有9个,有就先添加9个,没有直接获取所有数据
  68. if (allData.value.length > 9) {
  69. for (let i = 0;i < needle.value ;i++) {
  70. tableData.value[i] = allData.value[i]
  71. }
  72. } else {
  73. tableData.value = allData.value
  74. }
  75. },
  76. { immediate: true, deep: true })
  77. onMounted(()=>{
  78. tableRef.value && tableRef.value.tableRef && tableRef.value.tableRef.$refs.bodyWrapper.addEventListener('mousewheel', scrollBehavior)
  79. })
  80. onUnmounted(() => {
  81. // 卸载
  82. tableRef.value && tableRef.value.tableRef && tableRef.value.tableRef.$refs.bodyWrapper.removeEventListener('mousewheel', scrollBehavior)
  83. })
  84. const queryData = async ()=>{
  85. console.log('查询数据')
  86. let ortab = tableData.value
  87. if (needle.value < allData.value.length) {
  88. let pusharr = allData.value.slice(needle.value, needle.value + 10)
  89. pusharr.forEach((ele)=>{
  90. ortab.push(ele)
  91. })
  92. needle.value = needle.value + 10
  93. return ortab
  94. }
  95. }
  96. const scrollBehavior = async (e)=>{
  97. // 滚动方向判定
  98. const scrollDirection = e.deltaY > 0 ? 'down' : 'up'
  99. if (scrollDirection === 'down') {
  100. // 获取提供实际滚动的容器
  101. const dom = tableRef.value.tableRef.$refs.bodyWrapper.getElementsByClassName('el-scrollbar__wrap')[0]
  102. const { clientHeight, scrollTop, scrollHeight } = dom
  103. // 父容器高度 + 子容器距离父容器顶端的高度 = 子容器可滚动的高度
  104. if (clientHeight + scrollTop === scrollHeight) {
  105. console.log('竖向滚动条已经滚动到底部')
  106. const res = await queryData()
  107. if (res) {
  108. tableData.value = res
  109. }
  110. }
  111. }
  112. }
  113. // 暴露出去
  114. defineExpose({
  115. tableSelectAll,
  116. })
  117. </script>
  118. <style lang='scss' scoped>
  119. </style>