uni-th.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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" :filterDefaultValue="filterDefaultValue" :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. // #ifdef MP-TOUTIAO
  44. virtualHost: false,
  45. // #endif
  46. // #ifndef MP-TOUTIAO
  47. virtualHost: true
  48. // #endif
  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. filterDefaultValue: {
  88. type: [Array,String],
  89. default () {
  90. return ""
  91. }
  92. }
  93. },
  94. data() {
  95. return {
  96. border: false,
  97. ascending: false,
  98. descending: false
  99. }
  100. },
  101. computed: {
  102. // 根据props中的width属性 自动匹配当前th的宽度(px)
  103. customWidth(){
  104. if(typeof this.width === 'number'){
  105. return this.width
  106. } else if(typeof this.width === 'string') {
  107. let regexHaveUnitPx = new RegExp(/^[1-9][0-9]*px$/g)
  108. let regexHaveUnitRpx = new RegExp(/^[1-9][0-9]*rpx$/g)
  109. let regexHaveNotUnit = new RegExp(/^[1-9][0-9]*$/g)
  110. if (this.width.match(regexHaveUnitPx) !== null) { // 携带了 px
  111. return this.width.replace('px', '')
  112. } else if (this.width.match(regexHaveUnitRpx) !== null) { // 携带了 rpx
  113. let numberRpx = Number(this.width.replace('rpx', ''))
  114. // #ifdef MP-WEIXIN
  115. let widthCoe = uni.getWindowInfo().screenWidth / 750
  116. // #endif
  117. // #ifndef MP-WEIXIN
  118. let widthCoe = uni.getSystemInfoSync().screenWidth / 750
  119. // #endif
  120. return Math.round(numberRpx * widthCoe)
  121. } else if (this.width.match(regexHaveNotUnit) !== null) { // 未携带 rpx或px 的纯数字 String
  122. return this.width
  123. } else { // 不符合格式
  124. return ''
  125. }
  126. } else {
  127. return ''
  128. }
  129. },
  130. contentAlign() {
  131. let align = 'left'
  132. switch (this.align) {
  133. case 'left':
  134. align = 'flex-start'
  135. break
  136. case 'center':
  137. align = 'center'
  138. break
  139. case 'right':
  140. align = 'flex-end'
  141. break
  142. }
  143. return align
  144. }
  145. },
  146. created() {
  147. this.root = this.getTable('uniTable')
  148. this.rootTr = this.getTable('uniTr')
  149. this.rootTr.minWidthUpdate(this.customWidth ? this.customWidth : 140)
  150. this.border = this.root.border
  151. this.root.thChildren.push(this)
  152. },
  153. methods: {
  154. sort() {
  155. if (!this.sortable) return
  156. this.clearOther()
  157. if (!this.ascending && !this.descending) {
  158. this.ascending = true
  159. this.$emit('sort-change', { order: 'ascending' })
  160. return
  161. }
  162. if (this.ascending && !this.descending) {
  163. this.ascending = false
  164. this.descending = true
  165. this.$emit('sort-change', { order: 'descending' })
  166. return
  167. }
  168. if (!this.ascending && this.descending) {
  169. this.ascending = false
  170. this.descending = false
  171. this.$emit('sort-change', { order: null })
  172. }
  173. },
  174. ascendingFn() {
  175. this.clearOther()
  176. this.ascending = !this.ascending
  177. this.descending = false
  178. this.$emit('sort-change', { order: this.ascending ? 'ascending' : null })
  179. },
  180. descendingFn() {
  181. this.clearOther()
  182. this.descending = !this.descending
  183. this.ascending = false
  184. this.$emit('sort-change', { order: this.descending ? 'descending' : null })
  185. },
  186. clearOther() {
  187. this.root.thChildren.map(item => {
  188. if (item !== this) {
  189. item.ascending = false
  190. item.descending = false
  191. }
  192. return item
  193. })
  194. },
  195. ondropdown(e) {
  196. this.$emit("filter-change", e)
  197. },
  198. /**
  199. * 获取父元素实例
  200. */
  201. getTable(name) {
  202. let parent = this.$parent
  203. let parentName = parent.$options.name
  204. while (parentName !== name) {
  205. parent = parent.$parent
  206. if (!parent) return false
  207. parentName = parent.$options.name
  208. }
  209. return parent
  210. }
  211. }
  212. }
  213. </script>
  214. <style lang="scss">
  215. $border-color: #ebeef5;
  216. $uni-primary: #007aff !default;
  217. .uni-table-th {
  218. padding: 12px 10px;
  219. /* #ifndef APP-NVUE */
  220. display: table-cell;
  221. box-sizing: border-box;
  222. /* #endif */
  223. font-size: 14px;
  224. font-weight: bold;
  225. color: #909399;
  226. border-bottom: 1px $border-color solid;
  227. }
  228. .uni-table-th-row {
  229. /* #ifndef APP-NVUE */
  230. display: flex;
  231. /* #endif */
  232. flex-direction: row;
  233. }
  234. .table--border {
  235. border-right: 1px $border-color solid;
  236. }
  237. .uni-table-th-content {
  238. display: flex;
  239. align-items: center;
  240. flex: 1;
  241. }
  242. .arrow-box {
  243. }
  244. .arrow {
  245. display: block;
  246. position: relative;
  247. width: 10px;
  248. height: 8px;
  249. // border: 1px red solid;
  250. left: 5px;
  251. overflow: hidden;
  252. cursor: pointer;
  253. }
  254. .down {
  255. top: 3px;
  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: $uni-primary;
  269. }
  270. }
  271. }
  272. .up {
  273. ::after {
  274. content: '';
  275. width: 8px;
  276. height: 8px;
  277. position: absolute;
  278. left: 2px;
  279. top: 5px;
  280. transform: rotate(45deg);
  281. background-color: #ccc;
  282. }
  283. &.active {
  284. ::after {
  285. background-color: $uni-primary;
  286. }
  287. }
  288. }
  289. </style>