uni-th.vue 6.9 KB

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