uni-th.vue 6.9 KB

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