uni-th.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <!-- #ifdef H5 -->
  3. <th :rowspan="rowspan" :colspan="colspan" class="uni-table-th" :class="{ 'table--border': border }" :style="{ width: customWidth + 'px', 'text-align': align }">
  4. <view class="uni-table-th-row">
  5. <view class="uni-table-th-content" :style="{ 'justify-content': contentAlign }" @click="sort">
  6. <slot></slot>
  7. <view v-if="sortable" class="arrow-box">
  8. <text class="arrow up" :class="{ active: ascending }" @click.stop="ascendingFn"></text>
  9. <text class="arrow down" :class="{ active: descending }" @click.stop="descendingFn"></text>
  10. </view>
  11. </view>
  12. <dropdown v-if="filterType || filterData.length" :filterData="filterData" :filterType="filterType" @change="ondropdown"></dropdown>
  13. </view>
  14. </th>
  15. <!-- #endif -->
  16. <!-- #ifndef H5 -->
  17. <view class="uni-table-th" :class="{ 'table--border': border }" :style="{ width: customWidth + 'px', 'text-align': align }"><slot></slot></view>
  18. <!-- #endif -->
  19. </template>
  20. <script>
  21. import dropdown from './filter-dropdown.vue'
  22. /**
  23. * Th 表头
  24. * @description 表格内的表头单元格组件
  25. * @tutorial https://ext.dcloud.net.cn/plugin?id=3270
  26. * @property {Number | String} width 单元格宽度(支持纯数字、携带单位px或rpx)
  27. * @property {Boolean} sortable 是否启用排序
  28. * @property {Number} align = [left|center|right] 单元格对齐方式
  29. * @value left 单元格文字左侧对齐
  30. * @value center 单元格文字居中
  31. * @value right 单元格文字右侧对齐
  32. * @property {Array} filterData 筛选数据
  33. * @property {String} filterType [search|select] 筛选类型
  34. * @value search 关键字搜素
  35. * @value select 条件选择
  36. * @event {Function} sort-change 排序触发事件
  37. */
  38. export default {
  39. name: 'uniTh',
  40. options: {
  41. virtualHost: true
  42. },
  43. components: {
  44. dropdown
  45. },
  46. emits:['sort-change','filter-change'],
  47. props: {
  48. width: {
  49. type: [String, Number],
  50. default: ''
  51. },
  52. align: {
  53. type: String,
  54. default: 'left'
  55. },
  56. rowspan: {
  57. type: [Number, String],
  58. default: 1
  59. },
  60. colspan: {
  61. type: [Number, String],
  62. default: 1
  63. },
  64. sortable: {
  65. type: Boolean,
  66. default: false
  67. },
  68. filterType: {
  69. type: String,
  70. default: ""
  71. },
  72. filterData: {
  73. type: Array,
  74. default () {
  75. return []
  76. }
  77. }
  78. },
  79. data() {
  80. return {
  81. border: false,
  82. ascending: false,
  83. descending: false
  84. }
  85. },
  86. computed: {
  87. // 根据props中的width属性 自动匹配当前th的宽度(px)
  88. customWidth(){
  89. if(typeof this.width === 'number'){
  90. return this.width
  91. } else if(typeof this.width === 'string') {
  92. let regexHaveUnitPx = new RegExp(/^[1-9][0-9]*px$/g)
  93. let regexHaveUnitRpx = new RegExp(/^[1-9][0-9]*rpx$/g)
  94. let regexHaveNotUnit = new RegExp(/^[1-9][0-9]*$/g)
  95. if (this.width.match(regexHaveUnitPx) !== null) { // 携带了 px
  96. return this.width.replace('px', '')
  97. } else if (this.width.match(regexHaveUnitRpx) !== null) { // 携带了 rpx
  98. let numberRpx = Number(this.width.replace('rpx', ''))
  99. let widthCoe = uni.getSystemInfoSync().screenWidth / 750
  100. return Math.round(numberRpx * widthCoe)
  101. } else if (this.width.match(regexHaveNotUnit) !== null) { // 未携带 rpx或px 的纯数字 String
  102. return this.width
  103. } else { // 不符合格式
  104. return ''
  105. }
  106. } else {
  107. return ''
  108. }
  109. },
  110. contentAlign() {
  111. let align = 'left'
  112. switch (this.align) {
  113. case 'left':
  114. align = 'flex-start'
  115. break
  116. case 'center':
  117. align = 'center'
  118. break
  119. case 'right':
  120. align = 'flex-end'
  121. break
  122. }
  123. return align
  124. }
  125. },
  126. created() {
  127. this.root = this.getTable('uniTable')
  128. this.rootTr = this.getTable('uniTr')
  129. this.rootTr.minWidthUpdate(this.customWidth ? this.customWidth : 140)
  130. this.border = this.root.border
  131. this.root.thChildren.push(this)
  132. },
  133. methods: {
  134. sort() {
  135. if (!this.sortable) return
  136. this.clearOther()
  137. if (!this.ascending && !this.descending) {
  138. this.ascending = true
  139. this.$emit('sort-change', { order: 'ascending' })
  140. return
  141. }
  142. if (this.ascending && !this.descending) {
  143. this.ascending = false
  144. this.descending = true
  145. this.$emit('sort-change', { order: 'descending' })
  146. return
  147. }
  148. if (!this.ascending && this.descending) {
  149. this.ascending = false
  150. this.descending = false
  151. this.$emit('sort-change', { order: null })
  152. }
  153. },
  154. ascendingFn() {
  155. this.clearOther()
  156. this.ascending = !this.ascending
  157. this.descending = false
  158. this.$emit('sort-change', { order: this.ascending ? 'ascending' : null })
  159. },
  160. descendingFn() {
  161. this.clearOther()
  162. this.descending = !this.descending
  163. this.ascending = false
  164. this.$emit('sort-change', { order: this.descending ? 'descending' : null })
  165. },
  166. clearOther() {
  167. this.root.thChildren.map(item => {
  168. if (item !== this) {
  169. item.ascending = false
  170. item.descending = false
  171. }
  172. return item
  173. })
  174. },
  175. ondropdown(e) {
  176. this.$emit("filter-change", e)
  177. },
  178. /**
  179. * 获取父元素实例
  180. */
  181. getTable(name) {
  182. let parent = this.$parent
  183. let parentName = parent.$options.name
  184. while (parentName !== name) {
  185. parent = parent.$parent
  186. if (!parent) return false
  187. parentName = parent.$options.name
  188. }
  189. return parent
  190. }
  191. }
  192. }
  193. </script>
  194. <style lang="scss">
  195. $border-color: #ebeef5;
  196. .uni-table-th {
  197. padding: 12px 10px;
  198. /* #ifndef APP-NVUE */
  199. display: table-cell;
  200. box-sizing: border-box;
  201. /* #endif */
  202. font-size: 14px;
  203. font-weight: bold;
  204. color: #909399;
  205. border-bottom: 1px $border-color solid;
  206. }
  207. .uni-table-th-row {
  208. /* #ifndef APP-NVUE */
  209. display: flex;
  210. /* #endif */
  211. flex-direction: row;
  212. }
  213. .table--border {
  214. border-right: 1px $border-color solid;
  215. }
  216. .uni-table-th-content {
  217. display: flex;
  218. align-items: center;
  219. flex: 1;
  220. }
  221. .arrow-box {
  222. }
  223. .arrow {
  224. display: block;
  225. position: relative;
  226. width: 10px;
  227. height: 8px;
  228. // border: 1px red solid;
  229. left: 5px;
  230. overflow: hidden;
  231. cursor: pointer;
  232. }
  233. .down {
  234. top: 3px;
  235. ::after {
  236. content: '';
  237. width: 8px;
  238. height: 8px;
  239. position: absolute;
  240. left: 2px;
  241. top: -5px;
  242. transform: rotate(45deg);
  243. background-color: #ccc;
  244. }
  245. &.active {
  246. ::after {
  247. background-color: #007aff;
  248. }
  249. }
  250. }
  251. .up {
  252. ::after {
  253. content: '';
  254. width: 8px;
  255. height: 8px;
  256. position: absolute;
  257. left: 2px;
  258. top: 5px;
  259. transform: rotate(45deg);
  260. background-color: #ccc;
  261. }
  262. &.active {
  263. ::after {
  264. background-color: #007aff;
  265. }
  266. }
  267. }
  268. </style>