uni-th.vue 6.3KB

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