wd-table-col.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <view
  3. :class="`wd-table-col ${fixed ? 'wd-table-col--fixed' : ''} ${isLastFixed && isDef(table) && table.state.scrollLeft ? 'is-shadow' : ''}`"
  4. :style="columnStyle"
  5. >
  6. <view
  7. :class="`wd-table__cell ${stripe && isOdd(index) ? 'is-stripe' : ''} ${border ? 'is-border' : ''} is-${align}`"
  8. v-for="(row, index) in column"
  9. :key="index"
  10. :style="cellStyle"
  11. @click="handleRowClick(index)"
  12. >
  13. <slot name="value" v-if="$slots.value" :row="getScope(index)" :index="index"></slot>
  14. <text :class="`wd-table__value ${ellipsis ? 'is-ellipsis' : ''}`" v-else>{{ row }}</text>
  15. </view>
  16. </view>
  17. </template>
  18. <script lang="ts">
  19. export default {
  20. name: 'wd-table-col',
  21. options: {
  22. addGlobalClass: true,
  23. virtualHost: true,
  24. styleIsolation: 'shared'
  25. }
  26. }
  27. </script>
  28. <script lang="ts" setup>
  29. import { type CSSProperties, computed, ref } from 'vue'
  30. import { addUnit, isDef, objToStyle, isOdd, isFunction } from '../common/util'
  31. import { tableColumnProps, type SortDirection } from './types'
  32. import { useParent } from '../composables/useParent'
  33. import { TABLE_KEY } from '../wd-table/types'
  34. const props = defineProps(tableColumnProps)
  35. const { parent: table, index: columnIndex } = useParent(TABLE_KEY)
  36. const sortDirection = ref<SortDirection>(0) // 排序方向
  37. // 是否开启斑马纹
  38. const stripe = computed(() => {
  39. if (isDef(table)) {
  40. return table.props.stripe
  41. } else {
  42. return false
  43. }
  44. })
  45. /**
  46. * 是否有边框
  47. */
  48. const border = computed(() => {
  49. if (isDef(table)) {
  50. return table.props.border
  51. } else {
  52. return false
  53. }
  54. })
  55. /**
  56. * 是否超出省略
  57. */
  58. const ellipsis = computed(() => {
  59. if (isDef(table)) {
  60. return table.props.ellipsis
  61. } else {
  62. return false
  63. }
  64. })
  65. /**
  66. * 是否最后一个固定元素
  67. */
  68. const isLastFixed = computed(() => {
  69. let isLastFixed: boolean = false
  70. if (props.fixed && isDef(table)) {
  71. isLastFixed = table.getIsLastFixed(props)
  72. }
  73. return isLastFixed
  74. })
  75. /**
  76. * 列样式
  77. */
  78. const columnStyle = computed(() => {
  79. let style: CSSProperties = {}
  80. if (isDef(props.width)) {
  81. style['width'] = addUnit(props.width)
  82. }
  83. if (props.fixed && isDef(table) && isFunction(table.getFixedStyle)) {
  84. style = table.getFixedStyle(columnIndex.value, style)
  85. }
  86. return style
  87. })
  88. /**
  89. * 单元格样式
  90. */
  91. const cellStyle = computed(() => {
  92. let style: CSSProperties = {}
  93. const rowHeight: string | number = isDef(table) && isDef(table.props) ? table.props.rowHeight : 50 // 自定义行高
  94. if (isDef(rowHeight)) {
  95. style['height'] = addUnit(rowHeight)
  96. }
  97. if (props.fixed && isDef(table) && isFunction(table.getFixedStyle)) {
  98. style = table.getFixedStyle(columnIndex.value, style)
  99. }
  100. return objToStyle(style)
  101. })
  102. // 列数据
  103. const column = computed(() => {
  104. if (!isDef(table) || !isDef(table.props) || !isDef(table.props.data) || !Array.isArray(table.props.data)) {
  105. return []
  106. }
  107. const column: any[] = table.props.data.map((item) => {
  108. return item[props.prop]
  109. })
  110. return column
  111. })
  112. /**
  113. * 行点击事件
  114. * @param index 行下标
  115. */
  116. function handleRowClick(index: number) {
  117. if (!isDef(table)) {
  118. return
  119. }
  120. isFunction(table.rowClick) && table.rowClick(index)
  121. }
  122. // 行数据
  123. function getScope(index: number) {
  124. if (!isDef(table) || !isDef(table.props) || !isDef(table.props.data) || !Array.isArray(table.props.data)) {
  125. return {}
  126. }
  127. return table.props.data[index] || {}
  128. }
  129. defineExpose({ sortDirection: sortDirection })
  130. </script>
  131. <style lang="scss" scoped>
  132. @import './index.scss';
  133. </style>